询问图形距离 [英] asking about graph distance

查看:101
本文介绍了询问图形距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以知道怎么写才能回答以下问题.

May i know how to write to get answer the following question.

Dictionary<String, Point> a = new Dictionary<string, Point>();
a.Add("Point A", new Point(0, 0));
a.Add("Point B", new Point(5, 5));
a.Add("Point C", new Point(-5, 10));
a.Add("Point D", new Point(10, -5));
a.Add("Point E", new Point(-10, -10));
a.Add("Point F", new Point(5, 10));
MeasureDistance(a)
this dictionary to pass as pamater to the method

void MeasureDistance(Dictioary<string,point> b)
{

//May i know how to write code using c# 

}


我可以知道如何使用C#编写代码吗?
我想得到以下结果.

A点到B点.
B点逐渐指向A点.
C点到A点.
D点colse到A点.
点E结束点到点A.
F点指向B点.


May i know how to write code using c#
i want to get the following result.

Point A colse to the Point B.
Point B colse to the Point A.
Point C colse to the Point A.
Point D colse to the Point A.
Point E colse to the Point A.
Point F colse to the Point B.

推荐答案

您的问题背后的数学是大约2500年前的一位希腊老人所描述的.他的名字是Pythagoras.使用该名称,Google会迅速带您了解他的公式,该公式用于计算直角三角形中的距离".然后使用该公式计算字典中所有可能的距离,并遍历它们以获得每个点的最短距离.
The math behind your problem was described by an old Greek guy some 2500 years ago. His name was Pythagoras. Using that name, Google will quickly get you to his formula for calculating "distances" in a right-angled triangle. And then use that formula for all possible distances in your dictionary, and loop over them to get the shortest distances for each point.


首先,字典并不是您真正想要的.只需创建一个数组或列表,以及一个用于名称和指向的类:

First of all a dictionary is not really what you want. Just create an array or list, and a class for the name and point:

private class NamedPoint
{
  public string Name;
  public Point Point;
}

private void Calculate(IEnumerable<namedpoint> points)
{
  foreach (var pt1 in points)
  {
    NamedPoint ptClosest = null;
    double ptClosestDistance = double.PositiveInfinity;
    foreach (var pt2 in points)
    {
      if (pt1.Name == pt2.Name) break;
      if (GetDistance(pt1.Point, pt2.Point) < ptClosestDistance)
      {
        ptClosestDistance = GetDistance(pt1.Point, pt2.Point);
        ptClosest = pt2;
      }
      Console.WriteLine(string.Format("{1} is closest to {0}", pt1.Name, ptClosest.Name));
    }
  }
}

private double GetDistance(Point p1, Point p2)
{
  return Math.Sqrt((p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y));
}


这篇关于询问图形距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