diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 75f0af4..a5801a7 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -40,18 +40,54 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + + + + @@ -59,14 +95,13 @@ - - + + - @@ -75,24 +110,18 @@ - - - - - - - - - - - - - - - + + + + + + + + + @@ -105,11 +134,13 @@ @@ -118,10 +149,10 @@ true - @@ -196,13 +227,16 @@ + + + - + @@ -441,7 +475,32 @@ - + + + + + + @@ -453,72 +512,45 @@ 1423576956527 - - + - + - + + - - + + - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + - + + + + + + + - + + + + - - - - - - - - @@ -557,16 +587,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - @@ -576,24 +637,58 @@ - - - + + + + + + + + + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + - - + + diff --git a/out/production/Fractals/DrawingTools.class b/out/production/Fractals/DrawingTools.class new file mode 100644 index 0000000..86b057e Binary files /dev/null and b/out/production/Fractals/DrawingTools.class differ diff --git a/out/production/Fractals/Main.class b/out/production/Fractals/Main.class new file mode 100644 index 0000000..c6f7f2e Binary files /dev/null and b/out/production/Fractals/Main.class differ diff --git a/out/production/Fractals/Numbers.class b/out/production/Fractals/Numbers.class new file mode 100644 index 0000000..d3ef0e6 Binary files /dev/null and b/out/production/Fractals/Numbers.class differ diff --git a/out/production/Fractals/PythagorasTree$PythagorasTreeVariant.class b/out/production/Fractals/PythagorasTree$PythagorasTreeVariant.class new file mode 100644 index 0000000..9e59bd9 Binary files /dev/null and b/out/production/Fractals/PythagorasTree$PythagorasTreeVariant.class differ diff --git a/out/production/Fractals/PythagorasTree.class b/out/production/Fractals/PythagorasTree.class new file mode 100644 index 0000000..20467c3 Binary files /dev/null and b/out/production/Fractals/PythagorasTree.class differ diff --git a/out/production/Fractals/PythagorasTreeQueue.class b/out/production/Fractals/PythagorasTreeQueue.class new file mode 100644 index 0000000..c8fcd6c Binary files /dev/null and b/out/production/Fractals/PythagorasTreeQueue.class differ diff --git a/out/production/Fractals/PythagorasTriangle$1.class b/out/production/Fractals/PythagorasTriangle$1.class new file mode 100644 index 0000000..7878c41 Binary files /dev/null and b/out/production/Fractals/PythagorasTriangle$1.class differ diff --git a/out/production/Fractals/PythagorasTriangle.class b/out/production/Fractals/PythagorasTriangle.class new file mode 100644 index 0000000..dafa0ae Binary files /dev/null and b/out/production/Fractals/PythagorasTriangle.class differ diff --git a/out/production/Fractals/PythagorasTriangleList.class b/out/production/Fractals/PythagorasTriangleList.class new file mode 100644 index 0000000..65a16f3 Binary files /dev/null and b/out/production/Fractals/PythagorasTriangleList.class differ diff --git a/src/DrawingTools.java b/src/DrawingTools.java new file mode 100644 index 0000000..398905a --- /dev/null +++ b/src/DrawingTools.java @@ -0,0 +1,12 @@ +import ch.aplu.turtle.Turtle; + +/** + * Created by Christian on 10.02.2015. + */ +public class DrawingTools { + public static void drawSquare(Turtle turtle, double size) { + for (int i = 0; i < 4; i++) { + turtle.forward(size).left(90); + } + } +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..e7f294b --- /dev/null +++ b/src/Main.java @@ -0,0 +1,202 @@ +import ch.aplu.turtle.Options; +import ch.aplu.turtle.Playground; +import ch.aplu.turtle.Turtle; + +import java.awt.*; +import java.util.Scanner; + +/** + * Created by Christian on 10.02.2015. + */ +public class Main { + public static void main(String[] args) { + + playgroundSettings(); + + while (true) { + if (args.length > 0) newPythagorasTree(args[0]); + else newPythagorasTree(null); + } + } + + public static Turtle newTurtle() { + Playground playground = new Playground(); + playground.setAntiAliasing(true); + playground.setAutoscrolls(true); + playground.setPreferredSize(new Dimension(2160, 1440)); + playground.setMinimumSize(new Dimension(2160, 1440)); + playground.add(new Turtle(Color.GREEN)); + Turtle turtle = playground.getTurtle(0); + turtle.setFillColor(Color.BLACK).fill(); + turtle.setPenColor(Color.GREEN); + turtle.hideTurtle(); + + turtle.penUp(); + turtle.right(180); + turtle.forward(350); + turtle.left(90); + turtle.forward(50); + turtle.left(90); + turtle.penDown(); + return turtle; + } + + public static void newPythagorasTree(String input) { + PythagorasTree.PythagorasTreeVariant variant; + + Scanner scanner = new Scanner(System.in); + + if (input != null) variant = variantFromString(input); + else variant = variantInput(scanner); + + double size = squareSizeInput(scanner); + int loopSize = loopSizeInput(scanner); + int depth = depthInput(scanner); + double angle = 45; + + if (variant == PythagorasTree.PythagorasTreeVariant.AsymetricPythagorasTree) angle = angleInput(scanner); + + for (int i = 0; i < loopSize; i++) { + new PythagorasTreeQueue(variant, new PythagorasTriangleList(variant, depth, angle), newTurtle(), size, 0, depth); + } + } + + public static PythagorasTree.PythagorasTreeVariant variantFromString(String input) { + PythagorasTree.PythagorasTreeVariant variant; + + if (input.equalsIgnoreCase("SymetricPythagorasTree")) { + variant = PythagorasTree.PythagorasTreeVariant.SymetricPythagorasTree; + } else if (input.equalsIgnoreCase("AsymetricPythagorasTree")) { + variant = PythagorasTree.PythagorasTreeVariant.AsymetricPythagorasTree; + } else if (input.equalsIgnoreCase("RegularRandomPythagorasTree")) { + variant = PythagorasTree.PythagorasTreeVariant.RegularRandomPythagorasTree; + } else if (input.equalsIgnoreCase("PisanoPythagorasTreeOne")) { + variant = PythagorasTree.PythagorasTreeVariant.PisanoPythagorasTreeOne; + } else if (input.equalsIgnoreCase("PisanoPythagorasTreeTwo")) { + variant = PythagorasTree.PythagorasTreeVariant.PisanoPythagorasTreeTwo; + } else if (input.equalsIgnoreCase("PisanoPythagorasTreeThree")) { + variant = PythagorasTree.PythagorasTreeVariant.PisanoPythagorasTreeThree; + } else if (input.equalsIgnoreCase("PisanoPythagorasTreeFour")) { + variant = PythagorasTree.PythagorasTreeVariant.PisanoPythagorasTreeFour; + } else if (input.equalsIgnoreCase("PisanoPythagorasTreeFive")) { + variant = PythagorasTree.PythagorasTreeVariant.PisanoPythagorasTreeFive; + } else variant = PythagorasTree.PythagorasTreeVariant.unselected; + return variant; + } + + public static void playgroundSettings() { + Scanner scanner = new Scanner(System.in); + Options.setFrameTitle("PythagorasTree"); + + System.out.println("How wide should the window be?"); + int width = Integer.parseInt(scanner.nextLine()); + + System.out.println("How high should the window be?"); + int height = Integer.parseInt(scanner.nextLine()); + + Options.setPlaygroundSize(width, height); + Options.setFramePosition(0, 0); + } + + public static Double squareSizeInput(Scanner scanner) { + System.out.println("Size of First Square:"); + return Double.parseDouble(scanner.nextLine()); + } + + public static Integer loopSizeInput(Scanner scanner) { + System.out.println("Create Loop? Type '1' for no loop and any larger number for a loop of that length:"); + return Integer.parseInt(scanner.nextLine()); + } + + public static Integer depthInput(Scanner scanner) { + System.out.println("How many levels should the tree be deep?"); + return Integer.parseInt(scanner.nextLine()); + } + + public static Double angleInput(Scanner scanner) { + System.out.println("What angle should the triangles of the tree have?"); + System.out.println("The angle put into here will be used as the left angle."); + return Double.parseDouble(scanner.nextLine()); + } + + public static PythagorasTree.PythagorasTreeVariant variantInput(Scanner scanner) { + PythagorasTree.PythagorasTreeVariant variant = PythagorasTree.PythagorasTreeVariant.unselected; + while (variant == PythagorasTree.PythagorasTreeVariant.unselected) { + System.out.println("No variant selected! Type in variant (To get the variants, type in 'help'):"); + String variantString = scanner.nextLine(); + if (variantString.equals("help")) variantHelp(); + variant = variantFromString(variantString); + } + System.out.println(variant); + return variant; + } + public static void variantHelp() { + System.out.println("SymetricPythagorasTree:"); + System.out.println(""); + System.out.println("This creates a normal symetric Pythagoras Tree."); + System.out.println(""); + System.out.println(""); + System.out.println(""); + + System.out.println("AsymetricPythagorasTree:"); + System.out.println(""); + System.out.println("This creates an asymetric Pythagoras Tree."); + System.out.println("The used angle will be adjustable later."); + System.out.println(""); + System.out.println(""); + System.out.println(""); + + System.out.println("RegularRandomPythagorasTree:"); + System.out.println(""); + System.out.println("This creates an asymetric Pythagoras Tree."); + System.out.println("Each level of the tree will have it's own randomly generated angle."); + System.out.println("The generated angles will be between 20 and 70 degree."); + System.out.println(""); + System.out.println(""); + System.out.println(""); + + System.out.println("PisanoPythagorasTreeOne:"); + System.out.println(""); + System.out.println("This creates an asymetric Pythagoras Tree."); + System.out.println("The angles used in this tree are generated from the Fibonacci Numbers."); + System.out.println("This (and the following variants) are similar to the regular random trees, "); + System.out.println("just with angles generated from the Fibonacci Numbers."); + System.out.println("The angles of this variant calculated like this: "); + System.out.println("Fibonacci number of the Level modulo 4 multiplied by 10 plus 30"); + System.out.println(""); + System.out.println(""); + System.out.println(""); + + System.out.println("PisanoPythagorasTreeTwo:"); + System.out.println(""); + System.out.println("The angles of this variant calculated like this: "); + System.out.println("Fibonacci number of the Level modulo 2 multiplied by 30 plus 30"); + System.out.println(""); + System.out.println(""); + System.out.println(""); + + System.out.println("PisanoPythagorasTreeThree:"); + System.out.println(""); + System.out.println("The angles of this variant calculated like this: "); + System.out.println("Fibonacci number of the Level modulo 7 multiplied by 5 plus 30"); + System.out.println(""); + System.out.println(""); + System.out.println(""); + + System.out.println("PisanoPythagorasTreeFour:"); + System.out.println(""); + System.out.println("The angles of this variant calculated like this: "); + System.out.println("Fibonacci number of the Level modulo 31 plus 30"); + System.out.println(""); + System.out.println(""); + System.out.println(""); + + System.out.println("PisanoPythagorasTreeFive:"); + System.out.println(""); + System.out.println("The angles of this variant calculated like this: "); + System.out.println("Fibonacci number of the Level modulo 9 multiplied by 4 plus 29"); + System.out.println(""); + System.out.println(""); + System.out.println(""); + } +} diff --git a/src/Numbers.java b/src/Numbers.java new file mode 100644 index 0000000..26d2292 --- /dev/null +++ b/src/Numbers.java @@ -0,0 +1,27 @@ +import java.util.LinkedList; + +/** + * Created by Christian on 10.02.2015. + */ +public class Numbers { + LinkedList fibonacci = new LinkedList(); + LinkedList regularRandom = new LinkedList(); + + public Numbers() { + fibonacci.add((long) 1); + fibonacci.add((long) 1); + regularRandom.add(Math.random()); + } + + public long calcFibonacci(int index) { + if (index < fibonacci.size()) return fibonacci.get(index); + else return calcFibonacci(index - 1) + calcFibonacci(index - 2); + } + + public double getRegularRandom(int index){ + while (regularRandom.size() <= index) { + regularRandom.add(Math.random()); + } + return regularRandom.get(index); + } +} diff --git a/src/PythagorasTree.java b/src/PythagorasTree.java new file mode 100644 index 0000000..c4128f1 --- /dev/null +++ b/src/PythagorasTree.java @@ -0,0 +1,82 @@ +import ch.aplu.turtle.Turtle; + +import java.awt.*; +import java.util.LinkedList; + +/** + * Created by Christian on 10.02.2015. + */ +public class PythagorasTree { + private PythagorasTree rightSubTree; + private PythagorasTree leftSubTree; + private PythagorasTreeVariant variant; + private PythagorasTriangleList triangles; + private Turtle turtle; + private double size; + private int momentaryDepth; + private int maximalDepth; + + public PythagorasTree(PythagorasTreeVariant variant, PythagorasTriangleList triangles, Turtle turtle, double size, int momentaryDepth, int maximalDepth) { + this.variant = variant; + this.triangles = triangles; + this.turtle = turtle; + this.size = size; + this.momentaryDepth = momentaryDepth; + this.maximalDepth = maximalDepth; + } + + public void addPythagorasTree(PythagorasTreeQueue queue) { + if (momentaryDepth + 1 >= maximalDepth) return; + + turtle.forward(size); + + turtle.pushState(); + turtle.right(triangles.getLeftAngle(momentaryDepth)); + PythagorasTree treeOne = queue.remove(); + treeOne.drawPythagorasTree(queue); + + + turtle.popState(); + + + + turtle.setPenColor(drawColor(momentaryDepth, maximalDepth)); + + turtle.left(triangles.getRightAngle(momentaryDepth)); + turtle.forward(size * triangles.getRightSizeFactor(momentaryDepth)); + PythagorasTree treeTwo = queue.remove(); + treeTwo.drawPythagorasTree(queue); + treeOne.addPythagorasTree(queue); + treeTwo.addPythagorasTree(queue); + } + + public void drawPythagorasTree(PythagorasTreeQueue queue) { + rightSubTree = new PythagorasTree(variant, triangles, turtle, size * triangles.getRightSizeFactor(momentaryDepth), momentaryDepth + 1, maximalDepth); + queue.add(rightSubTree); + leftSubTree = new PythagorasTree(variant, triangles, turtle, size * triangles.getLeftSizeFactor(momentaryDepth), momentaryDepth + 1, maximalDepth); + queue.add(leftSubTree); + + DrawingTools.drawSquare(turtle, size); + + } + + public static Color drawColor(int momentaryDepth, int maximalDepth) { + + double colorFactor = 255 / maximalDepth; + + int red = (int) (colorFactor * (maximalDepth - momentaryDepth)); + if (red > 139) red = 139; + if (red < 0) red = 0; + int green = 255 - (int) (colorFactor * (maximalDepth - momentaryDepth)); + if (green < 69) green = 69; + int blue = (int) (colorFactor * ((maximalDepth - momentaryDepth) / 5)); + if (blue > 19) blue = 19; + return new Color(red, green, blue); + } + + public enum PythagorasTreeVariant { + SymetricPythagorasTree, AsymetricPythagorasTree, RegularRandomPythagorasTree, + PisanoPythagorasTreeOne, PisanoPythagorasTreeTwo, PisanoPythagorasTreeThree, PisanoPythagorasTreeFour, PisanoPythagorasTreeFive, + unselected, + } +} diff --git a/src/PythagorasTreeQueue.java b/src/PythagorasTreeQueue.java new file mode 100644 index 0000000..61cb404 --- /dev/null +++ b/src/PythagorasTreeQueue.java @@ -0,0 +1,16 @@ +import ch.aplu.turtle.Turtle; + +import java.util.LinkedList; + +/** + * Created by Christian on 10.02.2015. + */ +public class PythagorasTreeQueue extends LinkedList { + public PythagorasTreeQueue(PythagorasTree.PythagorasTreeVariant variant, PythagorasTriangleList triangles, Turtle turtle, double size, int momentaryDepth, int maximalDepth) { + super(); + add(new PythagorasTree(variant, triangles, turtle, size, momentaryDepth, maximalDepth)); + PythagorasTree tree = remove(); + tree.drawPythagorasTree(this); + tree.addPythagorasTree(this); + } +} diff --git a/src/PythagorasTriangle.java b/src/PythagorasTriangle.java new file mode 100644 index 0000000..b339330 --- /dev/null +++ b/src/PythagorasTriangle.java @@ -0,0 +1,79 @@ +/** + * Created by Christian on 10.02.2015. + */ +public class PythagorasTriangle { + private double leftSizeFactor; + private double leftAngle; + private double rightSizeFactor; + private double rightAngle; + private PythagorasTree.PythagorasTreeVariant variant; + + public PythagorasTriangle(PythagorasTree.PythagorasTreeVariant variant, int momentaryDepth, Numbers numbers, Double angle) { + + switch (variant) { + + + case SymetricPythagorasTree: + leftAngle = 45; + break; + + + case AsymetricPythagorasTree: + if (angle < 0 || angle > 90) return; + leftAngle = angle; + break; + + + case RegularRandomPythagorasTree: + leftAngle = numbers.getRegularRandom(momentaryDepth) * 50 + 20; + break; + + + case PisanoPythagorasTreeOne: + leftAngle = (numbers.calcFibonacci(momentaryDepth) % 4) * 10 + 30; + break; + + + case PisanoPythagorasTreeTwo: + leftAngle = (numbers.calcFibonacci(momentaryDepth) % 2) * 30 + 30; + break; + + + case PisanoPythagorasTreeThree: + leftAngle = (numbers.calcFibonacci(momentaryDepth) % 7) * 5 + 30; + break; + + + case PisanoPythagorasTreeFour: + leftAngle = (numbers.calcFibonacci(momentaryDepth) % 31) + 30; + break; + + + case PisanoPythagorasTreeFive: + leftAngle = (numbers.calcFibonacci(momentaryDepth) % 9) * 4 + 29; + break; + + default: + leftAngle = 45; + break; + } + rightAngle = (double) 90 - leftAngle; + leftSizeFactor = Math.sin(Math.toRadians(leftAngle)); + rightSizeFactor = Math.sin(Math.toRadians(rightAngle)); + } + public double getLeftSizeFactor() { + return leftSizeFactor; + } + + public double getLeftAngle() { + return leftAngle; + } + + public double getRightSizeFactor() { + return rightSizeFactor; + } + + public double getRightAngle() { + return rightAngle; + } +} diff --git a/src/PythagorasTriangleList.java b/src/PythagorasTriangleList.java new file mode 100644 index 0000000..839cc97 --- /dev/null +++ b/src/PythagorasTriangleList.java @@ -0,0 +1,31 @@ +import java.util.LinkedList; + +/** + * Created by Christian on 10.02.2015. + */ +public class PythagorasTriangleList { + private LinkedList triangles = new LinkedList(); + private Numbers numbers = new Numbers(); + + public PythagorasTriangleList(PythagorasTree.PythagorasTreeVariant variant, double maximalDepth, double angle) { + for (int i = 0; i < maximalDepth; i++) { + triangles.add(new PythagorasTriangle(variant, i, numbers, angle)); + } + } + + public double getLeftAngle(int index) { + return triangles.get(index).getLeftAngle(); + } + + public double getRightAngle(int index) { + return triangles.get(index).getRightAngle(); + } + + public double getLeftSizeFactor(int index) { + return triangles.get(index).getLeftSizeFactor(); + } + + public double getRightSizeFactor(int index) { + return triangles.get(index).getRightSizeFactor(); + } +} diff --git a/src/TurtleState.java b/src/TurtleState.java new file mode 100644 index 0000000..87579c0 --- /dev/null +++ b/src/TurtleState.java @@ -0,0 +1,16 @@ +import ch.aplu.turtle.Turtle; + +/** + * Created by Christian on 10.02.2015. + */ +public class TurtleState { + private double x; + private double y; + private double heading; + + public TurtleState(Turtle turtle) { + x = turtle.getX(); + y = turtle.getY(); + heading = turtle.heading(); + } +}