private static Vector2 verticalIntersectionPoint(Line verticalLine, Slope nonVerticalSlope) { double x = verticalLine.P1.X; double y = nonVerticalSlope.Value * x + nonVerticalSlope.YIntercept; return(new Vector2((double)x, (double)y)); }
/// <summary> /// Checks if slopes are parallel. /// </summary> /// <param name="slopeA">First Slope.</param> /// <param name="slopeB">Second Slope.</param> /// <returns>True if the slopes are parallel.</returns> public static bool AreParallel(Slope slopeA, Slope slopeB) { if (!slopeA.HasValue && !slopeB.HasValue) { return(true); } if (slopeA.HasValue && slopeB.HasValue && slopeA.Value == slopeB.Value) { //lines are parallel return(true); } return(false); }
/// <summary> /// Check for slope equality. /// </summary> /// <param name="slopeA">First Slope.</param> /// <param name="slopeB">Second Slope.</param> /// <returns>Returns if the two slopes are equal.</returns> public static bool AreEqual(Slope slopeA, Slope slopeB) { if (!AreParallel(slopeA, slopeB)) { return(false); } if (!slopeA.HasValue && !slopeB.HasValue) { //lines are both vertical, see if x are the same return(slopeA.line.P1.X == slopeB.line.P1.X); } //lines are parallel, but not vertical, see if y-intercept is the same return(slopeA.YIntercept == slopeB.YIntercept); }
/** * Calculates the intersection of slopes of two lines. * * @param lineA First line to use for slope. * @param lineB Second line to use for slope. * @param options Optional IPathIntersectionOptions. * @returns point of intersection of the two slopes, or null if the slopes did not intersect. */ internal static Vector2?FromSlopeIntersection(Line lineA, Line lineB, out bool AreOverlapped, bool excludeTangents = false) { var slopeA = Slope.Of(lineA.P1, lineA.P2); var slopeB = Slope.Of(lineB.P1, lineB.P2); AreOverlapped = false; //see if slope are parallel if (Slope.AreParallel(slopeA, slopeB)) { if (Slope.AreEqual(slopeA, slopeB)) { //check for overlap AreOverlapped = Helper.IsLineOverlapping(lineA, lineB, excludeTangents); } return(null); } Vector2?pointOfIntersection = null; if (!slopeA.HasValue) { pointOfIntersection = verticalIntersectionPoint(lineA, slopeB); } else if (!slopeB.HasValue) { pointOfIntersection = verticalIntersectionPoint(lineB, slopeA); } else { // find intersection by line equation double x = (slopeB.YIntercept - slopeA.YIntercept) / (slopeA.Value - slopeB.Value); double y = slopeA.Value * x + slopeA.YIntercept; pointOfIntersection = new Vector2((double)x, (double)y); } return(pointOfIntersection); }