Die Koch-Kurve ist ein Beispiel für eine überall stetige, aber nirgends differenzierbare Kurve. Es handelt sich bei ihr ferner um eines der ersten formal beschriebenen fraktalen Objekte. Die Koch-Kurve ist auch in Form der kochschen Schneeflocke bekannt, die durch geeignete Kombination dreier Koch-Kurven entsteht.
Wie zeichne ich ein regelmäßiges Polygon?
X Koordinate mit dem Sinus von 2 Pi * Zaehler / Anzahl der Polygone multiplizieren
Y Koordinate mit dem Sinus von 2 Pi * Zaehler / Anzahl der Polygone multiplizieren
so gebt ihr z.B. die Punkte aus
Wie man aus eine Linie eine KochKurve erstellt hat unser Proffessor ausführlich an der Tafel erklärt.
Die Formeln solltet ihr mitgeschrieben haben.
R = 2/3 p + q/3
T = 2/3 q + p/3
S = knifflig
Meine rekursive Funktion ruft übrigens 4 weitere Funktionen auf und trägt den schönen Namen. MachAusEinerLinieVier()
übergeben tue ich neben den Koordinaten noch das Graphics Objekt und die rekursionsstufe
Und was meint ihr in welchem Fall ich die Rekursion beende?
Richtig.. gar nicht so schwer...
fehlt nur noch die bunte GUI. Die habe ich aber noch nicht
UPDATE: Fertiger Code
KochApplication.java
Alles anzeigen
KochCanvas.java
Alles anzeigen
KochGUI.java
Alles anzeigen
Kochkurve.java (eigentlicher Algorithmus)
Alles anzeigen
Kochvis.java
Alles anzeigen
Wie zeichne ich ein regelmäßiges Polygon?
X Koordinate mit dem Sinus von 2 Pi * Zaehler / Anzahl der Polygone multiplizieren
Y Koordinate mit dem Sinus von 2 Pi * Zaehler / Anzahl der Polygone multiplizieren
so gebt ihr z.B. die Punkte aus
Wie man aus eine Linie eine KochKurve erstellt hat unser Proffessor ausführlich an der Tafel erklärt.
Die Formeln solltet ihr mitgeschrieben haben.
R = 2/3 p + q/3
T = 2/3 q + p/3
S = knifflig

