diff --git a/java/KochscheKurve.java b/java/KochscheKurve.java new file mode 100644 index 0000000..2e6bb03 --- /dev/null +++ b/java/KochscheKurve.java @@ -0,0 +1,112 @@ +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; + +/** + * Kochsche Kurve + * + * @author Andreas Jaggi + * @created 26. M�rz 2004 + * @version 1.0 + */ + +public class KochscheKurve + extends JFrame { + /** + * Constructor for the KochscheKurve object + */ + public KochscheKurve() { + setSize( 600, 600 ); + setTitle( "Die Kochsche Kurve" ); + } + + + /** + * �berladene "interne" Methode, die aufgerufen wird, wenn das Fenster neu + * gezeichnet werden muss + * + * @param g Graphik-Objekt, auf dem gezeichnet wird + */ + public void paint( Graphics g ) { + super.paint( g ); + + double x1; + double x2; + double x3; + double y1; + double y2; + double y3; + int depth = 15; + + x1 = 100; + y1 = 400; + x2 = 500; + y2 = 400; + x3 = ( x2 - x1 ) * Math.cos( -Math.PI / 3 ) - + ( y2 - y1 ) * Math.sin( -Math.PI / 3 ) + x1; + y3 = ( x2 - x1 ) * Math.sin( -Math.PI / 3 ) + + ( y2 - y1 ) * Math.cos( -Math.PI / 3 ) + y1; + + koch( g, depth, x2, y2, x1, y1 ); + koch( g, depth, x1, y1, x3, y3 ); + koch( g, depth, x3, y3, x2, y2 ); + } + + + /** + * Rekursive Funktion, welche den Fraktal zwischen zwei Punkten bis zu einer + * bestimmten Tiefe zeichnet. + * + * @param g Graphik-Objekt, auf dem gezeichnet wird + * @param depth Rekursionstiefe + * @param x1 X-Koordinate des ersten Punktes + * @param y1 Y-Koordinate des ersten Punktes + * @param x2 X-Koordinate des zweiten Punktes + * @param y2 X-Koordinate des zweiten Punktes + */ + public void koch( Graphics g, int depth, double x1, double y1, double x2, + double y2 ) { + double x13 = x1 + ( x2 - x1 ) / 3.0; + double x23 = x1 + 2.0 * ( x2 - x1 ) / 3.0; + + double y13 = y1 + ( y2 - y1 ) / 3.0; + double y23 = y1 + 2.0 * ( y2 - y1 ) / 3.0; + + double xd = ( x23 - x13 ) * Math.cos( -Math.PI / 3 ) - + ( y23 - y13 ) * Math.sin( -Math.PI / 3 ) + x13; + double yd = ( x23 - x13 ) * Math.sin( -Math.PI / 3 ) + + ( y23 - y13 ) * Math.cos( -Math.PI / 3 ) + y13; + + if ( depth > 0 ) { + koch( g, depth - 1, x1, y1, x13, y13 ); + koch( g, depth - 1, x13, y13, xd, yd ); + koch( g, depth - 1, xd, yd, x23, y23 ); + koch( g, depth - 1, x23, y23, x2, y2 ); + } else { + g.drawLine( (int) x1, (int) y1, (int) x2, (int) y2 ); + } + } + + + /** + * The main program for the KochscheKurve class + * + * @param args The command line arguments + */ + public static void main( String[] args ) { + KochscheKurve graf = new KochscheKurve(); + + graf.setVisible( true ); + + graf.addWindowListener( + new WindowAdapter() { + public void windowClosing( WindowEvent e ) { + System.exit( 0 ); + } + } + ); + + } + +} +