/// <summary> /// Rotates the currrent point around another point at a given angle from the origin. /// </summary> /// <param name="angle">The angle from the origin.</param> /// <param name="point">The point about which to rotate.</param> public void RotatePoint(double angle, DoublePoint point) { // Convert angle to radians double radianAngle = MathUtils.toRadians(angle); // Decrement this point by the given point about which to rotate this -= point; // Copy off the X/Y position values double tempX = X; double tempY = Y; // Perform the actual rotation X = Math.Cos(radianAngle)* tempX - Math.Sin(radianAngle)* tempY; Y = Math.Sin(radianAngle)* tempX + Math.Cos(radianAngle)* tempY; // Add the coordinates that had been offset back in this += point; }
/// <summary> /// Construct line segment with the specified start and end points. /// </summary> /// <param name="startPoint">The start point.</param> /// <param name="endPoint">The end point.</param> public DoubleLine(DoublePoint startPoint, DoublePoint endPoint) { Start = startPoint; End = endPoint; }
/// <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> /// Calculate Manhattan distance between a point and x and y coordinates in two-dimensional space. /// </summary> /// <param name="a">The point in two-dimensional space.</param> /// <param name="x">The x-coordinate in two-dimensiona space.</param> /// <param name="y">The y-coordinate in two-dimensiona space.</param> /// <returns>The manhattan distance.</returns> public static double CalculateManhattanDistance(DoublePoint a, int x, int y) { var xDelta = Math.Abs(a.X - x); var yDelta = Math.Abs(a.Y - y); return xDelta + yDelta; }
/// <summary> /// Calculate Manhattan distance between two points in two-dimensional space. /// </summary> /// <param name="a">The first point in two-dimensional space.</param> /// <param name="b">The second point in two-dimensional space.</param> /// <returns>The manhattan distance.</returns> public static double CalculateManhattanDistance(DoublePoint a, DoublePoint b) { var xDelta = Math.Abs(a.X - b.X); var yDelta = Math.Abs(a.Y - b.Y); return xDelta + yDelta; }
/// <summary> /// Calculate Euclidean distance between two points. /// <param name="a">The point in two-dimensional space.</param> /// <param name="x">The x-coordinate in two-dimensiona space.</param> /// <param name="y">The y-coordinate in two-dimensiona space.</param> /// <returns>The Euclidean distance.</returns> /// </summary> public static double CalculateEuclideanDistance(DoublePoint a, int x, int y) { var xDelta = (a.X - x); var yDelta = (a.Y - y); return Math.Sqrt(xDelta*xDelta + yDelta*yDelta); }
/// <summary> /// Calculate Euclidean distance between two points. /// </summary> /// <param name="a">The first point in two-dimensional space.</param> /// <param name="b">The second point in two-dimensional space.</param> /// <returns>The Euclidean distance.</returns> public static double CalculateEuclideanDistance(DoublePoint a, DoublePoint b) { var xDelta = (a.X - b.X); var yDelta = (a.Y - b.Y); return Math.Sqrt(xDelta*xDelta + yDelta*yDelta); }
/// <summary> /// Calculate the squared distance between two points. /// </summary> /// <param name="a">The first point in two-dimensional space.</param> /// <param name="b">The second point in two-dimensional space.</param> /// <returns>The squared distance.</returns> public static double CalculateSquaredDistance(DoublePoint a, DoublePoint b) { var xDelta = a.X - b.X; var yDelta = a.Y - b.Y; return xDelta*xDelta + yDelta*yDelta; }
#pragma warning restore 1591 #region Static Methods /// <summary> /// Calculates the angle that the given point makes with the origin in radians. /// </summary> /// <param name="a">The point whose position to compare with the origin.</param> /// <returns>The angle (in degrees) that the given point makes with the origin.</returns> public static double CalculateAngleFromOrigin(DoublePoint a) { // If we're both X and Y are zero, the point is at the origin, but consider // this to be 90 degrees if (a.X == 0) { if (a.Y == 0) { return 90; } // If only X is 0 but Y isn't, we still can't calculate the slope so // consider this to be 270 degrees return 270; } // Calculate the slope (this would just be Y/X since it's compared to the // origin) and take the arc tangent (which yields the angle in radians) var angle = MathUtils.toDegrees(Math.Atan(a.Y/a.X)); // If the X coordinate is positive, just return the calculated angle if (a.X > 0) return angle; // Otherwise, return the angle plus 180 degrees return angle + 180; }