/// <summary> /// Calculates the squared length of the given line segment. /// </summary> /// <param name="line">The line segment to measure.</param> /// <returns>The squared length of the line segment.</returns> public static double CalculateSquaredLineSegmentLength(DoubleLine line) { return DoublePoint.CalculateSquaredDistance(line.Start, line.End); }
/// <summary> /// Calculates the length of the given line segment. /// </summary> /// <param name="line">The line segment to measure.</param> /// <returns>The length of the line segment.</returns> public static double CalculateLineSegmentLength(DoubleLine line) { return DoublePoint.CalculateEuclideanDistance(line.Start, line.End); }
/// <summary> /// Calculates the euclidean shortest distance between a line segment and point. /// </summary> /// <param name="line">The line segment involved in the calculation.</param> /// <param name="point">The point involved in the calculation.</param> /// <returns>The shortest euclidean distance between the given line segment and point.</returns> public static double CalculateEuclideanDistanceFromLineToPoint(DoubleLine line, DoublePoint point) { return Math.Sqrt(CalculateSquaredDistanceFromLineToPoint(line, point)); }
/// <summary> /// Calculates the squared shortest distance between a line segment and point. /// </summary> /// <param name="line">The line segment involved in the calculation.</param> /// <param name="point">The point involved in the calculation.</param> /// <returns>The shortest squared distance between the given line segment and point.</returns> public static double CalculateSquaredDistanceFromLineToPoint(DoubleLine line, DoublePoint point) { // Calculate the projection of the given point onto the given line var numerator = (point.X - line.Start.X)*(line.End.X - line.Start.X) + (point.Y - line.Start.Y)*(line.End.Y - line.Start.Y); var denominator = DoublePoint.CalculateSquaredDistance(line.Start, line.End); var projection = numerator/denominator; // If the projection is beyond the segment, return the distance to // either the start or end point on the line segment (whichever // happens to be the shortest) if (projection < 0 || projection > 1) { var distanceToStart = DoublePoint.CalculateSquaredDistance(line.Start, point); var distanceToEnd = DoublePoint.CalculateSquaredDistance(line.End, point); return distanceToStart < distanceToEnd ? distanceToStart : distanceToEnd; } // Create a point on the line segment from which to measure the distance to the given point var segmentPoint = new DoublePoint(line.Start.X + projection*(line.End.X - line.Start.X), line.Start.Y + projection*(line.End.Y - line.Start.Y)); // Measure the distance from this point on the segment to the given point return DoublePoint.CalculateSquaredDistance(segmentPoint, point); }
/// <summary> /// Calculates the closest point on the line segment to the given point. /// </summary> /// <param name="line">The line segment on which to find the closest point.</param> /// <param name="point">The source point.</param> /// <returns>The closest point on the line segment.</returns> public static DoublePoint CalculateLineSegmentClosestPoint(DoubleLine line, DoublePoint point) { // Calculate the projection of the given point onto the given line double numerator = (point.X - line.Start.X)*(line.End.X - line.Start.X) + (point.Y - line.Start.Y)*(line.End.Y - line.Start.Y); double denominator = DoublePoint.CalculateSquaredDistance(line.Start, line.End); double projection = numerator/denominator; // Return the intersection point on the line segment return new DoublePoint(line.Start.X + projection*(line.End.X - line.Start.X), line.Start.Y + projection*(line.End.Y - line.Start.Y)); }
/// <summary> /// Calculates the intersection between two line segments. /// </summary> /// <param name="a">The first line segment.</param> /// <param name="b">The segment line segment.</param> /// <param name="intersectionFound">Whether or not the lines intersect.</param> /// <returns>The point of intersection between the two given line segments.</returns> public static DoublePoint CalculateLineIntersection(DoubleLine a, DoubleLine b, out bool intersectionFound) { // Calculate the determinant's denominator var denominator = (a.Start.X - a.End.X)*(b.Start.Y - b.End.Y) - (a.Start.Y - a.End.Y)*(b.Start.X - b.End.X); if (denominator != 0) { // Calculate the determinants var xDeterminant = ((a.Start.X*a.End.Y - a.Start.Y*a.End.X)*(b.Start.X - b.End.X) - (a.Start.X - a.End.X)*(b.Start.X*b.End.Y - b.Start.Y*b.End.X))/denominator; var yDeterminant = ((a.Start.X*a.End.Y - a.Start.Y*a.End.X)*(b.Start.Y - b.End.Y) - (a.Start.Y - a.End.Y)*(b.Start.X*b.End.Y - b.Start.Y*b.End.X))/denominator; // Ensure that the intersection point actually lies within both line segments if (xDeterminant >= Math.Min(a.Start.X, a.End.X) && xDeterminant <= Math.Max(a.Start.X, a.End.X) && xDeterminant >= Math.Min(b.Start.X, b.End.X) && xDeterminant <= Math.Max(b.Start.X, b.End.X) && yDeterminant >= Math.Min(a.Start.Y, a.End.Y) && yDeterminant <= Math.Max(a.Start.Y, a.End.Y) && yDeterminant >= Math.Min(b.Start.Y, b.End.Y) && yDeterminant <= Math.Max(b.Start.Y, b.End.Y)) { intersectionFound = true; return new DoublePoint(xDeterminant, yDeterminant); } } // If the denominator came out to 0 or the point isn't within both // line segments, then the lines don't intersect intersectionFound = false; return new DoublePoint(0, 0); }
#pragma warning restore 1591 #region Static Methods /// <summary> /// Calculates the midpoint of the given line segment. /// </summary> /// <param name="line">The line segment of which to find the midpoint.</param> /// <returns>The midpoint of the given line segment.</returns> public static DoublePoint CalculateMidpoint(DoubleLine line) { // Half the sum of both the start and end X and Y coordinates var x = (line.Start.X + line.End.X)/2; var y = (line.Start.Y + line.End.Y)/2; // Return a new point based on those halved coordinates return new DoublePoint(x, y); }