-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw_utils.py
More file actions
36 lines (24 loc) · 1.02 KB
/
draw_utils.py
File metadata and controls
36 lines (24 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np
def draw_disk(tk_canvas, pos, radius, **kwargs):
bbox_min = pos - radius
bbox_max = pos + radius
tk_canvas.create_oval(bbox_min[0], bbox_min[1], bbox_max[0], bbox_max[1], width=0, **kwargs)
def draw_line_segment(tk_canvas, start, end, **kwargs):
tk_canvas.create_line(start[0], start[1], end[0], end[1], **kwargs)
def draw_line(tk_canvas, p1, p2, **kwargs):
bbox_max = np.array((tk_canvas.winfo_width(), tk_canvas.winfo_height()))
bbox_min = np.zeros(2)
line_dir = p2 - p1
line_ori = p1
# Simple hack for removing divide-by-zero
eps = 1e-20
line_dir[line_dir == 0] = eps
time_hit_a = (bbox_min - line_ori) / line_dir
time_hit_b = (bbox_max - line_ori) / line_dir
time_hit_max = np.maximum(time_hit_a, time_hit_b)
time_hit_min = np.minimum(time_hit_a, time_hit_b)
time_enter = np.max(time_hit_min)
time_exit = np.min(time_hit_max)
pos_enter = line_ori + (time_enter * line_dir)
pos_exit = line_ori + (time_exit * line_dir)
draw_line_segment(tk_canvas, pos_enter, pos_exit, **kwargs)