1 //**************************************************************************
2 //**
3 //** ## ## ## ## ## #### #### ### ###
4 //** ## ## ## ## ## ## ## ## ## ## #### ####
5 //** ## ## ## ## ## ## ## ## ## ## ## ## ## ##
6 //** ## ## ######## ## ## ## ## ## ## ## ### ##
7 //** ### ## ## ### ## ## ## ## ## ##
8 //** # ## ## # #### #### ## ##
9 //**
10 //** $Id: vector.h 1741 2006-09-26 23:26:48Z dj_jl $
11 //**
12 //** Copyright (C) 1999-2006 Jānis Legzdiņš
13 //**
14 //** This program is free software; you can redistribute it and/or
15 //** modify it under the terms of the GNU General Public License
16 //** as published by the Free Software Foundation; either version 2
17 //** of the License, or (at your option) any later version.
18 //**
19 //** This program is distributed in the hope that it will be useful,
20 //** but WITHOUT ANY WARRANTY; without even the implied warranty of
21 //** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 //** GNU General Public License for more details.
23 //**
24 //**************************************************************************
25
26 class TVec
27 {
28 public:
29 double x;
30 double y;
31
TVec()32 TVec() { }
33
TVec(double Ax,double Ay)34 TVec(double Ax, double Ay)
35 {
36 x = Ax;
37 y = Ay;
38 }
39
TVec(const double f[2])40 TVec (const double f[2])
41 {
42 x = f[0];
43 y = f[1];
44 }
45
46 const double& operator[](int i) const
47 {
48 return (&x)[i];
49 }
50
51 double& operator[](int i)
52 {
53 return (&x)[i];
54 }
55
56 TVec &operator += (const TVec &v)
57 {
58 x += v.x;
59 y += v.y;
60 return *this;
61 }
62
63 TVec &operator -= (const TVec &v)
64 {
65 x -= v.x;
66 y -= v.y;
67 return *this;
68 }
69
70 TVec &operator *= (double scale)
71 {
72 x *= scale;
73 y *= scale;
74 return *this;
75 }
76
77 TVec &operator /= (double scale)
78 {
79 x /= scale;
80 y /= scale;
81 return *this;
82 }
83
84 TVec operator + () const
85 {
86 return *this;
87 }
88
89 TVec operator - () const
90 {
91 return TVec(-x, -y);
92 }
93
Length()94 double Length() const
95 {
96 return sqrt(x * x + y * y);
97 }
98 };
99
100 inline TVec operator + (const TVec &v1, const TVec &v2)
101 {
102 return TVec(v1.x + v2.x, v1.y + v2.y);
103 }
104
105 inline TVec operator - (const TVec &v1, const TVec &v2)
106 {
107 return TVec(v1.x - v2.x, v1.y - v2.y);
108 }
109
110 inline TVec operator * (const TVec& v, double s)
111 {
112 return TVec(s * v.x, s * v.y);
113 }
114
115 inline TVec operator * (double s, const TVec& v)
116 {
117 return TVec(s * v.x, s * v.y);
118 }
119
120 inline TVec operator / (const TVec& v, double s)
121 {
122 return TVec(v.x / s, v.y / s);
123 }
124
125 inline bool operator == (const TVec& v1, const TVec& v2)
126 {
127 return v1.x == v2.x && v1.y == v2.y;
128 }
129
130 inline bool operator != (const TVec& v1, const TVec& v2)
131 {
132 return v1.x != v2.x || v1.y != v2.y;
133 }
134
Length(const TVec & v)135 inline double Length(const TVec &v)
136 {
137 return sqrt(v.x * v.x + v.y * v.y);
138 }
139
Normalise(const TVec & v)140 inline TVec Normalise(const TVec& v)
141 {
142 return v / v.Length();
143 }
144
DotProduct(const TVec & v1,const TVec & v2)145 inline double DotProduct(const TVec& v1, const TVec& v2)
146 {
147 return v1.x * v2.x + v1.y * v2.y;
148 }
149
150 //==========================================================================
151 //
152 // PLANES
153 //
154 //==========================================================================
155
156 class TPlane
157 {
158 public:
159 TVec normal;
160 double dist;
161
Set(const TVec & Anormal,double Adist)162 void Set(const TVec &Anormal, double Adist)
163 {
164 normal = Anormal;
165 dist = Adist;
166 }
167
168 // Initialises vertical plane from point and direction
SetPointDir(const TVec & point,const TVec & dir)169 void SetPointDir(const TVec &point, const TVec &dir)
170 {
171 normal = Normalise(TVec(dir.y, -dir.x));
172 dist = DotProduct(point, normal);
173 }
174
175 // Initialises vertical plane from 2 points
Set2Points(const TVec & v1,const TVec & v2)176 void Set2Points(const TVec &v1, const TVec &v2)
177 {
178 SetPointDir(v1, v2 - v1);
179 }
180 };
181