コード例 #1
0
        /// <summary>
        /// Get the <see cref="PdfPoint"/> that is the intersection of two lines.
        /// </summary>
        public static PdfPoint?Intersect(this PdfLine line, PdfLine other)
        {
            // if the bounding boxes do not intersect, the lines cannot intersect
            if (!line.GetBoundingRectangle().IntersectsWith(other.GetBoundingRectangle()))
            {
                return(null);
            }

            var eq1 = GetSlopeIntercept(line.Point1, line.Point2);
            var eq2 = GetSlopeIntercept(other.Point1, other.Point2);

            if (double.IsNaN(eq1.Slope) && double.IsNaN(eq2.Slope))
            {
                return(null);                                                    // both lines are vertical (hence parallel)
            }
            if (eq1.Slope == eq2.Slope)
            {
                return(null);                        // both lines are parallel
            }
            var intersection = new PdfPoint();

            if (double.IsNaN(eq1.Slope))
            {
                var x = eq1.Intercept;
                var y = eq2.Slope * x + eq2.Intercept;
                intersection = new PdfPoint(x, y);
            }
            else if (double.IsNaN(eq2.Slope))
            {
                var x = eq2.Intercept;
                var y = eq1.Slope * x + eq1.Intercept;
                intersection = new PdfPoint(x, y);
            }
            else
            {
                var x = (eq2.Intercept - eq1.Intercept) / (eq1.Slope - eq2.Slope);
                var y = eq1.Slope * x + eq1.Intercept;
                intersection = new PdfPoint(x, y);
            }

            // check if the intersection point belongs to both segments
            // (for the moment we only know it belongs to both lines)
            if (!line.Contains(intersection))
            {
                return(null);
            }
            if (!other.Contains(intersection))
            {
                return(null);
            }
            return(intersection);
        }
コード例 #2
0
        /// <summary>
        /// Get the t values that are the intersections of the line and the curve.
        /// </summary>
        /// <returns>List of t values where the <see cref="BezierCurve"/> and the <see cref="PdfLine"/> intersect.</returns>
        public static double[] FindIntersectionT(this BezierCurve bezierCurve, PdfLine line)
        {
            // if the bounding boxes do not intersect, they cannot intersect
            var bezierBbox = bezierCurve.GetBoundingRectangle();

            if (!bezierBbox.HasValue)
            {
                return(null);
            }
            var lineBbox = line.GetBoundingRectangle();

            if (!bezierBbox.Value.IntersectsWith(lineBbox))
            {
                return(null);
            }

            double x1 = (double)line.Point1.X;
            double y1 = (double)line.Point1.Y;
            double x2 = (double)line.Point2.X;
            double y2 = (double)line.Point2.Y;

            return(FindIntersectionT(bezierCurve, x1, y1, x2, y2));
        }