Reputation: 547
Imagine we have set of V1= { p0(x0,y0), p1(x1,y1),p2(x2,y2),p3(x3,y3),p4(x4,y4)}
and set V2= { M0(x0,y0),.........Mn(xn,yn) }
The number of members in V1 is constant [ lets say 5 set of points }
Every time function minDifference() is called it should return set of points from V2 in which has the minimum difference with points in V1
in this example: output should return 5 set of points from V2 which has the least/minimum value difference with points in V1
Upvotes: 0
Views: 74
Reputation: 6317
Try this:
List<Point> V1 = new List<Point>();
V1.Add(new Point(2, 2));
V1.Add(new Point(4, 4));
V1.Add(new Point(6, 6));
V1.Add(new Point(8, 8));
V1.Add(new Point(10, 10));
List<Point> V2 = new List<Point>();
V2.Add(new Point(1, 1));
V2.Add(new Point(3, 3));
V2.Add(new Point(5, 5));
V2.Add(new Point(7, 7));
V2.Add(new Point(9, 9));
List<Point> result = new List<Point>();
foreach (Point p1 in V1)
{
Point minPoint = Point.Empty;
double minDist = double.MaxValue;
foreach (Point p2 in V2)
{
double dist = Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2));
if (dist < minDist)
{
minDist = dist;
minPoint = p2;
}
}
result.Add(minPoint);
}
As an extra optimization, you can drop the Math.Sqrt
since you don't really need the exact distance.
Upvotes: 1