-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rect/circle intersection tests hopefully completed
- Loading branch information
Showing
1 changed file
with
66 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,74 @@ | ||
boolean rectCircleIntersect(BoundingBox bb, Point p, float r){ | ||
if( inBox(p, bb) ){ return true; } //circle center in rect | ||
//if( circleContainsBox(bb, p._x, p._y, r)){ return true; } | ||
if(dist(bb.x1,bb.y1, p._x, p._y) <= r || dist(bb.x1,bb.y2, p._x, p._y)<=r || dist(bb.x2,bb.y1, p._x, p._y)<= r || dist(bb.x2,bb.y2, p._x, p._y) <= r){ return true; } | ||
if(boxContainsCircle(bb, p._x, p._y, r)){ return true; } | ||
if(lineIntersectsCircle(bb.x1, bb.y1, bb.x1, bb.y2, p._x, p._y, r)){ return true; }//left:bottom->top | ||
if(lineIntersectsCircle(bb.x1, bb.y2, bb.x2, bb.y2, p._x, p._y, r)){ return true; }//top:left->right | ||
if(lineIntersectsCircle(bb.x2, bb.y1, bb.x2, bb.y2, p._x, p._y, r)){ return true; }//right:bottom->top | ||
if(lineIntersectsCircle(bb.x1, bb.y1, bb.x2, bb.y1, p._x, p._y, r)){ return true; }//bottom: left->right | ||
else{ return false; } | ||
} | ||
|
||
//import javax.vecmath.Vector2d; | ||
|
||
//boolean rectCircleIntersect(BoundingBox bb, Point p, float r){ | ||
// if( inBox(p, bb) ){ return true; } //circle center in rect | ||
// else if(lineIntersectsCircle (bb.x1, bb.y1, bb.x1, bb.y2, p._x, p._y, r)){ return true; }//left:bottom->top | ||
// else if(lineIntersectsCircle (bb.x1, bb.y2, bb.x2, bb.y2, p._x, p._y, r)){ return true; }//top:left->right | ||
// else if(lineIntersectsCircle (bb.x2, bb.y1, bb.x2, bb.y2, p._x, p._y, r)){ return true; }//right:bottom->top | ||
// else if(lineIntersectsCircle (bb.x1, bb.y1, bb.x2, bb.y1, p._x, p._y, r)){ return true; }//bottom: left->right | ||
// else{ return false; } | ||
//} | ||
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 lineIntersectsCircle(float a1, float a2, float b1, float b2, float c1, float c2, float r) { | ||
// Vector2d v = new Vector2d((double)b1-a1, (double)b2-a2); | ||
// Vector2d c = new Vector2d((double)c1-a1, (double)c2-a2); | ||
// double dpT = v.dot(c);//numerator of projection | ||
// double dpB = c.dot(c);//denominator of projection | ||
// double projScalar = dpT/dpB;//the | ||
// double orthX = c1-projScalar*(b1-a1); | ||
// double orthY = c2-projScalar*(b2-a2); | ||
// //if( !onLine(0) ){return true; | ||
// if( distance(orthX, orthY, (double)c1-a1, (double)c2-a2) < r ) return true; | ||
//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); | ||
if(min(list) <= r) return true; | ||
else return false; | ||
} | ||
|
||
double distance(double x1, double y1, double x2, double y2){ | ||
float t1 = (float)x2-(float)x1; | ||
float t2 = (float)y2-(float)y1; | ||
return sqrt(t1*t1+t2*t2); | ||
boolean lineIntersectsCircle(float a1, float a2, float b1, float b2, float c1, float c2, float r){ | ||
float dpTop = (b1-a1)*(c1-a1)+(b2-a2)*(c2-a2); | ||
float len = sqrt((b1-a1)*(b1-a1)+(b2-a2)*(b2-a2)); | ||
float dpBot = len*len; | ||
float projScalar = dpTop/dpBot; | ||
float orthX = projScalar*(b1-a1); | ||
float orthY = projScalar*(b2-a2); | ||
if(!onLine(0.0,0.0,b1-a1,b2-a2,orthX,orthY)){//a1-a1=0,b1-b1=0 | ||
return false; | ||
} | ||
else{ | ||
if(dist(orthX, orthY, c1-a1, c2-a2) <= r){ | ||
return true; | ||
} | ||
else{ | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
boolean onLine(double a1, double a2, double b1, double b2, double p1, double p2){//returns true if p is on segment ab | ||
if( a1 == b1 ){//vertical line case | ||
if( a2<=p2 && p2<=b2 ){ return true; } | ||
else { return false; } | ||
boolean onLine(float a1,float a2, float b1, float b2, float p1, float p2){//returns true if p is on segment ab | ||
if( a1==0 && b1==0 ){//vertical line case | ||
if( a2<=p2 && p2<=b2 ){ | ||
return true; | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
else{// a2 == b2 horizontal line case | ||
if( a1<=p1 && p1<=b1 ){ return true; } | ||
else{ return false; } | ||
if( a1<=p1 && p1<=b1 ){ | ||
return true; | ||
} | ||
else{ | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/* Useful test suite that can be put at the end of RangeApp constructor for testing | ||
boolean t1 = lineIntersectsCircle(2.0, 1.0 , 5.0, 1.0, 3.5, 3, 2);//horizontal case | ||
//boolean t1 = lineIntersectsCircle(1.0, 2.0 , 1.0, 5.0, 3.5, 3, 2.0);//vertical case | ||
if(t1){println("True");} | ||
else{println("false");} | ||
*/ | ||
|