-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommonTypes.py
37 lines (26 loc) · 1 KB
/
CommonTypes.py
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
import math
import random
from typing import Self, overload
class Vector:
def __init__(self, x: float, y: float) -> None:
self.x: float = x
self.y: float = y
def length(self) -> float:
return math.sqrt(self.x * self.x + self.y * self.y)
def __mul__(self, other: float) -> Self:
return Vector(self.x * other, self.y * other)
def __add__(self, other: Self) -> Self:
return Vector(self.x + other.x, self.y + other.y)
def __repr__(self):
return f"Vector({self.x}, {self.y})"
pass
def origin() -> Vector:
return Vector(0,0)
def random_vec(max_length: float) -> Vector:
angle = random.uniform(0, 2 * math.pi)
length = random.uniform(0, max_length)
return Vector(length * math.cos(angle), length * math.sin(angle))
def random_vec_in_range(upper_left: Vector, lower_right: Vector) -> Vector:
x = random.uniform(upper_left.x, lower_right.x)
y = random.uniform(upper_left.y, lower_right.y)
return Vector(x,y)