pythagoreantree/src/Main.java
Jan Christian Grünhage 494d13bba3 Added Seeds
The added Seed Class is containing the information needed for building a tree.
With this it is possible to have nice I/O when running the application without arguments but adding the possibility to run the application with arguments for nearly no I/O, without having much code twice
2015-02-11 12:26:23 +01:00

269 lines
11 KiB
Java

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) {
PythagorasTreeSeed seed;
if (input != null) {
seed = getSeedFromArg(input);
} else {
seed = getSeedFromInput();
}
double size = seed.size;
int loopSize = seed.loopSize;
int depth = seed.depth;
double angle = seed.angle;
PythagorasTree.PythagorasTreeVariant variant = seed.variant;
for (int i = 0; i < loopSize; i++) {
new PythagorasTreeQueue(variant, new PythagorasTriangleList(variant, depth, angle), newTurtle(), size, 0, depth);
}
}
private static PythagorasTreeSeed getSeedFromInput() {
PythagorasTree.PythagorasTreeVariant variant;
Scanner scanner = new Scanner(System.in);
variant = variantInput(scanner);
double size = squareSizeInput(scanner);
int loopSize = loopSizeInput(scanner);
int depth = depthInput(scanner);
double angle;
switch (variant) {
case AsymetricPythagorasTree:
angle = angleInput(scanner);
break;
default:
angle = 45;
break;
}
PythagorasTreeSeed seed = new PythagorasTreeSeed(size, loopSize, depth, angle, variant);
return seed;
}
public static PythagorasTreeSeed getSeedFromArg(String input) {
String[] splitInput = input.split("_");
PythagorasTree.PythagorasTreeVariant variant;
variant = variantFromArg(splitInput[0]);
double size = Double.parseDouble(splitInput[1]);
int loopSize = Integer.parseInt(splitInput[2]);
int depth = Integer.parseInt(splitInput[3]);
double angle;
switch (variant) {
case AsymetricPythagorasTree:
angle = Double.parseDouble(splitInput[4]);
break;
default:
angle = 45;
break;
}
PythagorasTreeSeed seed = new PythagorasTreeSeed(size, loopSize, depth, angle, variant);
return seed;
}
public static PythagorasTree.PythagorasTreeVariant variantFromInput(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 PythagorasTree.PythagorasTreeVariant variantFromArg(String input) {
PythagorasTree.PythagorasTreeVariant variant;
if (input.equalsIgnoreCase("1")) {
variant = PythagorasTree.PythagorasTreeVariant.SymetricPythagorasTree;
} else if (input.equalsIgnoreCase("2")) {
variant = PythagorasTree.PythagorasTreeVariant.AsymetricPythagorasTree;
} else if (input.equalsIgnoreCase("3")) {
variant = PythagorasTree.PythagorasTreeVariant.RegularRandomPythagorasTree;
} else if (input.equalsIgnoreCase("41")) {
variant = PythagorasTree.PythagorasTreeVariant.PisanoPythagorasTreeOne;
} else if (input.equalsIgnoreCase("42")) {
variant = PythagorasTree.PythagorasTreeVariant.PisanoPythagorasTreeTwo;
} else if (input.equalsIgnoreCase("43")) {
variant = PythagorasTree.PythagorasTreeVariant.PisanoPythagorasTreeThree;
} else if (input.equalsIgnoreCase("44")) {
variant = PythagorasTree.PythagorasTreeVariant.PisanoPythagorasTreeFour;
} else if (input.equalsIgnoreCase("45")) {
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 = variantFromInput(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("");
}
}