-
-
Save nmmmnu/5017c1bed9976139137f6b544e6e1052 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from polygenerator import random_polygon | |
import matplotlib.pyplot as plt | |
def is_inside(edges, xp, yp): | |
cnt = 0 | |
for edge in edges: | |
(x1, y1), (x2, y2) = edge | |
if (yp < y1) != (yp < y2) and xp < x1 + ((yp-y1)/(y2-y1))*(x2-x1): | |
cnt += 1 | |
return cnt%2 == 1 | |
def onclick(event): | |
xp, yp = event.xdata, event.ydata | |
if is_inside(edges, xp, yp): | |
print("inside") | |
plt.plot(xp, yp, "go", markersize=5) | |
else: | |
print("outside") | |
plt.plot(xp, yp, "ro", markersize=5) | |
plt.gcf().canvas.draw() | |
polygon = random_polygon(num_points=20) | |
polygon.append(polygon[0]) | |
edges = list(zip(polygon, polygon[1:] + polygon[:1])) | |
plt.figure(figsize=(10, 10)) | |
plt.gca().set_aspect("equal") | |
xs, ys = zip(*polygon) | |
plt.gcf().canvas.mpl_connect('button_press_event', onclick) | |
plt.plot(xs, ys, "b-", linewidth=0.8) | |
plt.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment