943,901 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 8449
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jul 23rd, 2008
0

Re: Koch Snowflake

Click to Expand / Collapse  Quote originally posted by LevelSix ...
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.
Click to Expand / Collapse  Quote originally posted by LevelSix ...
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
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Jul 24th, 2008
0

Re: Koch Snowflake

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.
java Syntax (Toggle Plain Text)
  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. }
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 12th, 2008
0

Re: Koch Snowflake

Your code here seems to use a class Point. Yes?
Reputation Points: 22
Solved Threads: 3
Junior Poster in Training
LevelSix is offline Offline
59 posts
since May 2008
Aug 12th, 2008
0

Re: Koch Snowflake

Yes, java.awt.Point. It's part of the JDK, not a custom class.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 12th, 2008
0

Re: Koch Snowflake

Ahh, that makes more sense. Thanks.
Reputation Points: 22
Solved Threads: 3
Junior Poster in Training
LevelSix is offline Offline
59 posts
since May 2008
Aug 13th, 2008
0

Re: Koch Snowflake

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.

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 22
Solved Threads: 3
Junior Poster in Training
LevelSix is offline Offline
59 posts
since May 2008
Aug 13th, 2008
0

Re: Koch Snowflake

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 13th, 2008
0

Re: Koch Snowflake

Is this calculation different for each side? Like I was doing?
Reputation Points: 22
Solved Threads: 3
Junior Poster in Training
LevelSix is offline Offline
59 posts
since May 2008
Aug 13th, 2008
0

Re: Koch Snowflake

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 13th, 2008
0

Re: Koch Snowflake

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.
Reputation Points: 22
Solved Threads: 3
Junior Poster in Training
LevelSix is offline Offline
59 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Problems creating a Web Service Implementation
Next Thread in Java Forum Timeline: Decryption of PNG image for secret txt





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC