| | |
Koch Snowflake
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
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.
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)
(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.
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:
Java Syntax (Toggle Plain Text)
import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent; import java.awt.geom.Line2D; import java.util.ArrayList; import java.awt.geom.GeneralPath; import java.awt.Polygon; import java.awt.Point; public class KochComponent3 extends JComponent { ArrayList<Point> points = null; /** create the initial triangle */ private void createTriangle() { points = new ArrayList<Point>(); int length = Math.min(getWidth(), getHeight())*2/3; int x1 = (getWidth()-length)/2; int y1 = length/2; points.add(new Point(x1, y1)); int x2 = x1+length; int y2 = y1; points.add(new Point(x2, y2)); int x3 = x1+(length/2); int y3 = y1+length; points.add(new Point(x3, y3)); } public void next() { numIterations++; ArrayList<Point> newPoints = new ArrayList<Point>(); // generate the new points for each line segment for(int i = 0; i<points.size(); i++) { Point p1 = points.get(i); Point p2 = i==points.size()-1 ? points.get(0) : points.get(i+1); newPoints.add(p1); newPoints.addAll(getKochPoints(p1, p2)); } points = newPoints; xAngle += 30; yAngle += 30; repaint(); } public void paintComponent(Graphics g) { if(points==null) { createTriangle(); } Graphics2D g2 = (Graphics2D)g; Polygon koch = new Polygon(); for (Point p : points){ koch.addPoint(p.x, p.y); } g2.draw(koch); } /** Generate the points to add for each segment * for each iteration. * I am just putting in the easy ones here. * You get to do the trickier one! */ private ArrayList<Point> getKochPoints(Point p1, Point p2) { ArrayList<Point> kochPoints = new ArrayList<Point>(); int dx = p2.x-p1.x; int dy = p2.y-p1.y; Point kp1 = new Point(p1.x+dx/3, p1.y+dy/3); kochPoints.add(kp1); if(p1.y == p2.y) { Point kp2 = new Point(); kp2.setLocation(kp1); kp2.translate((int)((dx/3)/2), (int)(-dx/3)); kochPoints.add(kp2); } else if(p1.x > p1.y) { double v1 = Math.toRadians(30); double v2 = Math.toRadians(60); Point kp2 = new Point(); kp2.setLocation(kp1); kp2.translate((int)(-Math.cos(v1)*dx/3), (int)(-Math.sin(v2)*2*dx/3 + dx/6)); kochPoints.add(kp2); } else { double v1 = Math.toRadians(xAngle); double v2 = Math.toRadians(yAngle); Point kp2 = new Point(); kp2.setLocation(kp1); kp2.translate((int)(Math.cos(v2)*dx + dx/6), (int)(Math.sin(v1)*dx/3)); kochPoints.add(kp2); } Point kp3 = new Point(p1.x+2*dx/3, p1.y+2*dy/3); kochPoints.add(kp3); return kochPoints; } private int xAngle = 30; private int yAngle = 60; private int numIterations; }
![]() |
Similar Threads
- Koch Curve in Python (Python)
Other Threads in the Java Forum
- Previous Thread: compiler as the final year project
- Next Thread: Drag n Drop in same JList
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class client code component consumer csv database desktop eclipse error fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie number objects online oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time title tree tutorial-sample ubuntu update windows working






