'Python3: print respective x and y coordinate of a NumPy array
Let's say I have a Python script like that
import numpy as np
positions = np.array([[2.5, 8], [3, 10], [0, 5], [1, 5]])
x = positions[:, 0]
y = positions[:, 1]
def isInside(circle_x, circle_y, rad, x, y):
return ((x - circle_x) ** 2 + (y - circle_y) ** 2) <= rad ** 2
circle_x = 0;
circle_y = 5;
rad = 2;
for is_inside in isInside(circle_x, circle_y, rad, x, y):
print ("Inside" if is_inside else "Outside")
I now want to print the respective x and y coordinates also for each individual "Inside" or "Outside". This should look like this
[2.5, 8] Outside, [3, 10] Outside, [0, 5] Inside, [1, 5] Inside
How can I do this? Can anyone help me out?
Solution 1:[1]
import numpy as np
positions = np.array([[2.5, 8], [3, 10], [0, 5], [1, 5]])
circle_centre = np.array((0, 5))
rad = 2
print(*(f"[{x}, {y}] {('Outside', 'Inside')[is_inside]}"
for x, y, is_inside in zip(*np.transpose(positions), ((positions - circle_centre) ** 2).sum(1) <= rad ** 2)),
sep=", ")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vovin |
