pygame / src / pgVector2.c

 ``` 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121``` ```#include "pgVector2.h" #include int is_zero(double num) { return fabs(num) <= ZERO_EPSILON; } int is_equal(double a, double b) { double rErr; if(is_zero(a - b)) return 1; if(fabs(b) > fabs(a)) rErr = fabs((a - b) / b); else rErr = fabs((a - b) / a); return rErr <= RELATIVE_ZERO; } int less_equal(double a, double b) { return a < b || is_equal(a, b); } int more_equal(double a, double b) { return a > b || is_equal(a, b); } double c_get_length_square(Py_complex c) { double r; r = c.real * c.real; r += (c.imag * c.imag); return r; } double c_get_length(Py_complex c) { double r; r = c.real * c.real; r += (c.imag * c.imag); return sqrt(r); } Py_complex c_mul_complex_with_real(Py_complex c,double d) { Py_complex r; r.real = c.real * d; r.imag = c.imag * d; return r; } Py_complex c_div_complex_with_real(pgVector2 c,double d) { Py_complex r; assert(d != 0); r.real = c.real / d; r.imag = c.imag / d; return r; } void c_normalize(pgVector2* pVec) { double l = c_get_length(*pVec); assert(l > 0); pVec->real /= l; pVec->imag /= l; } double c_dot(pgVector2 a,pgVector2 b) { return a.real * b.real + a.imag * b.imag; } double c_cross(pgVector2 a, pgVector2 b) { return a.real*b.imag - a.imag*b.real; } pgVector2 c_fcross(double a, pgVector2 b) { pgVector2 ans; ans.real = -a*b.imag; ans.imag = a*b.real; return ans; } pgVector2 c_crossf(pgVector2 a, double b) { pgVector2 ans; ans.real = a.imag*b; ans.imag = -a.real*b; return ans; } void c_rotate(pgVector2* a, double seta) { double x = a->real; double y = a->imag; a->real = x*cos(seta) - y*sin(seta); a->imag = x*sin(seta) + y*cos(seta); } int c_equal(pgVector2* a, pgVector2* b) { return is_equal(a->real, b->real) && is_equal(a->imag, b->imag); } pgVector2 c_project(pgVector2 l,pgVector2 p) { double lp; c_normalize(&l); lp = c_dot(l,p); return c_mul_complex_with_real(l,lp); } ```