I have a line lets say
X1="184.357" Y1="-39.3242"
X2="244.261" Y2="-30.96551"
And I wish to extend the endpoint by 5.
How to achieve the extend of line by the value specified?
I have created a methods which calculates but I am not sure if it is correct.
Can anyone please help me correct this?
The method I created:
/// <summary>
/// Updates the end point of the line passed by the distance specified.
/// </summary>
/// <param name="line">The line of which the distance is to be updated.</param>
/// <param name="distance">The distance by which the end point is to be added.</param>
public static void ExtendLineEndPoint(this CustomLine line, double distance)
{
var angleOfLine = line.GetAngleOfLineInRadians();
var newPoint = line.EndPoint.GetPointFromDistance(angleOfLine, distance);
line.EndPoint = newPoint;
}
/// <summary>
/// Returns the angle of the line.
/// </summary>
/// <param name="line">The line of which the distance is to be updated.</param>
/// <returns>The angle in degrees of the line in radians.</returns>
public static double GetAngleOfLineInRadians(this CustomLine line)
{
double xDiff = line.EndPoint.X - line.StartPoint.X;
double yDiff = line.EndPoint.Y - line.StartPoint.Y;
return Math.Atan2(yDiff, xDiff);
}
/// <summary>
/// This method returns the point from the distance given along with the angle.
/// </summary>
/// <param name="point">The PointF object of which the new point is to be found.</param>
/// <param name="angle">The angle by which the point should be calculated. It must be in radians.</param>
/// <param name="distance">The distance by which the point should travel.</param>
/// <returns></returns>
public static PointF GetPointFromDistance(this PointF point, double angle, double distance)
{
var x = point.X + Math.Cos(angle) * distance;
var y = point.Y + Math.Sin(angle) * distance;
return new PointF(Convert.ToSingle(x), Convert.ToSingle(y));
}