I generating square grid with inscribed circle. values in circle denoted by 1 and those of square by 0.
import numpy as np
from typing import List, Tuple
def collect(x: int, y: int, sigma: float =3.0) -> List[Tuple[int, int]]:
""" create a small collection of points in a neighborhood of some point
"""
neighborhood = []
X = int(sigma)
for i in range(-X, X 1):
Y = int(pow(sigma * sigma - i * i, 1/2))
for j in range(-Y, Y 1):
neighborhood.append((x i, y j))
return neighborhood
def plotter(sigma: float =3.0) -> None:
""" Plot a binary grid """
arr = np.zeros([sigma * 2 1] * 2)
points = collect(int(sigma), int(sigma), sigma)
# flip pixel value if it lies inside (or on) the circle
for p in points:
R, C = p
arr[R][C] = 1
print(arr)
grid = plotter(10)
print(grid)
print(grid.shape)
The improvement I wanted to do here: Reference to each element to the grid is through grid[R][C], where R is the row and C is the column. How can I convert references of R and C to a cartesian system? For the above case, it should start at (-10,10), where the center point will be (0,0).
what logic I should use to get desired results. and most important when I will call ex. grid[-2][3] it should able to refer the location and get value at that point i.e. 0 or 1.
for x in range(len(grid)):
for y in range(len(grid[x])):
if (x > len(grid)/2 and y < len(grid)/2):
print ("lies in First quadrant")
elif (x < len(grid)/2 and y >len(grid)/20):
print ("lies in Second quadrant")
elif (x < len(grid)/2 and y > len(grid)/2):
print ("lies in Third quadrant")
elif (x > len(grid)/2 0 and y < len(grid)/2):
print ("lies in Fourth quadrant")
elif (x == len(grid)//2 and y < len(grid)/2):
print ("lies at positive y axis")
elif (x == len(grid)//2 and y < len(grid)/2):
print ("lies at negative y axis")
elif (y == len(grid)//2 and x < len(grid)/2):
print ("lies at negative x axis")
elif (y == len(grid)//2 and x > len(grid)/2):
print ("lies at positive x axis")
elif((x == len(grid)//2 and y == len(grid)/2):
print ("lies at origin")
CodePudding user response:
You can cycle through the grid as follows:
for i in range(len(grid)):
for j in range(len(grid[i])):
In this loop, i will cycle through all the rows and j all the points in the row i. Using an array as a grid, the top left will always by (0, 0) and the bottom right (m, m) where m is the dimension of the square grid. Therefore to convert to cartesian coordinates, you would add on what you want the top left corner to have the value of. For the example (-10, 10), we would get:
(x, y) = (i, j) (-10, 10)
By the looks of it, you want the y to decrease going down, so you can change the sign of j:
(x, y) = (i, -j) (-10, 10)
All of this is relative so it depends on which corner you are imagining is (0, 0), which direction you want x and y to increase in, whether the first layer of your grid represents the rows or the columns, etc. Hope this helps
CodePudding user response:
In reference to the second part of the question, you can simplify this greatly:
for i in range(len(grid)):
for j in range(len(grid[i])):
x = i - 10
y = j 10
This will give you the correct values of x and y in your cartesian coordinate system, so it can be compared against 0 rather than having to calculate the length every time. The rest of the code is valid but may be simpler as follows:
if x > 0:
if y > 0:
print("Quadrant 1")
elif y < 0:
print("Quadrant 4")
else:
print("positive x-axis")
elif x < 0:
if y > 0:
print("Quadrant 2")
elif y < 0:
print("Quadrant 3")
else:
print("negative x-axis")
else:
if y == 0:
print("Origin")
elif y > 0:
print("positive y-axis")
else:
print("negative y-axis")