Last active
May 16, 2016 15:38
-
-
Save Cranc/9e45c5ea06cca14e5917d06cb2a5ab6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.PrintWriter; | |
/** | |
* @author Lukas Kalbertodt | |
*/ | |
public class Sierpinski { | |
private static int svgHeight = 1000; | |
private static int svgWidth = 1000; | |
private static String HTMLHeader = "<!DOCTYPE html>\n" + | |
"<html>\n" + | |
"<body>"; | |
private static String HTMLclose = "</body>\n" + | |
"</html>"; | |
private static String XMLhead = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"; | |
private static String svgclose = "</svg>"; | |
private static String svghead = "<svg height=\""+ svgHeight +"\" width=\""+ svgWidth +"\">"; | |
private static String polygonhead = "<polygon points=\""; | |
private static String polygonstyleBlack = "\"style=\"fill:black;stroke:black;stroke-width:1;fill-rule:nonzero;\" />"; | |
private static String polygonstyleWhite = "\"style=\"fill:white;stroke:black;stroke-width:1;fill-rule:nonzero;\" />"; | |
private static int mainHeight = 1000; | |
private static int mainWidth = 1000; | |
private static boolean html = true; | |
public static void main(String[] args) { | |
// #### Try to parse command line argument #### | |
int iterations = 0; | |
if (args.length < 1) { | |
printUsage(); | |
return; | |
} | |
try { | |
iterations = Integer.parseInt(args[0]); | |
} catch(Exception e) { | |
System.out.println("I wasn't able to parse your argument as " | |
+ "an integer :("); | |
printUsage(); | |
return; | |
} | |
if (iterations < 0) { | |
System.out.println("You need to hand me a POSITIVE integer :("); | |
printUsage(); | |
return; | |
} | |
// #### Open file #### | |
PrintWriter out; | |
try { | |
String fileName; | |
if(html) | |
fileName = "sierpinski-" + iterations + ".html"; | |
else | |
fileName = "sierpinski-" + iterations + ".svg"; | |
out = new PrintWriter(fileName, "UTF-8"); | |
} catch (Exception e) { | |
System.out.println("Oops, some IO error occurred!"); | |
return; | |
} | |
// TODO: Write data to file | |
// Dummy data. Hint: this isn't valid svg! | |
if(html) | |
out.println(HTMLHeader); | |
out.println(XMLhead); | |
out.println(svghead); | |
//generate initial triangle | |
String draw = polygonhead; | |
Point a,b,c; | |
int x,y; | |
x = (svgWidth - mainWidth) / 2; | |
y = (svgHeight - mainHeight) / 2; | |
y += svgHeight; | |
a = new Point(x,y); | |
x = (svgWidth - mainWidth) / 2; | |
y = (svgHeight - mainHeight) / 2; | |
x += (mainWidth / 2); | |
b = new Point(x,y); | |
x = (svgWidth - mainWidth) / 2; | |
y = (svgHeight - mainHeight) / 2; | |
x += mainWidth; | |
y += mainHeight; | |
c = new Point(x,y); | |
draw += a.x + "," + a.y; | |
draw += " "; | |
draw += b.x + "," + b.y; | |
draw += " "; | |
draw += c.x + "," + c.y; | |
draw += polygonstyleBlack; | |
out.println(draw); | |
Triangle t = new Triangle(a,b,c); | |
drawit(iterations,t,out); | |
//close svg | |
out.println(svgclose); | |
if(html) | |
out.println(HTMLclose); | |
// I miss RAII :/ | |
out.close(); | |
} | |
/** | |
* Prints information about how to invoke this program on the terminal. | |
*/ | |
private static void printUsage() { | |
System.out.println("Usage:"); | |
System.out.println(" java Sierpinski <iterations>"); | |
System.out.println(); | |
System.out.println(" For example: java Sierpinski 4"); | |
} | |
private static void drawit(int iter, Triangle tri, PrintWriter out){ | |
if(iter <= 0) | |
return; | |
Triangle newtri[] = new Triangle[3]; | |
String draw = polygonhead; | |
Point a,b,c; | |
a = new Point((tri.a.x + tri.b.x) / 2, (tri.a.y + tri.b.y) / 2); | |
b = new Point((tri.b.x + tri.c.x) / 2, (tri.b.y + tri.c.y) / 2); | |
c = new Point((tri.a.x + tri.c.x) / 2, (tri.a.y + tri.c.y) / 2); | |
//draw inner polygon | |
draw += a.x + "," + a.y; | |
draw += " "; | |
draw += b.x + "," + b.y; | |
draw += " "; | |
draw += c.x + "," + c.y; | |
draw += polygonstyleWhite; | |
out.println(draw); | |
//generate new triangles | |
newtri[0] = new Triangle(tri.a,a,c); | |
newtri[1] = new Triangle(a,tri.b,b); | |
newtri[2] = new Triangle(c,b,tri.c); | |
iter--; | |
for (Triangle t: newtri) { | |
drawit(iter,t,out); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment