Skip to content

Instantly share code, notes, and snippets.

@ceptreee
Last active August 25, 2020 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ceptreee/b217e0a2538621fe497f6cfcd480d9cc to your computer and use it in GitHub Desktop.
Save ceptreee/b217e0a2538621fe497f6cfcd480d9cc to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
def ellipse(ax,x,y,width,height,angle=0,color="k"):
e = patches.Ellipse(xy = (x,y), width = width, height = height,
angle = angle, color = color)
ax.add_patch(e)
def plot_rw():
for pos in r_pos:
ellipse(ax,pos[0],pos[1],2*pos[2],2*pos[3],color="r")
for pos in w_pos:
ellipse(ax,pos[0],pos[1],2*pos[2],2*pos[3],color="w")
def plot_b_default():
for pos in b_pos:
ellipse(ax,pos[0],pos[1],2*pos[2],2*pos[3],color="b")
def plot_b(cx,cy):
for i in range(N_b):
wx = w_pos[i][0]
wy = w_pos[i][1]
wθ = calc_θ(cx - wx,cy - wy)
bx = wx + br[i] * np.cos(wθ)
by = wy + br[i] * np.sin(wθ)
ellipse(ax,bx,by,2*b_pos[i][2],2*b_pos[i][3],color="b")
b_pos[i][0] = bx
b_pos[i][1] = by
return b_pos
def calc_θ(cx,cy):
return np.sign(cy)*np.arccos(cx/np.sqrt(cx**2+cy**2))
def calc_r(cx,cy):
return np.sqrt(cx**2+cy**2)
def set_axis():
plt.xlim([0,512])
plt.ylim([0,512])
plt.gca().invert_yaxis()
plt.axis("off")
def press(e):
global key
if key == "":
key = e.key
else:
key = ""
def motion(event):
global b_pos,key
if (event.xdata) is None or (event.ydata is None):
return
cx = event.xdata
cy = event.ydata
plt.cla()
plot_rw()
if key == "":
plot_b_default()
else:
b_pos = plot_b(cx,cy)
set_axis()
plt.draw()
# https://twitter.com/3socha/status/1298182210399244289
# https://twitter.com/3socha/status/1298182261964066817
r_pos = [[223, 70, 55, 55],
[336, 87, 70, 70],
[389,172, 87, 39],
[380,250, 52, 58],
[370,365, 77, 71],
[275,437, 65, 65],
[180,430, 50, 50],
[135,360, 35, 55],
[ 93,277, 62, 62],
[ 80,175, 53, 53],
[155,198, 48, 48],
[151,120, 48, 48]]
w_pos = [[354, 62,31,31],
[400,370,40,34],
[267,463,23,23],
[113,289,30,30],
[ 67,167,26,26]]
b_pos = [[364, 49, 14,14],
[418,362, 17,17],
[267,476, 9, 9],
[110,273, 13,13],
[ 53,162, 13,13]]
N_b = len(b_pos)
br = [0]*N_b
for i in range(N_b):
bx = b_pos[i][0]
by = b_pos[i][1]
wx = w_pos[i][0]
wy = w_pos[i][1]
br[i] = calc_r(bx-wx,by-wy)
key = ""
plt.close('all')
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(111)
set_axis()
plt.connect('motion_notify_event', motion)
plt.connect('key_press_event', press)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment