Rev 2097 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1528 | jag | 1 | /* |
2 | * vector2d.h |
||
2302 | jag | 3 | * DIN Is Noise is copyright (c) 2006-2025 Jagannathan Sampath |
1528 | jag | 4 | * For more information, please visit http://dinisnoise.org/ |
5 | */ |
||
6 | |||
7 | #ifndef __vector2d |
||
8 | #define __vector2d |
||
9 | |||
10 | #include <math.h> |
||
11 | #include "point.h" |
||
12 | |||
13 | template <class T> inline void direction (point<T>& p1p2, point<T>& p1, point<T>& p2) { |
||
14 | p1p2.x = p2.x - p1.x; |
||
15 | p1p2.y = p2.y - p1.y; |
||
16 | } |
||
17 | |||
18 | template<class T> inline void direction (T& dx, T& dy, const T& x1, const T& y1, const T& x2, const T& y2) { |
||
19 | dx = x2 - x1; |
||
20 | dy = y2 - y1; |
||
21 | } |
||
22 | |||
23 | template <class T> inline void perpendicular (point<T>& r, point<T>& p) { |
||
24 | r.x = p.y; |
||
25 | r.y = -p.x; |
||
26 | } |
||
27 | |||
28 | |||
29 | template <class T> inline void perpendicular (T& px, T& py, const T& vx, const T& vy) { |
||
30 | px = vy; |
||
31 | py = -vx; |
||
32 | } |
||
33 | |||
34 | template <class T> inline int is_zero_vec (const T& x1, const T& y1, const T& e = 0.0001) { |
||
35 | return (equals (x1, (T)0.0, e) && equals (y1, (T)0.0, e)); |
||
36 | } |
||
37 | |||
38 | template <class T> inline double magnitude (const T& x1, const T& y1, const T& x2, const T& y2) { |
||
39 | T dx = x2 - x1, dy = y2 - y1; |
||
40 | return sqrt ((double) (dx * dx + dy * dy)); |
||
41 | } |
||
42 | |||
43 | template <class T> inline double magnitude (const T& vx, const T& vy) { |
||
44 | return sqrt ((double) (vx * vx + vy * vy)); |
||
45 | } |
||
46 | |||
47 | template <class T> inline double magnitude (point<T>& p) { |
||
48 | return sqrt ((double) (p.x * p.x + p.y * p.y)); |
||
49 | } |
||
50 | |||
51 | template <class T> inline double magnitude2 (const T& x1, const T& y1, const T& x2, const T& y2) { |
||
52 | T dx = x2 - x1, dy = y2 - y1; |
||
53 | return (dx * dx + dy * dy); |
||
54 | } |
||
55 | |||
56 | template <class T> inline T magnitude2 (const T& vx, const T& vy) { |
||
57 | return (vx * vx + vy * vy); |
||
58 | } |
||
59 | |||
60 | |||
61 | template<class S, class T> inline double unit_vector (S& ux, S& uy, const T& x1, const T& y1, const T& x2, const T& y2, const S& zx = 0, const S& zy = 0) { |
||
62 | // unit vector joining x1,y1 & x2,y2 |
||
63 | T dx, dy; direction (dx, dy, x1, y1, x2, y2); |
||
64 | double mag = magnitude (dx, dy); |
||
65 | if (mag > 0) { |
||
66 | ux = (S) (dx / mag); |
||
67 | uy = (S) (dy / mag); |
||
68 | } else { |
||
69 | ux = zx; |
||
70 | uy = zy; |
||
71 | } |
||
72 | return mag; |
||
73 | } |
||
74 | |||
75 | template <class T> inline double unit_vector (T& ux, T& uy, const T& vx, const T& vy) { |
||
76 | double mag = magnitude (vx, vy); |
||
77 | if (mag > 0) { |
||
78 | ux = vx / mag; |
||
79 | uy = vy / mag; |
||
80 | } else { |
||
81 | ux = uy = 0; |
||
82 | } |
||
83 | return mag; |
||
84 | } |
||
85 | |||
86 | template <class T> inline double unit_vector (point<T>& u, point<T>& v) { |
||
87 | double mag = magnitude (v.x, v.y); |
||
88 | if (mag > 0) { |
||
89 | u.x = v.x / mag; |
||
90 | u.y = v.y / mag; |
||
91 | } else { |
||
92 | u.x = u.y = 0; |
||
93 | } |
||
94 | return mag; |
||
95 | } |
||
96 | |||
97 | |||
98 | template <typename T> inline void rotate_vector (T& vx, T& vy, float angle) { |
||
99 | T rx = vx * cos (angle) - vy * sin (angle); |
||
100 | T ry = vx * sin (angle) + vy * cos (angle); |
||
101 | vx = rx; |
||
102 | vy = ry; |
||
103 | } |
||
104 | |||
105 | #endif |