|
| 1 | +# An OO wrapper around the Vector2 struct, by @Emtyloc |
| 2 | + |
| 3 | +from pyray import * |
| 4 | + |
| 5 | +class Vector2Ex(list): |
| 6 | + def __init__(self, x, y): |
| 7 | + super(Vector2Ex, self).__init__([x, y]) |
| 8 | + |
| 9 | + @property |
| 10 | + def x(self): |
| 11 | + return self[0] |
| 12 | + |
| 13 | + @x.setter |
| 14 | + def x(self, value): |
| 15 | + self[0]= value |
| 16 | + |
| 17 | + @property |
| 18 | + def y(self): |
| 19 | + return self[1] |
| 20 | + |
| 21 | + @y.setter |
| 22 | + def y(self, value): |
| 23 | + self[1]= value |
| 24 | + |
| 25 | + @staticmethod |
| 26 | + def to_Vec2(v: Vector2): |
| 27 | + """ |
| 28 | + Cast Vector2 to Vec2. |
| 29 | + """ |
| 30 | + return Vector2Ex(v.x, v.y) |
| 31 | + |
| 32 | + def __repr__(self) -> str: |
| 33 | + return f"{self.x}, {self.y}" |
| 34 | + |
| 35 | + def __eq__(self, other): |
| 36 | + if isinstance(other, Vector2Ex): |
| 37 | + return self.x == other.x and self.y == other.y |
| 38 | + return False |
| 39 | + |
| 40 | + def __add__(self, other): |
| 41 | + if isinstance(other, Vector2Ex): |
| 42 | + return Vector2Ex(self.x + other.x, self.y + other.y) |
| 43 | + return Vector2Ex(self.x + other, self.y + other) |
| 44 | + |
| 45 | + def __iadd__(self, other): |
| 46 | + if isinstance(other, Vector2Ex): |
| 47 | + self.x += other.x |
| 48 | + self.y += other.y |
| 49 | + else: |
| 50 | + res = vector2_add_value(self, other) |
| 51 | + self.x = res.x |
| 52 | + self.y = res.y |
| 53 | + return self |
| 54 | + |
| 55 | + def __radd__(self, other): |
| 56 | + return self + other |
| 57 | + |
| 58 | + def __sub__(self, other): |
| 59 | + if isinstance(other, Vector2Ex): |
| 60 | + return Vector2Ex(self.x - other.x, self.y - other.y) |
| 61 | + return Vector2Ex(self.x - other, self.y - other) |
| 62 | + |
| 63 | + def __isub__(self, other): |
| 64 | + if isinstance(other, Vector2Ex): |
| 65 | + self.x -= other.x |
| 66 | + self.y -= other.y |
| 67 | + else: |
| 68 | + self.x -= other |
| 69 | + self.y -= other |
| 70 | + return self |
| 71 | + |
| 72 | + def __rsub__(self, other): |
| 73 | + return Vector2Ex(other - self.x, other - self.y) |
| 74 | + |
| 75 | + def __mul__(self, other): |
| 76 | + if isinstance(other, Vector2Ex): |
| 77 | + res = vector2_multiply(self, other) |
| 78 | + return self.to_Vec2(res) |
| 79 | + return Vector2Ex(self.x * other, self.y * other) |
| 80 | + |
| 81 | + def __imul__(self, other): |
| 82 | + if isinstance(other, Vector2Ex): |
| 83 | + res = vector2_multiply(self, other) |
| 84 | + else: |
| 85 | + res = vector2_scale(self, other) |
| 86 | + self.x = res.x |
| 87 | + self.y = res.y |
| 88 | + return self |
| 89 | + |
| 90 | + def __truediv__(self, other): |
| 91 | + if isinstance(other, Vector2Ex): |
| 92 | + res = vector_2divide(self, other) |
| 93 | + return self.to_Vec2(res) |
| 94 | + return Vector2Ex(self.x / other, self.y / other) |
| 95 | + |
| 96 | + def __itruediv__(self, other): |
| 97 | + if isinstance(other, Vector2Ex): |
| 98 | + res = vector_2divide(self, other) |
| 99 | + else: |
| 100 | + res = vector2_scale(self, 1/other) |
| 101 | + self.x = res.x |
| 102 | + self.y = res.y |
| 103 | + return self |
| 104 | + |
| 105 | + def __neg__(self): |
| 106 | + return Vector2Ex(-self.x, -self.y) |
| 107 | + |
| 108 | + def __pos__(self): |
| 109 | + return Vector2Ex(self.x, self.y) |
| 110 | + |
| 111 | + def __pow__(self, exponent): |
| 112 | + return Vector2Ex(self.x ** exponent, self.y ** exponent) |
| 113 | + |
| 114 | + # PyRay mapped vector2 functions |
| 115 | + |
| 116 | + def angle(self, vec2): |
| 117 | + return vector2_angle(self, vec2) |
| 118 | + |
| 119 | + def clamp(self, min_vec2, max_vec2): |
| 120 | + res = vector2_clamp(self, min_vec2, max_vec2) |
| 121 | + return self.to_Vec2(res) |
| 122 | + |
| 123 | + def clamp_value(self, min_val: float, max_val: float): |
| 124 | + res = vector2_clamp_value(self, min_val, max_val) |
| 125 | + return self.to_Vec2(res) |
| 126 | + |
| 127 | + def distance(self, vec2): |
| 128 | + return vector_2distance(self, vec2) |
| 129 | + |
| 130 | + def distance_sqr(self, vec2) -> float: |
| 131 | + return vector_2distance_sqr(self, vec2) |
| 132 | + |
| 133 | + def dot_product(self, vec2) -> float: |
| 134 | + return vector_2dot_product(self, vec2) |
| 135 | + |
| 136 | + def invert(self): |
| 137 | + res = vector2_invert(self) |
| 138 | + return self.to_Vec2(res) |
| 139 | + |
| 140 | + def length(self): |
| 141 | + return vector2_length(self) |
| 142 | + |
| 143 | + def length_sqr(self) -> float: |
| 144 | + return vector2_length_sqr(self) |
| 145 | + |
| 146 | + def lerp(self, vec2, amount: float): |
| 147 | + res = vector2_lerp(self, vec2, amount) |
| 148 | + return self.to_Vec2(res) |
| 149 | + |
| 150 | + def move_towards(self, target_vec2, max_distance: float): |
| 151 | + res = vector2_move_towards(self, target_vec2, max_distance) |
| 152 | + return self.to_Vec2(res) |
| 153 | + |
| 154 | + def negate(self): |
| 155 | + res = vector2_negate(self) |
| 156 | + return self.to_Vec2(res) |
| 157 | + |
| 158 | + def normalize(self): |
| 159 | + res = vector2_normalize(self) |
| 160 | + return self.to_Vec2(res) |
| 161 | + |
| 162 | + def reflect(self, normal_vec2): |
| 163 | + res = vector2_reflect(self, normal_vec2) |
| 164 | + return self.to_Vec2(res) |
| 165 | + |
| 166 | + def rotate(self, angle: float): |
| 167 | + res = vector2_rotate(self, angle) |
| 168 | + return self.to_Vec2(res) |
| 169 | + |
| 170 | + def transform(self, mat: Matrix): |
| 171 | + res = vector2_transform(self, mat) |
| 172 | + return self.to_Vec2(res) |
| 173 | + |
| 174 | + @staticmethod |
| 175 | + def line_angle(start_vec2, end_vec2) -> float: |
| 176 | + return vector2_line_angle(start_vec2, end_vec2) |
| 177 | + |
| 178 | + @staticmethod |
| 179 | + def one(): |
| 180 | + return Vector2Ex(1, 1) |
| 181 | + |
| 182 | + @staticmethod |
| 183 | + def zero(): |
| 184 | + return Vector2Ex(0, 0) |
| 185 | + |
| 186 | + |
| 187 | +if __name__ == "__main__": |
| 188 | + # Arithmetic ops |
| 189 | + v1 = Vector2Ex(5, 5) |
| 190 | + v2 = Vector2Ex(10, 10) |
| 191 | + |
| 192 | + print(v1 + v2) # 15, 15 |
| 193 | + print(v1 - v2) # -5, -5 |
| 194 | + |
| 195 | + print(v1 * v2) # 50.0, 50.0 |
| 196 | + print(v1 / v2) # 0.5, 0.5 |
| 197 | + |
| 198 | + print(v1 * 2) # 10, 10 |
| 199 | + print(v2 / 2) # 5.0, 5.0 |
| 200 | + |
| 201 | + v1+=v2 |
| 202 | + print(v1) #15, 15 |
| 203 | + v2-=v1 |
| 204 | + print(v2) #-5, -5 |
| 205 | + |
| 206 | + v1/=-v2 |
| 207 | + print(v1) #3.0, 3.0 |
| 208 | + v2*=v1 |
| 209 | + print(v2) #-15.0, -15.0 |
| 210 | + |
| 211 | + v3 = Vector2Ex(3, 5) |
| 212 | + print(v3 ** 2) #9, 25 |
| 213 | + |
| 214 | + v1 = Vector2Ex.one() |
| 215 | + print(v1) |
| 216 | + |
| 217 | + v0 = Vector2Ex.zero() |
| 218 | + print(v0) |
| 219 | + |
| 220 | + # Vector2 pyray methods |
| 221 | + v1 = Vector2Ex(3, 4) |
| 222 | + v2 = Vector2Ex(1, 2) |
| 223 | + v_min = Vector2Ex(0, 0) |
| 224 | + v_max = Vector2Ex(5, 5) |
| 225 | + |
| 226 | + print("Angle:", v1.angle(v2)) |
| 227 | + |
| 228 | + print("Clamp:", v1.clamp(v_min, v_max)) |
| 229 | + |
| 230 | + print("Clamp value:", v1.clamp_value(1.5, 3.5)) |
| 231 | + |
| 232 | + print("Distance:", v1.distance(v2)) |
| 233 | + |
| 234 | + print("Distance Sqr:", v1.distance_sqr(v2)) |
| 235 | + |
| 236 | + print("Dot Product:", v1.dot_product(v2)) |
| 237 | + |
| 238 | + print("Invert:", v1.invert()) |
| 239 | + |
| 240 | + print("Length:", v1.length()) |
| 241 | + |
| 242 | + print("Length Sqr:", v1.length_sqr()) |
| 243 | + |
| 244 | + print("Lerp:", v1.lerp(v2, 0.5)) |
| 245 | + |
| 246 | + print("Line Angle:", Vector2Ex.line_angle(v1, v2)) |
| 247 | + |
| 248 | + print("Move Towards:", v1.move_towards(v2, 0.5)) |
| 249 | + |
| 250 | + print("Negate:", v1.negate()) |
| 251 | + |
| 252 | + print("Normalize:", v1.normalize()) |
| 253 | + |
| 254 | + print("Reflect:", v1.reflect(v2)) |
| 255 | + |
| 256 | + print("Rotate:", v1.rotate(45)) |
| 257 | + |
| 258 | + # I don't know why this not work |
| 259 | + # mat = Matrix2x2(1, 0, 0, 1) |
| 260 | + # print("Transform:", v1.transform(mat)) |
0 commit comments