Koch Snowflake

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2008
Posts: 412
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Koch Snowflake

 
0
  #11
Jul 23rd, 2008
Originally Posted by LevelSix View Post
I placed int variables in the draw method, and the paint method that are derived from the size of arrylist x. After draw is done being called, the size is 6. After the program moves on to paint, the size is 0. This is where the problem lies.
Originally Posted by LevelSix View Post
I can't for the life of me see why it does this.

can you post your revised code? if you can't see it maybe we can
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Koch Snowflake

 
0
  #12
Jul 24th, 2008
Consider the following for your KochComponent. All calculations are done outside of the paintComponent method (you don't need to override paint() at all). For each iteration, you need to add three intermediate points to each line segment to form the triangle portion.
  1. public class KochComponent extends JComponent {
  2.  
  3. List<Point> points = null;
  4.  
  5. /** create the initial triangle */
  6. private void createTriangle() {
  7. points = new ArrayList<Point>();
  8.  
  9. int length = Math.min(getWidth(), getHeight())*2/3;
  10.  
  11. int x1 = (getWidth()-length)/2;
  12. int y1 = length/2;
  13. points.add(new Point(x1, y1));
  14.  
  15. int x2 = x1+length;
  16. int y2 = y1;
  17. points.add(new Point(x2, y2));
  18.  
  19. int x3 = x1+(length/2);
  20. int y3 = y1+length;
  21. points.add(new Point(x3, y3));
  22.  
  23. }
  24.  
  25. public void next() {
  26. numIterations++;
  27.  
  28. List<Point> newPoints = new ArrayList<Point>();
  29. // generate the new points for each line segment
  30. for(int i = 0; i<points.size(); i++) {
  31. Point p1 = points.get(i);
  32. Point p2 = i==points.size()-1 ?
  33. points.get(0) : points.get(i+1);
  34. newPoints.add(p1);
  35. newPoints.addAll(getKochPoints(p1, p2));
  36. }
  37. points = newPoints;
  38. repaint();
  39. }
  40.  
  41. public void paintComponent(Graphics g) {
  42. // this is just to account for the fact that
  43. // the component has no initial width or height
  44. if(points==null) {
  45. createTriangle();
  46. }
  47.  
  48. Graphics2D g2 = (Graphics2D)g;
  49.  
  50. // Use this code to draw the actual poly
  51. // Polygon koch = new Polygon();
  52. // for (Point p : points){
  53. // koch.addPoint(p.x, p.y);
  54. // }
  55. // g2.draw(koch);
  56.  
  57. // This just draws unconnected points
  58. // for demo purposes
  59. for(Point p : points) {
  60. g2.drawRect(p.x, p.y, 1, 1);
  61. }
  62. }
  63.  
  64. /** Generate the points to add for each segment
  65.   * for each iteration.
  66.   * I am just putting in the easy ones here.
  67.   * You get to do the trickier one!
  68.   */
  69. private List<Point> getKochPoints(Point p1, Point p2) {
  70. List<Point> kochPoints = new ArrayList<Point>();
  71.  
  72. int dx = p2.x-p1.x;
  73. int dy = p2.y-p1.y;
  74.  
  75. Point kp1 = new Point(p1.x+dx/3, p1.y+dy/3);
  76. kochPoints.add(kp1);
  77.  
  78. // you have to figure out how to generate
  79. // kp2 here (the top of the triangle)
  80.  
  81. Point kp3 = new Point(p1.x+2*dx/3, p1.y+2*dy/3);
  82. kochPoints.add(kp3);
  83.  
  84. return kochPoints;
  85. }
  86.  
  87. private int numIterations;
  88. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 55
Reputation: LevelSix is an unknown quantity at this point 
Solved Threads: 3
LevelSix's Avatar
LevelSix LevelSix is offline Offline
Junior Poster in Training

Re: Koch Snowflake

 
0
  #13
Aug 12th, 2008
Your code here seems to use a class Point. Yes?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Koch Snowflake

 
0
  #14
Aug 12th, 2008
Yes, java.awt.Point. It's part of the JDK, not a custom class.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 55
Reputation: LevelSix is an unknown quantity at this point 
Solved Threads: 3
LevelSix's Avatar
LevelSix LevelSix is offline Offline
Junior Poster in Training

Re: Koch Snowflake

 
0
  #15
Aug 12th, 2008
Ahh, that makes more sense. Thanks.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 55
Reputation: LevelSix is an unknown quantity at this point 
Solved Threads: 3
LevelSix's Avatar
LevelSix LevelSix is offline Offline
Junior Poster in Training

Re: Koch Snowflake

 
0
  #16
Aug 13th, 2008
So, after toying with the code you gave me, I've run into a problem. While I can supposedly get the next iteration to appear, all the others after that are incorrect.

  1. import java.awt.Graphics;
  2. import java.awt.Graphics2D;
  3. import javax.swing.JComponent;
  4. import java.awt.geom.Line2D;
  5. import java.util.ArrayList;
  6. import java.awt.geom.GeneralPath;
  7. import java.awt.Polygon;
  8. import java.awt.Point;
  9.  
  10. public class KochComponent3 extends JComponent {
  11.  
  12. ArrayList<Point> points = null;
  13.  
  14. /** create the initial triangle */
  15. private void createTriangle() {
  16. points = new ArrayList<Point>();
  17.  
  18. int length = Math.min(getWidth(), getHeight())*2/3;
  19.  
  20. int x1 = (getWidth()-length)/2;
  21. int y1 = length/2;
  22. points.add(new Point(x1, y1));
  23.  
  24. int x2 = x1+length;
  25. int y2 = y1;
  26. points.add(new Point(x2, y2));
  27.  
  28. int x3 = x1+(length/2);
  29. int y3 = y1+length;
  30. points.add(new Point(x3, y3));
  31.  
  32. }
  33.  
  34. public void next() {
  35. numIterations++;
  36.  
  37. ArrayList<Point> newPoints = new ArrayList<Point>();
  38. // generate the new points for each line segment
  39. for(int i = 0; i<points.size(); i++) {
  40. Point p1 = points.get(i);
  41. Point p2 = i==points.size()-1 ?
  42. points.get(0) : points.get(i+1);
  43. newPoints.add(p1);
  44. newPoints.addAll(getKochPoints(p1, p2));
  45. }
  46. points = newPoints;
  47. repaint();
  48. }
  49.  
  50. public void paintComponent(Graphics g)
  51. {
  52. if(points==null) {
  53. createTriangle();
  54. }
  55.  
  56. Graphics2D g2 = (Graphics2D)g;
  57. Polygon koch = new Polygon();
  58. for (Point p : points){
  59. koch.addPoint(p.x, p.y);
  60. }
  61. g2.draw(koch);
  62.  
  63. }
  64.  
  65. /** Generate the points to add for each segment
  66.   * for each iteration.
  67.   * I am just putting in the easy ones here.
  68.   * You get to do the trickier one!
  69.   */
  70. private ArrayList<Point> getKochPoints(Point p1, Point p2) {
  71. ArrayList<Point> kochPoints = new ArrayList<Point>();
  72.  
  73. int dx = p2.x-p1.x;
  74. int dy = p2.y-p1.y;
  75. double xtra = dx / 3;
  76. double use = xtra * xtra;
  77.  
  78. int test = (int)(Math.sqrt(use-(use/36)));
  79. int test2 = p1.y+dy/3-test;
  80.  
  81. Point kp1 = new Point(p1.x+dx/3, p1.y+dy/3);
  82. kochPoints.add(kp1);
  83. if(p1.y == p2.y)
  84. {
  85. Point kp2 = new Point((p1.x+(dx/3)+((dx/3)/2)), ((p1.y+dy/3)-test));
  86. kochPoints.add(kp2);
  87. }
  88. else if(p1.x > p1.y)
  89. {
  90. Point kp2 = new Point(p1.x, p1.y+(dy/3)+test+11);
  91. kochPoints.add(kp2);
  92.  
  93. }
  94. else
  95. {
  96. Point kp2 = new Point(p1.x+(dx), p1.y+(2*dy/3) + test+11);
  97. kochPoints.add(kp2);
  98.  
  99.  
  100.  
  101. }
  102.  
  103.  
  104. Point kp3 = new Point(p1.x+2*dx/3, p1.y+2*dy/3);
  105. kochPoints.add(kp3);
  106.  
  107. return kochPoints;
  108. }
  109.  
  110. private int numIterations;
  111. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Koch Snowflake

 
0
  #17
Aug 13th, 2008
Yes, your mid point is calc'd incorrectly. You need to calculate the point perpendicular to the line formed by p1 and p2 at it's midpoint to form an equilateral triangle with sides len/3 in length.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 55
Reputation: LevelSix is an unknown quantity at this point 
Solved Threads: 3
LevelSix's Avatar
LevelSix LevelSix is offline Offline
Junior Poster in Training

Re: Koch Snowflake

 
0
  #18
Aug 13th, 2008
Is this calculation different for each side? Like I was doing?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Koch Snowflake

 
0
  #19
Aug 13th, 2008
The equation for each of those line segments will vary and therefore the perpendicular will vary as well.

(using translate() and rotate() the entire thing becomes trivially easy, but I am assuming that your assignment will not allow that)
Last edited by Ezzaral; Aug 13th, 2008 at 4:54 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 55
Reputation: LevelSix is an unknown quantity at this point 
Solved Threads: 3
LevelSix's Avatar
LevelSix LevelSix is offline Offline
Junior Poster in Training

Re: Koch Snowflake

 
0
  #20
Aug 13th, 2008
I'm not sure if it will or not, and my professor is currently out of contact.

Which class are those methods from? Just from the names I can't really see how you'd use them for this.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC