-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntVector2.java
358 lines (303 loc) · 6.98 KB
/
IntVector2.java
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* Copyright 2021 damios
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.damios.guacamole.gdx.math;
import java.io.Serializable;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector;
import com.badlogic.gdx.math.Vector2;
public final class IntVector2 implements Serializable, Vector<IntVector2> {
private static final long serialVersionUID = 1L;
/** The x-component of this vector. **/
public int x;
/** The y-component of this vector. **/
public int y;
/**
* Constructs a new vector at (0,0).
*/
public IntVector2() {
}
/**
* Constructs a vector with the given components.
*
* @param x
* The x-component
* @param y
* The y-component
*/
public IntVector2(int x, int y) {
this.x = x;
this.y = y;
}
/**
* Constructs a vector from the given vector
*
* @param v
* The vector
*/
public IntVector2(IntVector2 v) {
set(v);
}
@Override
public IntVector2 cpy() {
return new IntVector2(this);
}
@Override
public float len() {
return (float) Math.sqrt(x * x + y * y);
}
@Override
public float len2() {
return x * x + y * y;
}
@Override
public IntVector2 limit(float limit) {
return limit2(limit * limit);
}
@Override
public IntVector2 limit2(float limit2) {
float len2 = len2();
if (len2 > limit2) {
return scl((float) Math.sqrt(limit2 / len2));
}
return this;
}
@Override
public IntVector2 setLength(float len) {
return setLength2(len * len);
}
@Override
public IntVector2 setLength2(float len2) {
float oldLen2 = len2();
return (oldLen2 == 0 || oldLen2 == len2) ? this
: scl((float) Math.sqrt(len2 / oldLen2));
}
@Override
public IntVector2 clamp(float min, float max) {
final float len2 = len2();
if (len2 == 0f)
return this;
float max2 = max * max;
if (len2 > max2)
return scl((float) Math.sqrt(max2 / len2));
float min2 = min * min;
if (len2 < min2)
return scl((float) Math.sqrt(min2 / len2));
return this;
}
@Override
public IntVector2 set(IntVector2 v) {
x = v.x;
y = v.y;
return this;
}
public IntVector2 set(int x, int y) {
this.x = x;
this.y = y;
return this;
}
@Override
public IntVector2 sub(IntVector2 v) {
x -= v.x;
y -= v.y;
return this;
}
public IntVector2 sub(int x, int y) {
this.x -= x;
this.y -= y;
return this;
}
@Override
public IntVector2 nor() {
float len = len();
if (len != 0) {
x /= len;
y /= len;
}
return this;
}
@Override
public IntVector2 add(IntVector2 v) {
x += v.x;
y += v.y;
return this;
}
public IntVector2 add(int x, int y) {
this.x += x;
this.y += y;
return this;
}
@Override
public float dot(IntVector2 v) {
return x * v.x + y * v.y;
}
@Override
public IntVector2 scl(float scalar) {
x *= scalar;
y *= scalar;
return this;
}
public IntVector2 scl(int x, int y) {
this.x *= x;
this.y *= y;
return this;
}
@Override
public IntVector2 scl(IntVector2 v) {
this.x *= v.x;
this.y *= v.y;
return this;
}
@Override
public float dst(IntVector2 v) {
final float x_d = v.x - x;
final float y_d = v.y - y;
return (float) Math.sqrt(x_d * x_d + y_d * y_d);
}
@Override
public float dst2(IntVector2 v) {
final float x_d = v.x - x;
final float y_d = v.y - y;
return x_d * x_d + y_d * y_d;
}
@Override
public IntVector2 lerp(IntVector2 target, float alpha) {
final float invAlpha = 1.0f - alpha;
this.x = (int) ((x * invAlpha) + (target.x * alpha));
this.y = (int) ((y * invAlpha) + (target.y * alpha));
return this;
}
@Override
public IntVector2 interpolate(IntVector2 target, float alpha,
Interpolation interpolation) {
return lerp(target, interpolation.apply(alpha));
}
@Override
public IntVector2 setToRandomDirection() {
float theta = MathUtils.random(0f, MathUtils.PI2);
return this.set((int) MathUtils.cos(theta), (int) MathUtils.sin(theta));
}
@Override
public boolean isUnit() {
return isUnit(0.000000001f);
}
@Override
public boolean isUnit(float margin) {
return Math.abs(len2() - 1f) < margin;
}
@Override
public boolean isZero() {
return x == 0 && y == 0;
}
@Override
public boolean isZero(float margin) {
return len2() < margin;
}
@Override
public boolean isOnLine(IntVector2 other, float epsilon) {
return MathUtils.isZero(x * other.y - y * other.x, epsilon);
}
@Override
public boolean isOnLine(IntVector2 other) {
return MathUtils.isZero(x * other.y - y * other.x);
}
@Override
public boolean isCollinear(IntVector2 other, float epsilon) {
return isOnLine(other, epsilon) && dot(other) > 0f;
}
@Override
public boolean isCollinear(IntVector2 other) {
return isOnLine(other) && dot(other) > 0f;
}
@Override
public boolean isCollinearOpposite(IntVector2 other, float epsilon) {
return isOnLine(other, epsilon) && dot(other) < 0f;
}
@Override
public boolean isCollinearOpposite(IntVector2 other) {
return isOnLine(other) && dot(other) < 0f;
}
@Override
public boolean isPerpendicular(IntVector2 other) {
return MathUtils.isZero(dot(other));
}
@Override
public boolean isPerpendicular(IntVector2 other, float epsilon) {
return MathUtils.isZero(dot(other), epsilon);
}
@Override
public boolean hasSameDirection(IntVector2 other) {
return dot(other) > 0;
}
@Override
public boolean hasOppositeDirection(IntVector2 other) {
return dot(other) < 0;
}
@Override
public boolean epsilonEquals(IntVector2 other, float epsilon) {
if (other == null)
return false;
if (Math.abs(other.x - x) > epsilon)
return false;
if (Math.abs(other.y - y) > epsilon)
return false;
return true;
}
@Override
public IntVector2 mulAdd(IntVector2 v, float scalar) {
this.x += v.x * scalar;
this.y += v.y * scalar;
return this;
}
@Override
public IntVector2 mulAdd(IntVector2 v, IntVector2 mulVec) {
this.x += v.x * mulVec.x;
this.y += v.y * mulVec.y;
return this;
}
@Override
public IntVector2 setZero() {
this.x = 0;
this.y = 0;
return this;
}
@Override
public String toString() {
return "IntVector{x=" + x + ",y=" + y + "}";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Vector2 other = (Vector2) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}