/// <summary> /// Get the <see cref="PdfPoint"/>s that are the intersections of the line and the curve. /// </summary> /// <returns></returns> public static PdfPoint[] Intersect(this PdfPath.BezierCurve bezierCurve, PdfPath.Line line) { var ts = FindIntersectionT(bezierCurve, line); if (ts.Count() == 0) { return(null); } List <PdfPoint> points = new List <PdfPoint>(); foreach (var t in ts) { PdfPoint point = new PdfPoint( PdfPath.BezierCurve.ValueWithT(bezierCurve.StartPoint.X, bezierCurve.FirstControlPoint.X, bezierCurve.SecondControlPoint.X, bezierCurve.EndPoint.X, t), PdfPath.BezierCurve.ValueWithT(bezierCurve.StartPoint.Y, bezierCurve.FirstControlPoint.Y, bezierCurve.SecondControlPoint.Y, bezierCurve.EndPoint.Y, t) ); points.Add(point); } return(points.ToArray()); }
/// <summary> /// Checks if both lines are parallel. /// </summary> public static bool ParallelTo(this PdfPath.Line line, PdfPath.Line other) { var val1 = (line.To.Y - line.From.Y) * (other.To.X - other.From.X); var val2 = (other.To.Y - other.From.Y) * (line.To.X - line.From.X); return(Math.Round(val1 - val2, 5) == 0); }
/// <summary> /// Whether the line segment contains the point. /// </summary> public static bool Contains(this PdfPath.Line line, PdfPoint point) { if (line.To.X == line.From.X) { if (point.X == line.To.X) { return(Math.Sign(point.Y - line.To.Y) != Math.Sign(point.Y - line.From.Y)); } return(false); } if (line.To.Y == line.From.Y) { if (point.Y == line.To.Y) { return(Math.Sign(point.X - line.To.X) != Math.Sign(point.X - line.From.X)); } return(false); } var tx = (point.X - line.From.X) / (line.To.X - line.From.X); var ty = (point.Y - line.From.Y) / (line.To.Y - line.From.Y); if (Math.Round(tx - ty, 5) != 0) { return(false); } return(tx >= 0 && tx <= 1); }
/// <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[] IntersectT(this PdfPath.BezierCurve bezierCurve, PdfPath.Line line) { return(IntersectT(bezierCurve, line.From, line.To)); }
/// <summary> /// Get the <see cref="PdfPoint"/>s that are the intersections of the line and the curve. /// </summary> public static PdfPoint[] Intersect(this PdfPath.BezierCurve bezierCurve, PdfPath.Line line) { return(Intersect(bezierCurve, line.From, line.To)); }
/// <summary> /// Checks if the curve and the line are intersecting. /// <para>Avoid using this method as it is not optimised. Use <see cref="Intersect(PdfPath.BezierCurve, PdfPath.Line)"/> instead.</para> /// </summary> public static bool IntersectsWith(this PdfPath.BezierCurve bezierCurve, PdfPath.Line line) { return(IntersectsWith(bezierCurve, line.From, line.To)); }
/// <summary> /// Checks if both lines are parallel. /// </summary> public static bool ParallelTo(this PdfPath.Line line, PdfLine other) { return(ParallelTo(line.From, line.To, other.Point1, other.Point2)); }
/// <summary> /// Get the <see cref="PdfPoint"/> that is the intersection of two lines. /// </summary> public static PdfPoint?Intersect(this PdfPath.Line line, PdfLine other) { return(Intersect(line.From, line.To, other.Point1, other.Point2)); }
/// <summary> /// Whether two lines intersect. /// </summary> public static bool IntersectsWith(this PdfPath.Line line, PdfLine other) { return(IntersectsWith(line.From, line.To, other.Point1, other.Point2)); }
/// <summary> /// Whether the line segment contains the point. /// </summary> public static bool Contains(this PdfPath.Line line, PdfPoint point) { return(Contains(line.From, line.To, point)); }
/// <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)); }
/// <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); }
/// <summary> /// Whether two lines intersect. /// </summary> public static bool IntersectsWith(this PdfPath.Line line, PdfPath.Line other) { return(Intersect(line, other) != null); }