/
Vector.java
168 lines (131 loc) · 4.06 KB
/
Vector.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
package com.codingame.game;
public class Vector {
private final double x, y;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(x);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
Vector other = (Vector) obj;
if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x)) return false;
if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y)) return false;
return true;
}
public Vector(double x, double y) {
this.x = x;
this.y = y;
}
public Vector(Vector a, Vector b) {
this.x = b.x - a.x;
this.y = b.y - a.y;
}
public Vector(double angle) {
this.x = Math.cos(angle);
this.y = Math.sin(angle);
}
public Vector rotate(double angle) {
double nx = (x * Math.cos(angle)) - (y * Math.sin(angle));
double ny = (x * Math.sin(angle)) + (y * Math.cos(angle));
return new Vector(nx, ny);
};
public boolean equals(Vector v) {
return v.getX() == x && v.getY() == y;
}
public Vector round() {
return new Vector((int) Math.round(this.x), (int) Math.round(this.y));
}
public Vector truncate() {
return new Vector((int) this.x, (int) this.y);
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double distance(Vector v) {
return Math.sqrt((v.x - x) * (v.x - x) + (v.y - y) * (v.y - y));
}
public boolean inRange(Vector v, double range) {
return (v.x - x) * (v.x - x) + (v.y - y) * (v.y - y) <= range * range;
}
public Vector add(Vector v) {
return new Vector(x + v.x, y + v.y);
}
public Vector mult(double factor) {
return new Vector(x * factor, y * factor);
}
public Vector sub(Vector v) {
return new Vector(this.x - v.x, this.y - v.y);
}
public double length() {
return Math.sqrt(x * x + y * y);
}
public double lengthSquared() {
return x * x + y * y;
}
public Vector normalize() {
double length = length();
if (length == 0)
return new Vector(0, 0);
return new Vector(x / length, y / length);
}
public double dot(Vector v) {
return x * v.x + y * v.y;
}
public double angle() {
return Math.atan2(y, x);
}
@Override
public String toString() {
return "[" + x + ", " + y + "]";
}
public String toIntString() {
return (int) x + " " + (int) y;
}
public Vector project(Vector force) {
Vector normalize = this.normalize();
return normalize.mult(normalize.dot(force));
}
public final Vector cross(double s) {
return new Vector(-s * y, s * x);
}
public Vector hsymmetric(double center) {
return new Vector(2 * center - this.x, this.y);
}
public Vector vsymmetric(double center) {
return new Vector(this.x, 2 * center - this.y);
}
public Vector vsymmetric() {
return new Vector(this.x, -this.y);
}
public Vector hsymmetric() {
return new Vector(-this.x, this.y);
}
public Vector symmetric() {
return symmetric(new Vector(0, 0));
}
public Vector symmetric(Vector center) {
return new Vector(center.x * 2 - this.x, center.y * 2 - this.y);
}
public boolean withinBounds(double minx, double miny, double maxx, double maxy) {
return x >= minx && x < maxx && y >= miny && y < maxy;
}
public boolean isZero() {
return x == 0 && y == 0;
}
public Vector symmetricTruncate(Vector origin) {
return sub(origin).truncate().add(origin);
}
}