Search code examples
c#algorithmmathcenter

find the center point of coordinate 2d array c#


Is there a formula to average all the x, y coordinates and find the location in the dead center of them.

I have 100x100 squares and inside them are large clumps of 1x1 red and black points, I want to determine out of the red points which one is in the middle.

I looked into line of best fit formulas but I am not sure if this is what I need.

Sometimes all the red will be on one side, or the other side. I want to essentially draw a line then find the center point of that line, or just find the center point of the red squares only. based on the 100x100 grid.


Solution

  • List<Point> dots = new List<Point>();
    int totalX = 0, totalY = 0;
    foreach (Point p in dots)
    {
        totalX += p.X;
        totalY += p.Y;
    }
    int centerX = totalX / dots.Count;
    int centerY = totalY / dots.Count;