/*
* Author: Daniel Tanner
* Dragon Curve Program
*
* This program runs a recursive program to keep replacing a given
* string such as "f" with "f+h" and "h" with "f-h",
* and after a certain number of said recursions, translates that
* string into a physical display. The result is known as the
* Dragon Curve.
*/
import java.awt.*;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DragonCurve extends JPanel {
public Graphics g1;
public Color c =Color.WHITE; // set colour to white to create the blanks
public Random r = new Random();
public static double degToRad(int deg) { //degree to radian conversion
return deg * Math.PI / 180;
}
//paint method is automatically called for us by tree recursive (will be called 4 times! Once for each call to drawFractal
public void paint(final Graphics g) {
this.g1 = g;
g1.setColor(c);
int numRecursions = 14;
double startAngle = degToRad(0);
int distance = 3;
//First call to do the drawing, this call draws the trunk: start(X,Y) at base, angle 0, len calculated)
drawCurve(getWidth() / 2, getHeight() / 2, numRecursions, startAngle, distance); //first call
//System.out.println("in paint, number of calls to drawFractal is " + count +"\n");
}
//dragon curve method -- starts at the middle and applies rules to curve
public void drawCurve(int x0, int y0, int numRecursions, double startAngle, int distance)
{
String dragonString = "f";
for(int i = 0; i < numRecursions; i++)
{
String tempString = "";
for(int j = 0; j < dragonString.length(); j++)
{ //replace the f with f + h
if(dragonString.charAt(j) == 'f')
tempString += "f-h";
else if(dragonString.charAt(j) == 'h')
tempString += "f+h";
else
tempString += dragonString.charAt(j);
}
dragonString = tempString;
}
System.out.println(dragonString);
//now draw the image
for(int index = 0; index < dragonString.length(); index++)
{
if(dragonString.charAt(index) == 'f' || dragonString.charAt(index) == 'h')
{
g1.setColor(new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255))); //random colour (default is white)
int x1 = (int)(x0 + distance * Math.cos(startAngle));
int y1 = (int)(y0 + distance * Math.sin(startAngle));
g1.drawLine(x0, y0, x1, y1);
x0 = x1; y0 = y1;
}
else if(dragonString.charAt(index) == 'g')
{
g1.setColor(new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255))); //random colour (default is white)
int x1 = (int)(x0 + distance * Math.cos(startAngle));
int y1 = (int)(y0 + distance * Math.sin(startAngle));
x0 = x1; y0 = y1;
}
else if(dragonString.charAt(index) == '+')
startAngle = startAngle - Math.PI / 2.0;
else if(dragonString.charAt(index) == '-')
startAngle = startAngle + Math.PI / 2.0;
}
}
//main method
public static void main(String args[]) {
// System.out.println("count begins with at " + count +"\n");
JFrame FF = new JFrame("Drawing the Dragon Curve");
FF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FF.setVisible(true); //Show the first drawing.
DragonCurve F = new DragonCurve(); //call default constructor (not written); F is the Tree.
FF.setBackground(Color.BLACK);
FF.add(F);
FF.setSize(1000, 1000); //(width 1200,height 1000)
}
}
Friday Night Open Thread: Motivation
6 hours ago
No comments:
Post a Comment