point_1: tuple[int, int], point_2: tuple[int, int]
) -> np.ndarray:
"""Return an array of coordinates of points that lie on the line between
two given points.
"""
x1, y1 = np.array(point_1).astype(int)
x2, y2 = np.array(point_2).astype(int)
# Calculate the slope of the line
if x1 == x2:
# Handle the special case where the line is vertical
x_coords: np.ndarray = np.full(abs(y2 - y1) + 1, x1)
y_coords: np.ndarray = np.arange(min(y1, y2), max(y1, y2) + 1)
else:
slope: float = (y2 - y1) / (x2 - x1)
intercept: float = y1 - slope * x1
# Calculate the coordinates of the points on the line
x_coords: np.ndarray = np.arange(min(x1, x2), max(x1, x2) + 1)
y_coords: np.ndarray = np.around(slope * x_coords + intercept).astype(
int
)
# Combine the x and y coordinates into a single array
points: np.ndarray = np.column_stack((x_coords, y_coords))
# Remove duplicate points
points: np.ndarray = np.unique(points, axis=0)
# Sort the points by distance from the first input point
distances: np.ndarray = np.linalg.norm(points - point_1, axis=1)
sorted_indices: np.ndarray = np.argsort(distances)
points: np.ndarray = points[sorted_indices]
return points.astype(int)
To post more than a few lines of code, use a pastebin like: * dpaste.org * linkode.org * bin.kv2.dev * hastebin.com If you are stuck in a terminal, you can use ix.io or paste.rs to paste from the CLI: <command to print output> |& curl -F 'f:1=<-' ix.io <command to print output> |& curl --data-binary @- https://paste.rs
Обсуждают сегодня