a real ring

This commit is contained in:
2017-11-28 15:17:54 +01:00
parent 97f13a7eec
commit be14b27d69

View File

@@ -1,5 +1,8 @@
import peasy.PeasyCam; import peasy.PeasyCam;
static int RING_SIZE = 500;
static int RING_WIDTH = 120;
class Leaf { class Leaf {
PVector pos; PVector pos;
boolean reached = false; boolean reached = false;
@@ -69,7 +72,7 @@ class Tree {
Tree() { Tree() {
makeTorus(); makeTorus();
Branch root = new Branch(new PVector(300, 0), new PVector(0, -1)); Branch root = new Branch(new PVector(RING_SIZE, 0), new PVector(0, -1));
branches.add(root); branches.add(root);
Branch current = new Branch(root); Branch current = new Branch(root);
@@ -81,17 +84,20 @@ class Tree {
} }
void makeTorus() { void makeTorus() {
for (int i = 0; i < 200; i++) { for (int i = 0; i < 350; i++) {
for (int p = 0; p < 90; p++) { for (int p = 0; p < 90; p++) {
PVector pos = PVector.random3D(); PVector pos = PVector.random3D();
pos.mult(90); pos.x = 0;
pos.add(sin(i)*300, cos(i)*300); pos.y = 0;
pos.mult(RING_WIDTH);
pos.add(sin(i)*RING_SIZE, cos(i)*RING_SIZE);
leaves.add(new Leaf(pos)); leaves.add(new Leaf(pos));
} }
} }
} }
boolean closeEnough(Branch b) { boolean closeEnough(Branch b) {
int i = 0;
for (Leaf l : leaves) { for (Leaf l : leaves) {
float d = PVector.dist(b.pos, l.pos); float d = PVector.dist(b.pos, l.pos);
if (d < max_dist) return true; if (d < max_dist) return true;
@@ -101,7 +107,8 @@ class Tree {
void show() { void show() {
//for (Leaf l : leaves) l.show(); //for (Leaf l : leaves) l.show();
for (int i = 0; i < branches.size(); i++) { int size = branches.size();
for (int i = 0; i < size; i++) {
Branch b = branches.get(i); Branch b = branches.get(i);
if (b.parent != null) { if (b.parent != null) {
strokeWeight(2); strokeWeight(2);
@@ -158,42 +165,12 @@ class Tree {
} }
} }
} }
void fuse() {
ArrayList<Branch> nonparents = new ArrayList<Branch>();
for (Branch b : branches) {
boolean isnonparent = true;
for (Branch ib : branches) {
if (ib.parent != null && ib.parent.equals(b)) { isnonparent = false; break; }
}
if (isnonparent) nonparents.add(b);
}
for (Branch b : nonparents) {
Branch closest = null;
float record = -1;
for (Branch ib : nonparents) {
if (ib.equals(b)) continue;
PVector dir = PVector.sub(ib.pos, b.pos);
float d = dir.mag();
if (closest == null || d < record) {
closest = ib;
record = d;
}
}
if (closest != null) {
Branch n = new Branch(b);
n.pos = closest.pos.copy();
branches.add(n);
}
}
}
} }
Tree tree; Tree tree;
float min_dist = 5; float min_dist = 5;
float max_dist = 20; float max_dist = 20;
boolean fused = false;
PeasyCam cam; PeasyCam cam;
@@ -203,10 +180,24 @@ void setup() {
tree = new Tree(); tree = new Tree();
} }
void drawCircles() {
noFill();
strokeWeight(4);
pushMatrix();
translate(0, 0, RING_WIDTH);
ellipse(0, 0, RING_SIZE*2, RING_SIZE*2);
popMatrix();
pushMatrix();
translate(0, 0, -RING_WIDTH);
ellipse(0, 0, RING_SIZE*2, RING_SIZE*2);
popMatrix();
}
void draw() { void draw() {
background(255); background(255);
stroke(0); stroke(0);
tree.show(); tree.show();
if (tree.leaves.size() > 10) tree.grow(); tree.grow();
else if (!fused) { tree.fuse(); fused = true; } drawCircles();
} }