Skip to content

Commit

Permalink
not much
Browse files Browse the repository at this point in the history
  • Loading branch information
interl0per committed Nov 29, 2015
1 parent a99f462 commit c450419
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 80 deletions.
53 changes: 53 additions & 0 deletions stuff/tree/Point.pde
Original file line number Diff line number Diff line change
@@ -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;
}
}
31 changes: 26 additions & 5 deletions stuff/tree/main.pde
Original file line number Diff line number Diff line change
@@ -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();
}
}
170 changes: 95 additions & 75 deletions stuff/tree/tree.pde
Original file line number Diff line number Diff line change
@@ -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)));
}
}

0 comments on commit c450419

Please sign in to comment.