2

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));
}
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
Pankaj Nikam
  • 665
  • 7
  • 22
  • Wouldn't it just make the line as big as the new distance, not the original distance + the new distance? – Hayden Dec 08 '14 at 05:46
  • @Hayden no it wont. I have tried that. It actually extends by the distance specified. – Pankaj Nikam Dec 08 '14 at 09:04
  • Your question is ill-defined. What '5' do you want to extend what? X2, Y2 or the length toward point P2?? – TaW Dec 08 '14 at 09:22

1 Answers1

3

In order to extend a line you need to translate an endpoint along a vector. Here's the formula and an example in WPF:

class Program
{
    static void Main(string[] args)
    {
        Point3D p1 = new Point3D(1, 1, 0.0);
        Point3D p2 = new Point3D(3, 3, 0.0);

        Vector3D v = p2 - p1;
        double offset = 5;
        Point3D p3 = TranslatePoint(p2, offset, v);
    }

    static Point3D TranslatePoint(Point3D point, double offset, Vector3D vector)
    {
        vector.Normalize();
        double _offset = offset / vector.Length;
        Vector3D _vector = vector * _offset;
        Matrix3D m = new Matrix3D();
        m.Translate(_vector);
        Point3D result = m.Transform(point);
        return result;
    }
}

Point3D, Vector3D and Matrix3D come from System.Windows.Media.Media3D

and one more example with System.Drawing:

class Program
{
    static void Main(string[] args)
    {
        {
            PointF p1 = new PointF(1f, 1f);
            PointF p2 = new PointF(3f, 3f);

            PointF v = new PointF()
            {
                X = p2.X - p1.X,
                Y = p2.Y - p1.Y
            };
            float offset = 5;
            PointF p3 = TranslatePoint(p2, offset, v);
        }           
    }

    static PointF TranslatePoint(PointF point, float offset, PointF vector)
    {
        float magnitude = (float)Math.Sqrt((vector.X * vector.X) + (vector.Y * vector.Y)); // = length
        vector.X /= magnitude;
        vector.Y /= magnitude;
        PointF translation = new PointF()
        {
            X = offset * vector.X,
            Y = offset * vector.Y
        };
        using (Matrix m = new Matrix())
        {
            m.Translate(translation.X, translation.Y);
            PointF[] pts = new PointF[] { point };
            m.TransformPoints(pts);
            return pts[0];
        }
    }        
}
t3chb0t
  • 16,340
  • 13
  • 78
  • 118