Question

This isn't really a C# question, BUT I am doing this in C# and hopefully some individuals here can educate me on functions within the Math class, or help me find a good algorithm for this anyhow.

We'll use this example. I'm at coordinate 10, 10. I want to find all coordinates that are within a 7x7 grid, using 10, 10 as the center of it. Can anyone suggest a good algorithm, or use of the Math class to find all the coordinates I need?

Was it helpful?

Solution

This code make grid of '1' in the arr[15,15] with center at 10,10.

int x = 10, y = 10;

int lConerX = x - 4, lConerY = y - 4;//coords of top-left conner

for (int i = lConerX; i < lConerX + 7; i++)
{
    for (int j = lConerY; j < lConerY + 7; j++)
    {
         arr[i, j] = 1;
    }
}

OTHER TIPS

It seems you just need double cycle by X and Y coordinates pseudocode

X0=10
Y0=10
ASize = 7
HalfSize = ASize / 2
for Y = Y0 - HalfSize to Y0 + ASize - HalfSize do
  for X = X0 - HalfSize to X0 + ASize - HalfSize do
    output Y,X coordinates
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top