//test if circle collides with other circle //used in robot collision detection public bool Collide(Circle2D other) { var dx = other.p.x - p.x; var dy = other.p.y - p.y; dx *= dx; dy *= dy; var radSum = other.Radius + Radius; radSum *= radSum; return (dx + dy) < radSum; }
//calculate the nearest intersection of this line //with a circle -- if the line is interpreted as a ray //going from its first endpoint to the second public double nearest_intersection(Circle2D C, out bool found) { var dx = P2.x - P1.x; var dy = P2.y - P1.y; var px = P1.x - C.p.x; var py = P1.y - C.p.y; var a = dx*dx + dy*dy; var b = 2*px*dx + 2*py*dy; var c = px*px + py*py - C.Radius*C.Radius; var det = b*b - 4.0*a*c; if (det < 0.0) { found = false; return -1.0; } var sqrtDet = Math.Sqrt(det); var t1 = (-b + sqrtDet)/(2*a); var t2 = (-b - sqrtDet)/(2*a); found = false; var t = 0.0; if (t2 < 0) { if (t1 > 0) { found = true; t = t1; } } else { found = true; t = t2; } if (!found) return -1.0; return t*Math.Sqrt(dx*dx + dy*dy); }
public Circle2D(Circle2D other) { p = other.p; Radius = other.Radius; }