|
| 1 | +"""Processing math methods and vector/quaternion types.""" |
| 2 | +from .mewnala import math as _native_math |
| 3 | +from math import ( |
| 4 | + sin, cos, tan, |
| 5 | + asin, acos, atan, atan2, |
| 6 | + sqrt, exp, log, |
| 7 | + ceil, floor, |
| 8 | + degrees, radians, |
| 9 | +) |
| 10 | + |
| 11 | +Vec2 = _native_math.Vec2 |
| 12 | +Vec3 = _native_math.Vec3 |
| 13 | +Vec4 = _native_math.Vec4 |
| 14 | +Quat = _native_math.Quat |
| 15 | +VecIter = _native_math.PyVecIter |
| 16 | +vec2 = _native_math.vec2 |
| 17 | +vec3 = _native_math.vec3 |
| 18 | +vec4 = _native_math.vec4 |
| 19 | +quat = _native_math.quat |
| 20 | + |
| 21 | + |
| 22 | +def sq(x): |
| 23 | + return x * x |
| 24 | + |
| 25 | + |
| 26 | +def pow(base, exponent): |
| 27 | + return base ** exponent |
| 28 | + |
| 29 | + |
| 30 | +def constrain(value, low, high): |
| 31 | + if value < low: |
| 32 | + return low |
| 33 | + if value > high: |
| 34 | + return high |
| 35 | + return value |
| 36 | + |
| 37 | + |
| 38 | +def lerp(start, stop, amt): |
| 39 | + return start + (stop - start) * amt |
| 40 | + |
| 41 | + |
| 42 | +def norm(value, start, stop): |
| 43 | + return (value - start) / (stop - start) |
| 44 | + |
| 45 | + |
| 46 | +def remap(value, start1, stop1, start2, stop2): |
| 47 | + return start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1)) |
| 48 | + |
| 49 | + |
| 50 | +def mag(*args): |
| 51 | + if len(args) == 2: |
| 52 | + a, b = args |
| 53 | + return sqrt(a * a + b * b) |
| 54 | + if len(args) == 3: |
| 55 | + a, b, c = args |
| 56 | + return sqrt(a * a + b * b + c * c) |
| 57 | + raise TypeError(f"mag() takes 2 or 3 arguments ({len(args)} given)") |
| 58 | + |
| 59 | + |
| 60 | +def dist(*args): |
| 61 | + if len(args) == 4: |
| 62 | + x1, y1, x2, y2 = args |
| 63 | + dx, dy = x2 - x1, y2 - y1 |
| 64 | + return sqrt(dx * dx + dy * dy) |
| 65 | + if len(args) == 6: |
| 66 | + x1, y1, z1, x2, y2, z2 = args |
| 67 | + dx, dy, dz = x2 - x1, y2 - y1, z2 - z1 |
| 68 | + return sqrt(dx * dx + dy * dy + dz * dz) |
| 69 | + raise TypeError(f"dist() takes 4 or 6 arguments ({len(args)} given)") |
0 commit comments