对角线延伸一个值

本文关键字:一个 对角线 | 更新日期: 2023-09-27 18:33:24

我有一行让我们说

X1="184.357" Y1="-39.3242"
X2="244.261" Y2="-30.96551" 

我希望将端点延长 5。

  • 如何按指定值实现线的延伸?

  • 我创建了一个计算方法,但我不确定它是否正确。

谁能帮我纠正这个问题?

我创建的方法:

/// <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));
}

对角线延伸一个值

为了扩展一条线,你需要沿着向量转换一个端点。下面是 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;
    }
}

Point3DVector3DMatrix3D来自System.Windows.Media.Media3D

还有一个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];
        }
    }        
}