using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading;
public class Test
{
public class DataPoint {
public double X {get; set;}
public double Y {get; set;}
}
public static void Main()
{
int count = 39;
// generate our points
List<DataPoint>[] sets = new List<DataPoint>[count];
double[] result = new double[count];
for(var i=0; i<sets.Length; i++) {
sets[i] = GetRandomDataPoints(200);
}
// run our calculations async
int running = count;
using(ManualResetEvent resetEvent = new ManualResetEvent(false)){
for(int i=0; i<count; i++) {
int idx = i;
ThreadPool.QueueUserWorkItem(
new WaitCallback(o => {
result[idx] = GetAbsoluteMax(sets[idx]);
if (Interlocked.Decrement(ref running) == 0) {
resetEvent.Set();
}
}),
null
);
}
resetEvent.WaitOne();
}
foreach(var max in result) {
Console.WriteLine(max);
}
}
public static double GetAbsoluteMax(List<DataPoint> list) {
double max = 0;
// calc dist for each distinct pair Dist(P_1, P_2) == Dist(P_2, P_1)
for(var i=0; i<list.Count-1; i++) {
for(var j=i+1; j<list.Count; j++) {
var dX = list[i].X - list[j].X;
var dY = list[i].Y - list[j].Y;
// don't calculate the Square Root yet
var dist = dX * dX + dY * dY;
if(dist > max) {
max = dist;
}
}
}
return Math.Sqrt(max);
}
public static List<DataPoint> GetRandomDataPoints(int size) {
var result = new List<DataPoint>();
var rnd = new Random();
for(var i=0; i<size; i++) {
result.Add(new DataPoint() {
X=rnd.NextDouble()*100,
Y=rnd.NextDouble()*100
});
}
return result;
}
}