public static bool CircleLineIntersection(Circle2D c, Line2D l) { var a1 = new Vector2D(c.Point.X - c.Radius, c.Point.Y - c.Radius); var a2 = new Vector2D(c.Point.X + c.Radius, c.Point.Y + c.Radius); if (BoundingBoxIntersection(a1, a2, l.A, l.B)) { var slope = l.Slope; double inverseSlope; if (slope == null || (double) slope == 0) { // the bounding box already determined that an intersection has been made. return true; } // slightly more complicated... need to create a tangent line. and find the distance. inverseSlope = 1 / (double)slope; // y= mx+b // b = circle.y, x = circle .x Line2D l2 = new Line2D(); l2.A = new Vector2D(c.Point.X, c.Point.Y); l2.B.X = c.Point.X + c.Radius; l2.B.Y = c.Point.Y + inverseSlope * (c.Point.X + c.Radius); var p = LineLineIntersection(l, l2); if (p != null) { var d = new Line2D(c.Point, p); if (d.Length < c.Radius) return true; return false; } Line2D l3 = new Line2D(); l2.A = new Vector2D(c.Point.X, c.Point.Y); l2.B.X = c.Point.X - c.Radius; l2.B.Y = c.Point.Y + inverseSlope * (c.Point.X - c.Radius); var q = LineLineIntersection(l, l2); if (q != null) { var d = new Line2D(c.Point, q); if (d.Length < c.Radius) return true; return false; } } return false; }
public static bool CircleCircleIntersection(Circle2D a, Circle2D b) { var l = new Line2D(a.Point, b.Point); return l.Length < (a.Radius + b.Radius); }
public static bool CircleContainsVertex(Circle2D a, Vector2D b) { var l = new Line2D(a.Point, b); return l.Length < a.Radius; }
// private static int FindLineCircleIntersections(double cx, double cy, double radius, //Vector2D point1, Vector2D point2) // { // double dx, dy, A, B, C, det; // dx = point2.X - point1.X; // dy = point2.Y - point1.Y; // A = dx * dx + dy * dy; // B = 2 * (dx * (point1.X - cx) + dy * (point1.Y - cy)); // C = (point1.X - cx) * (point1.X - cx) + (point1.Y - cy) * (point1.Y - cy) - radius * radius; // det = B * B - 4 * A * C; // if ((A <= 0.0000001) || (det < 0)) // { // return 0; // } // else if (det == 0) // { // return 1; // } // else // { // return 2; // } // } // public static bool CirclePolygonIntersecton(Circle2D c, Polygon2D p) // { // foreach (var l in p.Lines) // { // var count = FindLineCircleIntersections(c.Point.X, c.Point.Y, c.Radius, l.A, l.B); // if (count == 2) // return true; // else if (count == 1 && IsPointInPolygon(p, c.Point)) // return true; // } // if (IsPointInPolygon(p, c.Point)) // return true; // return false; // } public static bool CirclePolygonIntersection(Circle2D c, Polygon2D p) { if (IsPointInPolygon(p, c.Point)) return true; foreach (var l in p.Lines) { if (CircleLineIntersection(c, l)) return true; } return false; }