-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.py
More file actions
74 lines (59 loc) · 2.25 KB
/
Copy pathrender.py
File metadata and controls
74 lines (59 loc) · 2.25 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from typing import Protocol
import cv2 as cv
import numpy as np
class Shape(Protocol):
class Circle(Protocol):
radius:float|int
class RigidBody(Protocol):
position:np.ndarray
shape:Shape
mass:float|int
def step():
...
def move():
...
def move_to():
...
class World(Protocol):
body_list:list[RigidBody]
def update():
...
class Renderer:
scale = 1
resolution = np.array([720,1280],dtype='int')
center = resolution//2
background_color = (0,0,0)
frame = np.zeros(list(resolution)+[3],dtype='uint8')
@classmethod
def _set_scale(cls,body_list:list[RigidBody],padding=50) -> None:
min_x , *_ , max_x = sorted([body.position[0] for body in body_list])
min_y , *_ , max_y = sorted([body.position[1] for body in body_list])
cls.scale = min((cls.resolution[1]-(2*padding))/(max_x-min_x),
(cls.resolution[0]-(2*padding))/(max_y-min_y))
@classmethod
def _set_center(cls,body_list:list[RigidBody]) -> None:
min_x , *_ , max_x = sorted([body.position[0] for body in body_list])
min_y , *_ , max_y = sorted([body.position[1] for body in body_list])
center = np.array([(min_y + max_y)/2,
(min_x + max_x)/2])
cls.center = cls.resolution/2 - (cls.scale*center)
@classmethod
def setup(cls,body_list:list[RigidBody]) -> None:
cls._set_scale(body_list)
cls._set_center(body_list)
@classmethod
def draw_circle_body(cls,position,rad,mass,color=(255,255,255)) -> None:
try:
x,y = map(int,(position*cls.scale)+cls.center[::-1])
r = int(rad*cls.scale)
cv.circle(img=cls.frame,center=(x,y),radius=r,color=color,thickness=-1)
except Exception as e:
print('error',e,position,rad,mass)
@classmethod
def render_frame(cls,body_list:list[RigidBody]) -> None:
cls.frame[::] = cls.background_color
for body in body_list:
cls.draw_circle_body(body.position,body.shape.radius,body.mass,color=body.shape.color)
@classmethod
def change_background(cls,color:tuple=(0,0,0)) -> None:
cls.background_color = color