This project is a visualization of a computer programming concept: Recursion. An H Tree is a fractal pattern that also has practical use in the layout of circuits inside of computer processors.
Recursion is a strange concept at first to wrap your head around. It's the concept of calling on yourself, similar to a loop. If you've ever watched someone watching tv on tv, then you have sort of seen a form of recursion.
import edu.princeton.cs.algs4.StdDraw;
public class RecursiveGraphic {
public static int canvasSize = 256;
public static int size = canvasSize /2;
public static void main(String[]args)
{
StdDraw.setCanvasSize();
StdDraw.setScale(0,canvasSize);
StdDraw.setPenColor(0,0,0);
StdDraw.setPenRadius(.001);
htree(Integer.parseInt(args[0]), canvasSize/2, canvasSize/2, size);
}
public static void drawH(double x, double y, double size)
{
double newX = x -(size/2);
StdDraw.line(newX, y, newX + size, y);
StdDraw.line(newX, y-(size/2), newX, y+(size/2));
StdDraw.line(newX+size, y-(size/2), newX + size, y + (size/2));
StdDraw.show(020);
}
public static void htree(int n, double x, double y, double size)
{
if(n>1)
{
System.out.print(n + " ");
drawH(x, y, size);
htree(n-1, x-size/2, y+size/2, size/2);
htree(n-1, x-size/2, y-size/2, size/2);
htree(n-1, x + size/2, y + size/2, size/2);
htree(n-1, x + size/2, y - size/2, size/2);
}
if(n==1)
{
System.out.print(n + " ");
drawH(x, y, size);
}
}
}