I hope you have a nice day, so here is my problem: I'm trying to find the middle point of most populated positions (X/Y) on a map but i'm stuck and i can't find a good and effective way to do it.
To find this position i've access to an collection of entities (and these entities have a Position property (which is a Vector2D) and map size
public readonly struct Vector2D
{
private static readonly double Sqrt = Math.Sqrt(2);
private static readonly Random Random = new Random();
public static Vector2D Zero { get; } = new Vector2D();
public int X { get; }
public int Y { get; }
public Vector2D(int x, int y)
{
X = x;
Y = y;
}
public Vector2D GetDistanceTo(Vector2D vector2D) => new Vector2D(Math.Abs(vector2D.X - X), Math.Abs(vector2D.Y - Y));
public int GetDistance(Vector2D destination)
{
int x = Math.Abs(X - destination.X);
int y = Math.Abs(Y - destination.Y);
int min = Math.Min(x, y);
int max = Math.Max(x, y);
return (int)(min * Sqrt + max - min);
}
public bool IsInRange(Vector2D position, int range)
{
int dx = Math.Abs(X - position.X);
int dy = Math.Abs(Y - position.Y);
return dx <= range && dy <= range && dx + dy <= range + range / 2;
}
public override string ToString() => $"{X}/{Y}";
}
That's the only thing i've tried
public Vector2D FindMostPopulatedPosition()
{
IEnumerable<Vector2D> positions = Entities.Select(x => x.Position);
return new Vector2D((int)positions.Average(x => x.X), (int)positions.Average(x => x.Y));
}
Here is some example images
Red dot: All entities
Blue dot: The position i'm looking for
Thanks for reading (and for your help)