diff --git a/.DS_Store b/.DS_Store index 16ae1e1..40abc44 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/RangeApp/circleTests.pde b/RangeApp/circleTests.pde new file mode 100644 index 0000000..dc450e9 --- /dev/null +++ b/RangeApp/circleTests.pde @@ -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; } + } +} + + +