/// <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 int CalculateSquaredDistanceFromLineToPoint(IntLine line, IntPoint 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 = IntPoint.CalculateSquaredDistance(line.Start, line.End); double 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 = IntPoint.CalculateSquaredDistance(line.Start, point); var distanceToEnd = IntPoint.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 IntPoint(line.Start.X + (int)projection * (line.End.X - line.Start.X), line.Start.Y + (int)projection * (line.End.Y - line.Start.Y)); // Measure the distance from this point on the segment to the given point return(IntPoint.CalculateSquaredDistance(segmentPoint, point)); }
/// <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 IntPoint CalculateIntersection(IntLine a, IntLine 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)) { intersectionFound = true; return(new IntPoint(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 IntPoint(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 IntPoint CalculateMidpoint(IntLine 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 IntPoint(x, y)); }
/// <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(IntLine line) { return(IntPoint.CalculateEuclideanDistance(line.Start, line.End)); }
/// <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 int CalculateSquaredLineSegmentLength(IntLine line) { return(IntPoint.CalculateSquaredDistance(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(IntLine line, IntPoint point) { return(Math.Sqrt(CalculateSquaredDistanceFromLineToPoint(line, point))); }