In a grid, lets say that there is a line segment. Here, one endpoint is in box 7 and the other in box 3. It passes through boxes 7, 8, 5, 6, and 3. For these boxes, I want the coords of the endpoints from the line that are in that box. For example, for box 7, it would be (6, 2) and (10, 8). For box 8, it would be (10, 8) and (12, 10). For box 5, it would be (12, 0) and (20, 18). Inputs would be coord of each endpoint. Here were my first steps, until my brain fried: In the code below, instead of each box having side length 10 like the diagram, mine is 256, and is a 2 x 2 grid (Hence why it is only 512 x 512)
def convert_slope_intercept(x, y, x2, y2):
# y = mx + b
rise = y2 - y
run = x2 - x
slope = rise / run
b = y2 - (slope * x2)
# slope, b, x_range, y_range
# y = mx + b
return [slope, b, (x, x2), (y, y2)]
plt.figure(figsize=(10, 10))
plt.imshow(img)
num = 0
for x in range(0, 512, 256):
for y in range(0, 512, 256):
if x_range[0] // 256 <= x // 256 <= x_range[1] // 256:
# y value at x + 256 is above current square
if slope * (x + 256) + b > y + 256:
right_y = y + 256
right_x = (right_y - b) / slope
# y value at x + 256 is below current square
if slope * (x + 256) + b < y:
right_y = y
right_x = (right_y - b) / slope
# if x <= x_range[1] <= x + 256:
if slope * x + b > y + 256:
left_y = y + 256
left_x = (left_y - b) / slope
if slope * x + b < y:
left_y = y
left_x = (left_y - b) / slope

