Example #1
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="PdfPath.BezierCurve"/> and the <see cref="PdfPath.Line"/> intersect.</returns>
        public static double[] FindIntersectionT(this PdfPath.BezierCurve bezierCurve, PdfPath.Line 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 (!lineBbox.HasValue)
            {
                return(null);
            }

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

            double x1 = line.From.X;
            double y1 = line.From.Y;
            double x2 = line.To.X;
            double y2 = line.To.Y;

            return(FindIntersectionT(bezierCurve, x1, y1, x2, y2));
        }
Example #2
0
        /// <summary>
        /// Get the <see cref="PdfPoint"/> that is the intersection of two lines.
        /// </summary>
        public static PdfPoint?Intersect(this PdfPath.Line line, PdfPath.Line other)
        {
            // if the bounding boxes do not intersect, the lines cannot intersect
            var thisLineBbox = line.GetBoundingRectangle();

            if (!thisLineBbox.HasValue)
            {
                return(null);
            }

            var lineBbox = other.GetBoundingRectangle();

            if (!lineBbox.HasValue)
            {
                return(null);
            }

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

            var eq1 = GetSlopeIntercept(line.From, line.To);
            var eq2 = GetSlopeIntercept(other.From, other.To);

            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);
        }