Example #1
0
        static CurveIntersectionInfo GetIntersectionInfoFromCurves(Curve c1, Curve c2)
        {
            // Utility function to test for intersection from convex polygon
            bool StrictlyInsideConvexPolygon(Double2[] poly, Double2 pt)
            {
                var length = poly.Length;

                for (int i = 0; i < length; i++)
                {
                    var ik = (i + 1) % length;
                    if (DoubleUtils.RoughlyEquals(poly[i], poly[ik]))
                    {
                        continue;
                    }
                    if ((poly[ik] - poly[i]).Cross(pt - poly[i]) <= 0)
                    {
                        return(false);
                    }
                }
                return(true);
            }

            // Utility function to test for intersection with curve
            bool CurveIntersects(Double2[] poly, Curve curve)
            {
                var length = poly.Length;

                for (int i = 0; i < length; i++)
                {
                    var ik = (i + 1) % length;
                    if (DoubleUtils.RoughlyEquals(poly[i], poly[ik]))
                    {
                        continue;
                    }
                    if (curve.IntersectionsWithSegment(poly[i], poly[ik]).Any(t => t >= 0 && t <= 1))
                    {
                        return(true);
                    }
                }
                return(false);
            }

            // Get the curves' enclosing polygons
            var p1 = GeometricUtils.EnsureCounterclockwise(c1.EnclosingPolygon);
            var p2 = GeometricUtils.EnsureCounterclockwise(c2.EnclosingPolygon);

            // If there is no intersection, no info
            if (!GeometricUtils.PolygonsOverlap(p1, p2, true))
            {
                return(null);
            }

            return(new CurveIntersectionInfo
            {
                InteriorPoints1 = p2.Where(p => StrictlyInsideConvexPolygon(p1, p)).ToArray(),
                InteriorPoints2 = p1.Where(p => StrictlyInsideConvexPolygon(p2, p)).ToArray(),
                CurveIntersects1 = CurveIntersects(p2, c1),
                CurveIntersects2 = CurveIntersects(p1, c2)
            });
        }