| 6 | |
| 7 | |
| 8 | class Vec2(object): |
| 9 | def __init__(self, x=0.0, y=0.0): |
| 10 | if type(x) in (list, tuple): |
| 11 | self.x = x[0] |
| 12 | self.y = x[1] |
| 13 | else: |
| 14 | self.x = x |
| 15 | self.y = y |
| 16 | |
| 17 | @staticmethod |
| 18 | def from_radian(rad): |
| 19 | x = math.cos(rad) |
| 20 | y = math.sin(rad) |
| 21 | return Vec2(x, y) |
| 22 | |
| 23 | def __add__(self, other): |
| 24 | return Vec2(self.x + other.x, self.y + other.y) |
| 25 | |
| 26 | def __sub__(self, other): |
| 27 | return Vec2(self.x - other.x, self.y - other.y) |
| 28 | |
| 29 | def __radd__(self, other): |
| 30 | return Vec2(other.x + self.x, other.y + self.y) |
| 31 | |
| 32 | def __rsub__(self, other): |
| 33 | return Vec2(other.x - self.x, other.y - self.y) |
| 34 | |
| 35 | def __mul__(self, other): |
| 36 | return Vec2(self.x * other, self.y * other) |
| 37 | |
| 38 | def __rmul__(self, other): |
| 39 | return Vec2(self.x * other, self.y * other) |
| 40 | |
| 41 | def to_list(self): |
| 42 | return [self.x, self.y] |
| 43 | |
| 44 | @classmethod |
| 45 | def intersection_angle(cls, v1, v2): |
| 46 | cosval = cls.dot_product(v1, v2) / (v1.length * v2.length) |
| 47 | if -2 < cosval < -1: |
| 48 | cosval = -1 |
| 49 | elif 1 < cosval < 2: |
| 50 | cosval = 1 |
| 51 | return math.acos(cosval) * (1 if cls.cross_product(v1, v2) > 0 else -1) |
| 52 | |
| 53 | @staticmethod |
| 54 | def dot_product(v1, v2): |
| 55 | return v1.x * v2.x + v1.y * v2.y |
| 56 | |
| 57 | @staticmethod |
| 58 | def cross_product(v1, v2): |
| 59 | return v1.x * v2.y - v2.x * v1.y |
| 60 | |
| 61 | @property |
| 62 | def length(self): |
| 63 | return math.sqrt(self.x ** 2 + self.y ** 2) |
| 64 | |
| 65 | def unit(self): |
no outgoing calls
no test coverage detected