Created
August 30, 2017 19:25
-
-
Save koreus7/037b7e3defe54601776f78ce0769971f to your computer and use it in GitHub Desktop.
Tree Fractal Thing
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.util.*; | |
import static java.lang.System.out; | |
public class TreeNode | |
{ | |
public List<TreeNode> children; | |
public TreeNode() | |
{ | |
children = new ArrayList<TreeNode>(); | |
} | |
} | |
public class Tree | |
{ | |
private TreeNode root; | |
public Tree(TreeNode root) | |
{ | |
this.root = root; | |
} | |
public TreeNode GetRoot() | |
{ | |
return this.root; | |
} | |
private void drawChildren(TreeNode node, float baseAngle, float seperationAngle, float startX, float startY, float radius, float scaleFactor, int currDepth) | |
{ | |
if(node.children != null && node.children.size() > 0) | |
{ | |
for(int i = 0; i < node.children.size(); i++) | |
{ | |
float angle = baseAngle + seperationAngle*i; | |
float x = startX + cos(angle)*radius; | |
float y = startY - sin(angle)*radius; | |
line(startX, startY, x, y); | |
drawChildren(node.children.get(i), angle - (seperationAngle*degree)/2.0, seperationAngle, x, y, radius*scaleFactor, scaleFactor, currDepth + 1); | |
} | |
} | |
} | |
public void Draw(float baseAngle, float seperationAngle, float startX, float startY, float radius, float scaleFactor) | |
{ | |
this.drawChildren(this.root, baseAngle, seperationAngle, startX, startY, radius, scaleFactor, 0); | |
} | |
} | |
Tree tree; | |
final int degree = 4; | |
final int depth = 4; | |
void setup() | |
{ | |
colorMode(HSB,1.0); | |
background(color(0,0,1)); | |
stroke(color(0,0,0)); | |
size(900,600); | |
tree = new Tree(new TreeNode()); | |
List<TreeNode> layer = new ArrayList<TreeNode>(); | |
List<TreeNode> newLayer = new ArrayList<TreeNode>(); | |
layer.add(tree.root); | |
for(int i = 0; i < depth; i++) | |
{ | |
for(TreeNode node: layer) | |
{ | |
for(int j = 0; j < degree; j++) | |
{ | |
TreeNode newNode = new TreeNode(); | |
node.children.add(newNode); | |
newLayer.add(newNode); | |
} | |
} | |
layer = newLayer; | |
newLayer = new ArrayList<TreeNode>(); | |
} | |
} | |
void draw() | |
{ | |
background(color(0,0,1)); | |
float timeMod = millis()/1000.0; | |
tree.Draw(0, TWO_PI*1.0/16.0 + timeMod, width/2, height/2, 100, 0.5); | |
tree.Draw(PI, TWO_PI*1.0/16.0 + timeMod, width/2, height/2, 100, 0.5); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment