【问题标题】:How can I measure diagonal distance points?如何测量对角距离点?
【发布时间】:2011-04-18 16:15:27
【问题描述】:

我可以计算水平点和垂直点,但我不知道如何使用对角点计算距离。有人可以帮我解决这个问题吗?

这是我的水平和垂直测量的代码:

private float ComputeDistance(float point1, float point2) 
{
        float sol1 = point1 - point2;
        float sol2 = (float)Math.Abs(Math.Sqrt(sol1 * sol1));

        return sol2;
}

protected override void OnMouseMove(MouseEventArgs e)
    {

        _endPoint.X = e.X;
        _endPoint.Y = e.Y;

        if (ComputeDistance(_startPoint.X, _endPoint.X) <= 10)
        {
            str = ComputeDistance(_startPoint.Y, _endPoint.Y).ToString();
        }
        else
        {
            if (ComputeDistance(_startPoint.Y, _endPoint.Y) <= 10)
            {
                str = ComputeDistance(_startPoint.X, _endPoint.X).ToString();
            }
        }
    }

假设 _startPoint 已经设置。

在这张图片中,对角点显然是错误的。

【问题讨论】:

  • Math.Sqrt(sol1 * sol1) == Math.Abs​​(sol1)

标签: c# winforms gdi distance measurement


【解决方案1】:

你需要使用毕达哥拉斯定理。

d = Math.Sqrt(Math.Pow(end.x - start.x, 2) + Math.Pow(end.y - start.y, 2))

【讨论】:

  • 这个是毕达哥拉斯的,等我试试这个。
【解决方案2】:

我认为您正在寻找Euclidean distance 公式。

在数学中,欧几里得距离或欧几里得度量是用尺子测量的两点之间的“普通”距离,由毕达哥拉斯公式给出。

【讨论】:

  • +1 指出实际上有很多计算距离的方法,欧几里得距离可能是这里想要的。
【解决方案3】:

【讨论】:

    【解决方案4】:

    很久以后...我想补充一点,您可以使用 .NET 的一些内置功能:

    using System.Windows;
    
    Point p1 = new Point(x1, y1);
    Point p2 = new Point(x2, y2);
    Vector v = p1 - p2;
    double distance = v.Length;
    

    或者简单地说:

    static double Distance(double x1, double x2, double y1, double y2)
    {
        return (new Point(x1, y1) - new Point(x2, y2)).Length;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2014-10-19
      相关资源
      最近更新 更多