Koch Snowflake

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

Join Date: May 2007
Posts: 4,472
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: 513
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Koch Snowflake

 
0
  #21
Aug 13th, 2008
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
  #22
Aug 13th, 2008
Hmm, I don't see how rotate would be applied, unless you rotate polygon.... Well, there's an idea.
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
  #23
Aug 13th, 2008
An idea that failed miserably. I can't seem to get the rotate(double) method to work at all. It tells me it can't find the method. I thought it was part of the Graphics2D package?

I'm probably implementing it wrong though, as I'm trying to do koch.rotate(theta);. Theta being the angle of course.
Last edited by LevelSix; Aug 13th, 2008 at 7:15 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,472
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: 513
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Koch Snowflake

 
0
  #24
Aug 13th, 2008
Given a Graphics2D context reference of "g2", you just call g2.rotate(theta) to rotate about the origin theta radians.
(Keeping in mind that the transformations are cumulative - the rotation and translation transforms are concatenated with the current transform on each call)
Last edited by Ezzaral; Aug 13th, 2008 at 8:00 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
  #25
Aug 13th, 2008
Adding Grapics2D to the getKochPoints method would seem to cause numerous problems however. Then the calling of the method would have to be changed as well, and that method doesn't support graphics content either.
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
  #26
Aug 14th, 2008
As I'm not sure how to implement the rotate method, I went back to try to get the calculations right. Unfortunately, I have not got it correct. My updated code:

  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.  
  13. ArrayList<Point> points = null;
  14.  
  15. /** create the initial triangle */
  16. private void createTriangle()
  17. {
  18. points = new ArrayList<Point>();
  19.  
  20. int length = Math.min(getWidth(), getHeight())*2/3;
  21.  
  22. int x1 = (getWidth()-length)/2;
  23. int y1 = length/2;
  24. points.add(new Point(x1, y1));
  25.  
  26. int x2 = x1+length;
  27. int y2 = y1;
  28. points.add(new Point(x2, y2));
  29.  
  30. int x3 = x1+(length/2);
  31. int y3 = y1+length;
  32. points.add(new Point(x3, y3));
  33.  
  34. }
  35.  
  36. public void next()
  37. {
  38. numIterations++;
  39.  
  40. ArrayList<Point> newPoints = new ArrayList<Point>();
  41. // generate the new points for each line segment
  42. for(int i = 0; i<points.size(); i++) {
  43. Point p1 = points.get(i);
  44. Point p2 = i==points.size()-1 ?
  45. points.get(0) : points.get(i+1);
  46. newPoints.add(p1);
  47. newPoints.addAll(getKochPoints(p1, p2));
  48.  
  49. }
  50. points = newPoints;
  51. xAngle += 30;
  52. yAngle += 30;
  53. repaint();
  54. }
  55.  
  56. public void paintComponent(Graphics g)
  57. {
  58. if(points==null)
  59. {
  60. createTriangle();
  61. }
  62.  
  63. Graphics2D g2 = (Graphics2D)g;
  64. Polygon koch = new Polygon();
  65. for (Point p : points){
  66. koch.addPoint(p.x, p.y);
  67. }
  68. g2.draw(koch);
  69.  
  70. }
  71.  
  72.  
  73.  
  74. /** Generate the points to add for each segment
  75.   * for each iteration.
  76.   * I am just putting in the easy ones here.
  77.   * You get to do the trickier one!
  78.   */
  79. private ArrayList<Point> getKochPoints(Point p1, Point p2)
  80. {
  81. ArrayList<Point> kochPoints = new ArrayList<Point>();
  82.  
  83. int dx = p2.x-p1.x;
  84. int dy = p2.y-p1.y;
  85.  
  86.  
  87. Point kp1 = new Point(p1.x+dx/3, p1.y+dy/3);
  88. kochPoints.add(kp1);
  89.  
  90.  
  91.  
  92.  
  93. if(p1.y == p2.y)
  94. {
  95. Point kp2 = new Point();
  96. kp2.setLocation(kp1);
  97. kp2.translate((int)((dx/3)/2), (int)(-dx/3));
  98. kochPoints.add(kp2);
  99. }
  100. else if(p1.x > p1.y)
  101. {
  102.  
  103.  
  104. double v1 = Math.toRadians(30);
  105. double v2 = Math.toRadians(60);
  106. Point kp2 = new Point();
  107. kp2.setLocation(kp1);
  108. kp2.translate((int)(-Math.cos(v1)*dx/3), (int)(-Math.sin(v2)*2*dx/3 + dx/6));
  109. kochPoints.add(kp2);
  110.  
  111. }
  112. else
  113. {
  114. double v1 = Math.toRadians(xAngle);
  115. double v2 = Math.toRadians(yAngle);
  116. Point kp2 = new Point();
  117. kp2.setLocation(kp1);
  118. kp2.translate((int)(Math.cos(v2)*dx + dx/6), (int)(Math.sin(v1)*dx/3));
  119. kochPoints.add(kp2);
  120. }
  121.  
  122. Point kp3 = new Point(p1.x+2*dx/3, p1.y+2*dy/3);
  123. kochPoints.add(kp3);
  124.  
  125.  
  126. return kochPoints;
  127. }
  128. private int xAngle = 30;
  129. private int yAngle = 60;
  130. private int numIterations;
  131.  
  132. }
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
  #27
Aug 14th, 2008
Would recursion make this problem any easier?
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