用一个值延伸对角线

时间:2014-12-08 05:05:17

标签: c# geometry line diagonal

我有一句话就说

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

1 个答案:

答案 0 :(得分:1)

为了扩展一条线,您需要沿着矢量平移一个端点。这是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];
        }
    }        
}