Skip to content

Commit

Permalink
added test stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
interl0per committed Nov 19, 2015
1 parent d93e888 commit 7b80f08
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
18 changes: 18 additions & 0 deletions stuff/tree/main.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

kd_tree t;
void settings()
{
size(512, 512);
t = new kd_tree(new Point(256,256), false);
}
void mouseReleased()
{
Point pt = new Point(mouseX, mouseY);
ellipse(pt.x,pt.y,5,5);
t.insert(pt);
}
void draw()
{
// println(t.left);
//println(t.right);
}
94 changes: 94 additions & 0 deletions stuff/tree/tree.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
class Point
{
float x,y;
public Point(float _x, float _y)
{
x = _x;
y = _y;
}
}

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;
}
void insert(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);

}
else
{
parent.right = new kd_tree(p, !parent.dir);
}
}
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);
}
}

line(p.x, p.y, parent.loc.x, parent.loc.y);

}

kd_tree search(Point p)
{
//search for the cell containing this point
if(dir)
{
if(p.x < loc.x)
{
if(left!=null)
return left.search(p);
else
return this;
}
else
{
if(right!=null)
return right.search(p);
else
return this;
}
}
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;
}
}
}
}

0 comments on commit 7b80f08

Please sign in to comment.