1 package org.newdawn.slick.geom;
2
3 import java.io.Serializable;
4
5 import org.newdawn.slick.util.FastTrig;
6
7 import javax.annotation.Nonnull;
8
9
10
11
12
13
14 public strictfp class Vector2f implements Serializable {
15
16 private static final long serialVersionUID = 1339934L;
17
18
19 public float x;
20
21 public float y;
22
23
24
25
26 public Vector2f() {
27 }
28
29
30
31
32
33
34 public Vector2f(float[] coords) {
35 x = coords[0];
36 y = coords[1];
37 }
38
39
40
41
42
43
44 public Vector2f(double theta) {
45 x = 1;
46 y = 0;
47 setTheta(theta);
48 }
49
50
51
52
53
54
55 void setTheta(double theta) {
56
57
58 if ((theta < -360) || (theta > 360)) {
59 theta = theta % 360;
60 }
61 if (theta < 0) {
62 theta = 360 + theta;
63 }
64 double oldTheta = getTheta();
65 if ((theta < -360) || (theta > 360)) {
66 oldTheta = oldTheta % 360;
67 }
68 if (theta < 0) {
69 oldTheta = 360 + oldTheta;
70 }
71
72 float len = length();
73 x = len * (float) FastTrig.cos(StrictMath.toRadians(theta));
74 y = len * (float) FastTrig.sin(StrictMath.toRadians(theta));
75
76
77
78
79
80 }
81
82
83
84
85
86
87
88
89
90 @Nonnull
91 public Vector2f add(double theta) {
92 setTheta(getTheta() + theta);
93
94 return this;
95 }
96
97
98
99
100
101
102
103 @Nonnull
104 public Vector2f sub(double theta) {
105 setTheta(getTheta() - theta);
106
107 return this;
108 }
109
110
111
112
113
114
115 double getTheta() {
116 double theta = StrictMath.toDegrees(StrictMath.atan2(y, x));
117 if ((theta < -360) || (theta > 360)) {
118 theta = theta % 360;
119 }
120 if (theta < 0) {
121 theta = 360 + theta;
122 }
123
124 return theta;
125 }
126
127
128
129
130
131
132 public float getX() {
133 return x;
134 }
135
136
137
138
139
140
141 public float getY() {
142 return y;
143 }
144
145
146
147
148
149
150 public Vector2f(@Nonnull Vector2f other) {
151 this(other.getX(),other.getY());
152 }
153
154
155
156
157
158
159
160 public Vector2f(float x, float y) {
161 this.x = x;
162 this.y = y;
163 }
164
165
166
167
168
169
170 public void set(@Nonnull Vector2f other) {
171 set(other.getX(),other.getY());
172 }
173
174
175
176
177
178
179
180 public float dot(@Nonnull Vector2f other) {
181 return (x * other.getX()) + (y * other.getY());
182 }
183
184
185
186
187
188
189
190
191 @Nonnull
192 public Vector2f set(float x, float y) {
193 this.x = x;
194 this.y = y;
195
196 return this;
197 }
198
199
200
201
202
203
204 @Nonnull
205 public Vector2f getPerpendicular() {
206 return new Vector2f(-y, x);
207 }
208
209
210
211
212
213
214
215 @Nonnull
216 public Vector2f set(float[] pt) {
217 return set(pt[0], pt[1]);
218 }
219
220
221
222
223
224
225 @Nonnull
226 public Vector2f negate() {
227 return new Vector2f(-x, -y);
228 }
229
230
231
232
233
234
235 @Nonnull
236 public Vector2f negateLocal() {
237 x = -x;
238 y = -y;
239
240 return this;
241 }
242
243
244
245
246
247
248
249 @Nonnull
250 public Vector2f add(@Nonnull Vector2f v)
251 {
252 x += v.getX();
253 y += v.getY();
254
255 return this;
256 }
257
258
259
260
261
262
263
264 @Nonnull
265 public Vector2f sub(@Nonnull Vector2f v)
266 {
267 x -= v.getX();
268 y -= v.getY();
269
270 return this;
271 }
272
273
274
275
276
277
278
279 @Nonnull
280 public Vector2f scale(float a)
281 {
282 x *= a;
283 y *= a;
284
285 return this;
286 }
287
288
289
290
291
292
293 @Nonnull
294 Vector2f normalise() {
295 float l = length();
296
297 if (l == 0) {
298 return this;
299 }
300
301 x /= l;
302 y /= l;
303 return this;
304 }
305
306
307
308
309
310
311 @Nonnull
312 public Vector2f getNormal() {
313 Vector2f cp = copy();
314 cp.normalise();
315 return cp;
316 }
317
318
319
320
321
322
323 public float lengthSquared() {
324 return (x * x) + (y * y);
325 }
326
327
328
329
330
331
332 public float length()
333 {
334 return (float) Math.sqrt(lengthSquared());
335 }
336
337
338
339
340
341
342
343 public void projectOntoUnit(@Nonnull Vector2f b, @Nonnull Vector2f result) {
344 float dp = b.dot(this);
345
346 result.x = dp * b.getX();
347 result.y = dp * b.getY();
348
349 }
350
351
352
353
354
355
356 @Nonnull
357 Vector2f copy() {
358 return new Vector2f(x,y);
359 }
360
361
362
363
364 @Nonnull
365 public String toString() {
366 return "[Vector2f "+x+","+y+" ("+length()+")]";
367 }
368
369
370
371
372
373
374
375 public float distance(@Nonnull Vector2f other) {
376 return (float) Math.sqrt(distanceSquared(other));
377 }
378
379
380
381
382
383
384
385
386
387 float distanceSquared(@Nonnull Vector2f other) {
388 float dx = other.getX() - getX();
389 float dy = other.getY() - getY();
390
391 return dx*dx +(dy*dy);
392 }
393
394
395
396
397 public int hashCode() {
398 return 997 * ((int)x) ^ 991 * ((int)y);
399 }
400
401
402
403
404 public boolean equals(Object other) {
405 if (other instanceof Vector2f) {
406 Vector2f o = ((Vector2f) other);
407 return (o.x == x) && (o.y == y);
408 }
409
410 return false;
411 }
412 }