Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
May i know how to write to get answer the following question.
C#
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# 

}

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.
Posted
Updated 10-May-12 10:17am
v3
Comments
OriginalGriff 10-May-12 1:37am    
Depends what you mean by "colse".
Even if you mean "close", that is a subjective comparison.
Is 1 "close" to 2? Yes, if your range is 1 - 100. No, if your range is 1-2
You need to define your problem better!
Remember that we can't see your screen, access your HDD, read your homework instructions, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
rosyBurmese 10-May-12 1:50am    
I mean that
Point A is nearer Point B than other point and
Point B is nearer Point A than other point and
Point F is nearer Point B than other point so on


i want to know how to calculate in c#.net
Sandeep Mewara 10-May-12 2:09am    
Did you try anything till now? Sounds like a homework question - what effort have you made?

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.
 
Share this answer
 
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));
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900