-
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.
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
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 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; | ||
} | ||
|
||
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 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; } | ||
} | ||
else{// a2 == b2 horizontal line case | ||
if( a1<=p1 && p1<=b1 ){ return true; } | ||
else{ return false; } | ||
} | ||
} | ||
|
||
|
||
|