Skip to content

Commit

Permalink
doneish
Browse files Browse the repository at this point in the history
  • Loading branch information
interl0per committed Dec 9, 2015
1 parent 4cebcb7 commit 5c8ac0b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 59 deletions.
5 changes: 0 additions & 5 deletions RangeApp/RangeApp.pde
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ void setup() {
drawTreeRelations = true;
drawTreeBoundaries = true;


BoundingBox tb = new BoundingBox(0,100, 0,100);
BoundingBox tb2 = new BoundingBox(50, 150, 50, 150);

println(contains(tb,tb2));
}

void draw() {
Expand Down
38 changes: 24 additions & 14 deletions RangeApp/Selector.pde
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,31 @@ class Selector{
if(_selecting){ //selecting with rect or circle
switch(_select_mode){
case 0:
Point p1 = cam.transform(new Point(_startX, _startY));
Point p2 = cam.transform(new Point(_endX+_startX, _endY+_startY));
// ellipse(p2._x, p2._y, 10, 10);
// println(p2._x + " " + p2._y);
//println(_endX + " " + _endY);
if(pot.getTree() != null){//prevents query of null/empty tree
ArrayList<Point> solution = pot.getTree().query(new BoundingBox(min(p1._x, p2._x), max(p1._x, p2._x), min(p1._y, p2._y), max(p1._y, p2._y)));
for(Point pp : solution)
{
ellipse(pp._x + width/2, pp._y + height/2, 30, 30);
Point p1 = cam.transform(new Point(_startX, _startY));
Point p2 = cam.transform(new Point(_endX+_startX, _endY+_startY));

if(pot.getTree() != null){//prevents query of null/empty tree
ArrayList<Point> solution = pot.getTree().queryBox(new BoundingBox(min(p1._x, p2._x), max(p1._x, p2._x), min(p1._y, p2._y), max(p1._y, p2._y)));
for(Point pp : solution)
{
ellipse(pp._x + width/2, pp._y + height/2, 30, 30);
}
println("size of list: " + solution.size());
}
break;
case 1:
Point pp1 = cam.transform(new Point(_startX, _startY));
Point pp2 = cam.transform(new Point(_endX+_startX, _endY+_startY));

if(pot.getTree() != null){//prevents query of null/empty tree
ArrayList<Point> solution = pot.getTree().queryCircle(pp1, dist(pp1._x, pp1._y, pp2._x, pp2._y));
for(Point pp : solution)
{
ellipse(pp._x + width/2, pp._y + height/2, 30, 30);
}
println("size of list: " + solution.size());
}
println("size of list: " + solution.size());
}
break;
case 1: //_tree.circleQuery(cam.transform(new Point(_a,_b)),
break;
}
_selecting = false;
_startX = -1;
Expand Down
6 changes: 3 additions & 3 deletions RangeApp/Tests.pde
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ int lineSideTest (float ax0, float ay0, float ax1, float ay1, float px, float py
}
}

boolean contains(BoundingBox a, BoundingBox b)
boolean containsBox(BoundingBox a, BoundingBox b)
{//does a contain b?
return b.x1 >= a.x1 && b.x2 <= a.x2 && b.y1 >= a.y1 && b.y2 <= a.y2;
}

boolean intersects(BoundingBox a, BoundingBox b)
boolean intersectsBox(BoundingBox a, BoundingBox b)
{
if(contains(a,b) || contains(b,a)) return true;
if(containsBox(a,b) || containsBox(b,a)) return true;
return ((a.x1 <= b.x2 && a.x1 >= b.x1 && a.y1 <= b.y2 && a.y2 >= b.y1) || (b.x1 <= a.x2 && b.x1 >= a.x1 && b.y1 <= a.y2 && b.y2 >= b.y1));
}

Expand Down
65 changes: 35 additions & 30 deletions RangeApp/Tree.pde
Original file line number Diff line number Diff line change
Expand Up @@ -82,60 +82,65 @@ class KdTree {
ArrayList<Point> report() {
ArrayList<Point> rightRes = new ArrayList<Point>();
ArrayList<Point> leftRes = new ArrayList<Point>();
leftRes.add(_loc);

if(isLeaf()) {
leftRes.add(_loc);
return leftRes;
}
if(_left != null)
leftRes = _left.report();
leftRes = merge(leftRes, _left.report());
if(_right != null)
rightRes = _right.report();
return merge(rightRes, leftRes);
}

ArrayList<Point> query(BoundingBox range) {
ArrayList<Point> queryBox(BoundingBox range) {
ArrayList<Point> rightRes = new ArrayList<Point>();
ArrayList<Point> leftRes = new ArrayList<Point>();

if(inBox(_loc, range)) {
if(inBox(_loc, range))
rightRes.add(_loc);
}

if(_left != null) {
if(contains(range, _left._bb)) {
if(containsBox(range, _left._bb))
leftRes = _left.report();
}
else if(intersects(_left._bb, range)) {
leftRes = _left.query(range);
}

else if(intersectsBox(_left._bb, range))
leftRes = _left.queryBox(range);
}

if(_right != null) {
if(contains(range, _right._bb)) {
if(containsBox(range, _right._bb))
rightRes = merge(_right.report(), rightRes);
}
else if(intersects(_right._bb, range)) {
rightRes = merge(_right.query(range), rightRes);
}

else if(intersectsBox(_right._bb, range))
rightRes = merge(_right.queryBox(range), rightRes);
}
return merge(rightRes, leftRes);
}

boolean isLeaf() {
return _left==null && _right==null;
}
void dbg(BoundingBox q)
{
if(inBox(_loc, q))
println(this);
if(_left!=null)
{
_left.dbg(q);
ArrayList<Point> queryCircle(Point c, float r) {
ArrayList<Point> rightRes = new ArrayList<Point>();
ArrayList<Point> leftRes = new ArrayList<Point>();

if(inCircle(c, r, _loc))
rightRes.add(_loc);

if(_left != null) {
if(circleContainsBox(_left._bb, c._x, c._y, r))
leftRes = _left.report();

else if(rectCircleIntersect(_left._bb, c, r))
leftRes = _left.queryCircle(c, r);
}
if(_right != null)
_right.dbg(q);

if(_right != null) {
if(circleContainsBox(_right._bb, c._x, c._y, r))
rightRes = merge(_right.report(), rightRes);

else if(rectCircleIntersect(_right._bb, c, r))
rightRes = merge(_right.queryCircle(c, r), rightRes);
}
return merge(rightRes, leftRes);
}

void drawRelations() {
pushStyle();
strokeWeight(i_relationWidth);
Expand Down
16 changes: 9 additions & 7 deletions RangeApp/circleTests.pde
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ boolean rectCircleIntersect(BoundingBox bb, Point p, float r){
if(lineIntersectsCircle(bb.x1, bb.y1, bb.x2, bb.y1, p._x, p._y, r)){ return true; }//bottom: left->right
else{ return false; }
}

boolean inCircle(Point p, float r, Point q)
{
return dist(p._x, p._y, q._x, q._y) <= r;
}
boolean circleContainsBox(BoundingBox bb, float cx, float cy, float r){//not currently using this for anything but I wrote it up and didn't know
if(dist(bb.x1,bb.y1, cx, cy) <= r && dist(bb.x1,bb.y2, cx, cy)<=r && dist(bb.x2,bb.y1, cx, cy)<= r && dist(bb.x2,bb.y2, cx, cy) <= r){ return true; }
else{ return false;}
}

boolean boxContainsCircle(BoundingBox bb, float cx, float cy, float r){
float list[] = new float[4];
list[1] = dist(cx,cy,bb.x1,cy);
list[2] = dist(cx,cy,bb.x2,cy);
list[3] = dist(cx,cy,cx,bb.y2);
list[4] = dist(cx,cy,cx,bb.y1);
list[0] = dist(cx,cy,bb.x1,cy);
list[1] = dist(cx,cy,bb.x2,cy);
list[2] = dist(cx,cy,cx,bb.y2);
list[3] = dist(cx,cy,cx,bb.y1);
if(min(list) <= r) return true;
else return false;
}
Expand Down Expand Up @@ -70,5 +73,4 @@ boolean t1 = lineIntersectsCircle(2.0, 1.0 , 5.0, 1.0, 3.5, 3, 2);//horizont
if(t1){println("True");}
else{println("false");}
*/

*/

0 comments on commit 5c8ac0b

Please sign in to comment.