Meine rekursive Funktion ruft übrigens 4 weitere Funktionen auf und trägt den schönen Namen. MachAusEinerLinieVier()
übergeben tue ich neben den Koordinaten noch das Graphics Objekt und die rekursionsstufe
Und was meint ihr in welchem Fall ich die Rekursion beende?
Richtig.. gar nicht so schwer...
fehlt nur noch die bunte GUI. Die habe ich aber noch nicht
UPDATE: Fertiger Code
KochApplication.java
Quellcode
- import java.awt.event.*;
- /**
- * Applikation (verbindet GUI und Kochkurve)
- * @author tbr
- *
- */
- public class KochApplication implements ItemListener, WindowListener {
- public static int depth=1, polygons=3;
- public static boolean clockwise=true;
- @SuppressWarnings("static-access")
- /**
- * reagiert auf Aenderungen im Formular
- * @param arg0 Item
- */
- public void itemStateChanged(ItemEvent arg0) {
- String s = arg0.getItem().toString();
- if(s.substring(0,5).equals("depth"))
- this.depth = Integer.parseInt(s.substring(6));
- else if(s.equals("clockwise"))
- this.clockwise = true;
- else if(s.equals("counterclockwise"))
- this.clockwise = false;
- else //4-sided
- this.polygons = Integer.parseInt(s.substring(0,s.indexOf("-")));
- }
- public void windowClosing(WindowEvent arg0) {
- System.exit(0);
- }
- public void windowOpened(WindowEvent arg0) {
- // TODO Auto-generated method stub
- }
- public void windowClosed(WindowEvent arg0) {
- // TODO Auto-generated method stub
- }
- public void windowIconified(WindowEvent arg0) {
- // TODO Auto-generated method stub
- }
- public void windowDeiconified(WindowEvent arg0) {
- // TODO Auto-generated method stub
- }
- public void windowActivated(WindowEvent arg0) {
- // TODO Auto-generated method stub
- }
- public void windowDeactivated(WindowEvent arg0) {
- // TODO Auto-generated method stub
- }
- }
KochCanvas.java
Quellcode
- import java.awt.*;
- /**
- * Klasse zum Zeichnen (reagiert auf Knopfdruck)
- * @author tbr
- *
- */
- @SuppressWarnings("serial")
- public class KochCanvas extends Canvas {
- /**
- * Konstruktor
- */
- public KochCanvas() {
- super.setBackground(Color.GRAY);
- }
- /**
- * Zeichne
- */
- public void paint(Graphics g) {
- //(Graphics g, weite, hoehe, rekursionstiefe, ecken, uhrzeigersinn?) {
- new Kochkurve(g, this.getWidth(), this.getHeight(), KochApplication.depth, KochApplication.polygons, KochApplication.clockwise);
- }
- }
KochGUI.java
Quellcode
- import java.awt.*;
- import java.awt.event.*;
- /**
- * GUI von Kochvis
- * @author tbr
- *
- */
- @SuppressWarnings("serial")
- public class KochGUI extends Frame {
- @SuppressWarnings("static-access")
- public KochGUI(KochApplication application) {
- super("KochGUI");
- GridBagLayout layout = new GridBagLayout();
- setLayout(layout);
- GridBagConstraints constraint = new GridBagConstraints();
- constraint.gridwidth = 1;
- constraint.gridheight = 7;
- constraint.gridx = 1;
- constraint.gridy = 1;
- final Canvas content = new KochCanvas();
- content.setSize(400,570);
- // constraint.fill = constraint.VERTICAL;
- constraint.fill = constraint.HORIZONTAL;
- layout.setConstraints(content, constraint);
- add(content);
- constraint.gridheight = 1;
- constraint.gridx = 2;
- constraint.gridy = 1;
- Label text1 = new Label("Depth of Recursion");
- layout.setConstraints(text1, constraint);
- add(text1);
- Choice choice1 = new Choice(); //Depth of Recursion
- choice1.addItemListener(application);
- for(int i=1; i<10; i++)
- choice1.add("depth "+i);
- constraint.gridy++;
- layout.setConstraints(choice1, constraint);
- add(choice1);
- Label text2 = new Label("Polygon");
- constraint.gridy++;
- layout.setConstraints(text2, constraint);
- add(text2);
- Choice choice2 = new Choice(); //Polygon
- choice2.addItemListener(application);
- for(int i=3; i<10; i++)
- choice2.add(i+"-sided");
- constraint.gridy++;
- layout.setConstraints(choice2, constraint);
- add(choice2);
- //Orientierung
- Label text3 = new Label("Orientation");
- constraint.gridy++;
- layout.setConstraints(text3, constraint);
- add(text3);
- final Choice choice3 = new Choice();
- choice3.addItemListener(application);
- choice3.add("clockwise");
- choice3.add("counterclockwise");
- constraint.gridy++;
- layout.setConstraints(choice3, constraint);
- add(choice3);
- Button button = new Button("draw");
- constraint.gridy++;
- constraint.ipady = 200;
- //constraint.fill = constraint.BOTH;
- layout.setConstraints(button, constraint);
- button.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- content.repaint();
- choice3.add("asd");
- //choice3.repaint();
- }
- });
- add(button);
- setSize(600,600);
- setLocation(150,100);
- // setResizable(false);
- pack();
- addWindowListener(application);
- setVisible(true);
- }
- }
Kochkurve.java (eigentlicher Algorithmus)
Quellcode
- import java.awt.*;
- /**
- * Klasse zur Berechnung des Kochpolygons
- * @author tbr
- *
- */
- public class Kochkurve {
- /**
- * Zeichnet das Polygon
- * Konstruktor
- * @param g
- * @param width
- * @param level
- * @param polygons
- * @param clockwise
- */
- public Kochkurve(Graphics g, int width, int height, int level, int polygons, boolean clockwise) {
- int r = width/2, paddingx=r, paddingy=height/2;
- int multiplikant = clockwise ? 1 : -1;
- Node tmp=null;
- for(int i=0; i<polygons+1; i++) {
- double phi = i*2*Math.PI/polygons;
- double x = Math.sin(phi) * -r * 0.85; //Fuelle nur 85% der Flaeche
- double y = Math.cos(phi) * -r * 0.85;
- Node calc = new Node(Math.round(x+paddingx), Math.round(y+paddingy));
- if(tmp != null)
- KochRek(g, tmp, calc, level, multiplikant);
- tmp = calc;
- }
- }
- /**
- * rekursive Funktion um aus einer Linie eine Kochkurve zu machen
- * @param g -> Grafikobjekt auf dem gezeichnet werden soll
- * @param p -> Knotenpunkt der Linie
- * @param q -> Knotenpunkt der Linie
- * @param level -> Rekursionstiefe
- * @param multiplikant -> um die Richtung der Operation zu beeinflussen
- */
- public void KochRek(Graphics g, Node p, Node q, int level, int multiplikant) {
- if(level == 1) {
- g.drawLine((int)Math.round(p.x),(int)Math.round(p.y),
- (int)Math.round(q.x), (int)Math.round(q.y));
- return;
- }
- Node R = new Node(p.x*2/3 + q.x/3, p.y*2/3 + q.y/3);
- Node T = new Node(p.x/3 + q.x*2/3, p.y/3 + q.y*2/3);
- Node S = new Node((int)((R.x+T.x)/2+ multiplikant*((R.y-T.y)*Math.sqrt(3)/2)),
- (int)((R.y+T.y)/2+ multiplikant*((T.x-R.x)*Math.sqrt (3)/2)));
- --level;
- KochRek(g, p, R, level, multiplikant);
- KochRek(g, R, S, level, multiplikant);
- KochRek(g, S, T, level, multiplikant);
- KochRek(g, T, q, level, multiplikant);
- }
- }
- /**
- * Abbildung eines Punktes im 2 Dimensionalen Koordinatensystem
- * @author Torben Brodt
- *
- */
- class Node {
- public double x, y;
- public Node(double x, double y) {
- this.x = x;
- this.y = y;
- }
- }
Kochvis.java
Quellcode
- /**
- * Ruft das Koch-Programm auf
- * @author tbr
- *
- * Der schwedische Mathematiker Helge von Koch stellte 1904 eines der ersten
- * formal beschriebenen fraktalen Objekte vor, welches inzwischen als Kochsche
- * Schneeflocke bekannt ist. Die Kochsche Schneeflocke ist ein Beispiel für eine stetige
- * Kurve, die an keiner Stelle eine Tangente besitzt. Außerdem handelt es sich bei
- * ihr um eine unendlich lange Kurve, die eine endliche Fläche einschließt.
- */
- public class Kochvis {
- public static void main(String[] args) {
- KochApplication application = new KochApplication();
- new KochGUI(application);
- }
- }