diff --git a/stuff/tree/Point.pde b/stuff/tree/Point.pde new file mode 100644 index 0000000..672655b --- /dev/null +++ b/stuff/tree/Point.pde @@ -0,0 +1,53 @@ +class Point { + float _r = 30; + float _x; + float _y; + boolean _isSelected; + + Point(float x0, float y0) { + _x = x0; + _y = y0; + _isSelected = false; + } + + void draw() { + pushStyle(); + //stroke(c_pointStroke); + //fill((_isSelected) ? c_pointSelect : c_pointDefault); + ellipse(_x, _y, _r, _r); + popStyle(); + } + + float dist2(Point q){ + return (q._x - _x)*(q._x - _x) + (q._y - _y)*(q._y - _y); + } + + boolean isSelectedPoint(float x, float y) { + return (x - _x)*(x - _x) + (y - _y) * (y - _y) < _r * _r; + } + + boolean isSelectedCir(float x, float y, float r) { + return (x - _x)*(x - _x) + (y - _y)*(y - _y) < r * r; + } + + //boolean isSelectedRect(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) { + // int l0 = lineSideTest(x1, y1, x2, y2, _x, _y); + // int l1 = lineSideTest(x2, y2, x3, y3, _x, _y); + // int l2 = lineSideTest(x3, y3, x4, y4, _x, _y); + // int l3 = lineSideTest(x4, y4, x1, y1, _x, _y); + + // return (l0 == l1) && (l1 == l2) && (l2 == l3); + //} + + void setSelected() { + _isSelected = true; + } + + void setUnSelected() { + _isSelected = false; + } + + void toggleSelected() { + _isSelected = !_isSelected; + } +} \ No newline at end of file diff --git a/stuff/tree/main.pde b/stuff/tree/main.pde index bd1711a..3e6557f 100644 --- a/stuff/tree/main.pde +++ b/stuff/tree/main.pde @@ -1,18 +1,39 @@ +import java.util.Random; -kd_tree t; +KdTree t; void settings() { size(512, 512); - t = new kd_tree(new Point(256,256), false); + t = new KdTree(new Point(256,256), false, 0); } void mouseReleased() { Point pt = new Point(mouseX, mouseY); - ellipse(pt.x,pt.y,5,5); + ellipse(pt._x,pt._y,5,5); t.insert(pt); + background(123); + t.draw(); } void draw() { - // println(t.left); - //println(t.right); + if(keyPressed) + { + if(key=='a') + { + Point ktest = new Point(mouseX, mouseY); + KdTree nn = t.search(ktest); + ellipse(nn._loc._x, nn._loc._y, 10, 10); + } + } + if(mousePressed) + { + Random rand = new Random(); + int x = rand.nextInt(512); + int y = rand.nextInt(512); + Point pt = new Point(x, y); + ellipse(pt._x,pt._y,5,5); + t.insert(pt); + background(123); + t.draw(); + } } \ No newline at end of file diff --git a/stuff/tree/tree.pde b/stuff/tree/tree.pde index c4ecb2d..bab6253 100644 --- a/stuff/tree/tree.pde +++ b/stuff/tree/tree.pde @@ -1,94 +1,114 @@ -class Point -{ - float x,y; - public Point(float _x, float _y) - { - x = _x; - y = _y; - } +class Abbv{ } +class KdTree { + KdTree _left, _right; + Point _loc; + int _depth; + color _c; + boolean _dir; // Vertical->True, Horizontal->False -class kd_tree -{ - Point loc; - kd_tree left, right; - private boolean dir;//vertical if true, horizontal if false - public kd_tree(Point p, boolean vert) - { - loc = p; - dir = vert; + public KdTree(Point p, boolean vert, int depth) { + _loc = p; + _dir = vert; + _depth = depth; + _c = colorize(depth); + _left = null; + _right = null; } - void insert(Point p) + KdTree nearest(Point p) { - //insert by cycling through dimensions - kd_tree parent = search(p); - //print(parent + " "); - if(parent.dir == true) - { - line(p.x, 0, p.x, 512); - if(parent.loc.x > p.x) - { - parent.left = new kd_tree(p, !parent.dir); - + float cd = _loc.dist2(p); + KdTree next = this; + if(_left != null && _left._loc.dist2(p) < cd){ + next = _left; + cd = _left._loc.dist2(p); + } + if(_right != null && _right._loc.dist2(p) < cd){ + next = _right; + cd = _right._loc.dist2(p); + } + println(this); + if(next == this) + return this; + else + return next.nearest(p); + } + void insert(Point p) { + KdTree parent = search(p); + if(parent._dir) { // If parent is vertical + if(parent._loc._x > p._x) { + parent._left = new KdTree(p, !parent._dir, parent._depth + 1); + } else { + parent._right = new KdTree(p, !parent._dir, parent._depth + 1); } - else - { - parent.right = new kd_tree(p, !parent.dir); + } else { + if(parent._loc._y > p._y) { + parent._left = new KdTree(p, !parent._dir, parent._depth + 1); + } else { + parent._right = new KdTree(p, !parent._dir, parent._depth + 1); } } - else - { - line(0, p.y, 512, p.y); - if(parent.loc.y < p.y) - { - parent.left = new kd_tree(p, !parent.dir); - } - else - { - parent.right = new kd_tree(p, !parent.dir); - } + } + + KdTree search(Point p) { + if(_dir) { // If vertical + if(p._x < _loc._x) { // If p is to the left of _loc + if(_left != null) { + return _left.search (p); + } else { + return this; + } + } else { // Then p is to the right of _loc + if(_right != null) { + return _right.search (p); + } else { + return this; + } + } + } else { // If Horizontal + if(p._y < _loc._y) { // If p is under _loc + if(_left != null) { + return _left.search(p); + } else { + return this; + } + } else { // Then p is over _loc + if(_right != null) { + return _right.search(p); + } else { + return this; + } + } } + } - line(p.x, p.y, parent.loc.x, parent.loc.y); + void draw() { + pushStyle(); + stroke(_c); - } - - kd_tree search(Point p) - { - //search for the cell containing this point - if(dir) - { - if(p.x < loc.x) + if (_left != null) { + line(_loc._x, _loc._y, _left._loc._x, _left._loc._y); + + pushStyle(); + if(_dir) { - if(left!=null) - return left.search(p); - else - return this; } else { - if(right!=null) - return right.search(p); - else - return this; } + popStyle(); + + _left.draw(); } - else - { - if(p.y < loc.y) - { - if(left!=null) - return left.search(p); - else - return this; - } - else - { - if(right!=null) - return right.search(p); - else - return this; - } + if (_right != null) { + line(_loc._x, _loc._y, _right._loc._x, _right._loc._y); + _right.draw(); } + popStyle(); + } + + color colorize (int depth) { + float d = depth * 0.2; + return color((int)255*cos(d) * cos(d), (int)255*abs(sin(d + PI)), (int)255*abs(sin(d+0.5))); } } \ No newline at end of file