diff options
author | Pacien TRAN-GIRARD | 2015-11-21 10:36:18 +0100 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2015-11-21 10:36:18 +0100 |
commit | 655ac88f4e73b2df532a451aedf5a561ea1b0d2c (patch) | |
tree | ef6f914a465575f313e2b280bf0639d87a4cbd58 /src/ch/epfl/maze/util/Vector2D.java | |
parent | 56279eb59ccdea48b18daa027a5095d861b4e2f4 (diff) | |
download | maze-solver-655ac88f4e73b2df532a451aedf5a561ea1b0d2c.tar.gz |
Import project structure
Diffstat (limited to 'src/ch/epfl/maze/util/Vector2D.java')
-rw-r--r-- | src/ch/epfl/maze/util/Vector2D.java | 228 |
1 files changed, 228 insertions, 0 deletions
diff --git a/src/ch/epfl/maze/util/Vector2D.java b/src/ch/epfl/maze/util/Vector2D.java new file mode 100644 index 0000000..5e4b975 --- /dev/null +++ b/src/ch/epfl/maze/util/Vector2D.java | |||
@@ -0,0 +1,228 @@ | |||
1 | package ch.epfl.maze.util; | ||
2 | |||
3 | /** | ||
4 | * Immutable 2-dimensional vector (<i>x</i>, <i>y</i>). | ||
5 | * | ||
6 | */ | ||
7 | |||
8 | public final class Vector2D { | ||
9 | |||
10 | /* shift constant to compute the hash */ | ||
11 | private static final int SHIFT = 1000; | ||
12 | |||
13 | /* 2-dimension coordinates */ | ||
14 | private final int mX, mY; | ||
15 | |||
16 | /** | ||
17 | * Constructs a 2-dimensional vector. | ||
18 | * | ||
19 | * @param x | ||
20 | * Horizontal coordinate | ||
21 | * @param y | ||
22 | * Vertical coordinate | ||
23 | */ | ||
24 | |||
25 | public Vector2D(int x, int y) { | ||
26 | mX = x; | ||
27 | mY = y; | ||
28 | } | ||
29 | |||
30 | /** | ||
31 | * Adds two coordinates to the vector. | ||
32 | * | ||
33 | * @param x | ||
34 | * Horizontal coordinate to add | ||
35 | * @param y | ||
36 | * Vertical coordinate to add | ||
37 | * @return The result of an addition with two coordinates | ||
38 | */ | ||
39 | |||
40 | public Vector2D add(int x, int y) { | ||
41 | return new Vector2D(mX + x, mY + y); | ||
42 | } | ||
43 | |||
44 | /** | ||
45 | * Adds a vector to the vector. | ||
46 | * | ||
47 | * @param v | ||
48 | * Vector to add | ||
49 | * @return The result of the addition with the vector | ||
50 | */ | ||
51 | |||
52 | public Vector2D add(Vector2D v) { | ||
53 | return add(v.mX, v.mY); | ||
54 | } | ||
55 | |||
56 | /** | ||
57 | * Subtracts two coordinates to the vector. | ||
58 | * | ||
59 | * @param x | ||
60 | * Horizontal coordinate to subtract | ||
61 | * @param y | ||
62 | * Vertical coordinate to subtract | ||
63 | * @return The result of the subtraction with the vector | ||
64 | */ | ||
65 | |||
66 | public Vector2D sub(int x, int y) { | ||
67 | return new Vector2D(mX - x, mY - y); | ||
68 | } | ||
69 | |||
70 | /** | ||
71 | * Subtracts a vector to the vector. | ||
72 | * | ||
73 | * @param v | ||
74 | * Vector to subtract | ||
75 | * @return The result of the subtraction with the vector | ||
76 | */ | ||
77 | |||
78 | public Vector2D sub(Vector2D v) { | ||
79 | return sub(v.mX, v.mY); | ||
80 | } | ||
81 | |||
82 | /** | ||
83 | * Negates the vector. | ||
84 | * | ||
85 | * @return The negated version of the vector | ||
86 | */ | ||
87 | |||
88 | public Vector2D negate() { | ||
89 | return new Vector2D(-mX, -mY); | ||
90 | } | ||
91 | |||
92 | /** | ||
93 | * Multiplies the coordinates of the vector by a scalar. | ||
94 | * | ||
95 | * @param scalar | ||
96 | * Number to multiply the coordinates with | ||
97 | * @return The result of the multiplication with a scalar | ||
98 | */ | ||
99 | |||
100 | public Vector2D mul(int scalar) { | ||
101 | return new Vector2D(scalar * mX, scalar * mY); | ||
102 | } | ||
103 | |||
104 | /** | ||
105 | * Divides the coordinates of the vector by a scalar. | ||
106 | * | ||
107 | * @param scalar | ||
108 | * Number to divide the coordinates with | ||
109 | * @return The result of the division with a scalar | ||
110 | */ | ||
111 | |||
112 | public Vector2D div(int scalar) { | ||
113 | return new Vector2D(scalar / mX, scalar / mY); | ||
114 | } | ||
115 | |||
116 | /** | ||
117 | * Normalizes the vector. | ||
118 | * | ||
119 | * @return The normalized version of the vector | ||
120 | */ | ||
121 | |||
122 | public Vector2D normalize() { | ||
123 | double dist = dist(); | ||
124 | return new Vector2D((int) (mX / dist), (int) (mY / dist)); | ||
125 | } | ||
126 | |||
127 | /** | ||
128 | * The Euclidean distance of the vector. | ||
129 | * | ||
130 | * @return The length of the vector | ||
131 | */ | ||
132 | |||
133 | public double dist() { | ||
134 | return Math.sqrt(mX * mX + mY * mY); | ||
135 | } | ||
136 | |||
137 | /** | ||
138 | * Adds a direction to the vector | ||
139 | * | ||
140 | * @param d | ||
141 | * Direction to add | ||
142 | * @return The result of the addition with the direction | ||
143 | */ | ||
144 | |||
145 | public Vector2D addDirectionTo(Direction d) { | ||
146 | switch (d) { | ||
147 | case UP: | ||
148 | return new Vector2D(mX, mY - 1); | ||
149 | |||
150 | case DOWN: | ||
151 | return new Vector2D(mX, mY + 1); | ||
152 | |||
153 | case LEFT: | ||
154 | return new Vector2D(mX - 1, mY); | ||
155 | |||
156 | case RIGHT: | ||
157 | return new Vector2D(mX + 1, mY); | ||
158 | |||
159 | case NONE: | ||
160 | default: | ||
161 | return new Vector2D(mX, mY); | ||
162 | } | ||
163 | } | ||
164 | |||
165 | /** | ||
166 | * Converts the vector to the closest corresponding direction. | ||
167 | * | ||
168 | * @return The closest direction corresponding to the vector | ||
169 | */ | ||
170 | |||
171 | public Direction toDirection() { | ||
172 | Vector2D normal = this.normalize(); | ||
173 | |||
174 | if (normal.mX == 0 && normal.mY == 1) { | ||
175 | return Direction.DOWN; | ||
176 | } else if (normal.mX == 0 && normal.mY == -1) { | ||
177 | return Direction.UP; | ||
178 | } else if (normal.mX == 1 && normal.mY == 0) { | ||
179 | return Direction.RIGHT; | ||
180 | } else if (normal.mX == -1 && normal.mY == 0) { | ||
181 | return Direction.LEFT; | ||
182 | } else { | ||
183 | return Direction.NONE; | ||
184 | } | ||
185 | } | ||
186 | |||
187 | /** | ||
188 | * Returns the horizontal coordinate of the vector. | ||
189 | * | ||
190 | * @return x-coordinate of the vector | ||
191 | */ | ||
192 | |||
193 | public int getX() { | ||
194 | return mX; | ||
195 | } | ||
196 | |||
197 | /** | ||
198 | * Returns the vertical coordinate of the vector. | ||
199 | * | ||
200 | * @return y-coordinate of the vector | ||
201 | */ | ||
202 | |||
203 | public int getY() { | ||
204 | return mY; | ||
205 | } | ||
206 | |||
207 | @Override | ||
208 | public String toString() { | ||
209 | return "(" + mX + ", " + mY + ")"; | ||
210 | } | ||
211 | |||
212 | @Override | ||
213 | public int hashCode() { | ||
214 | return mX * SHIFT + mY; | ||
215 | } | ||
216 | |||
217 | @Override | ||
218 | public boolean equals(Object o) { | ||
219 | if (o == null) { | ||
220 | return false; | ||
221 | } | ||
222 | if (o.getClass() != this.getClass()) { | ||
223 | return false; | ||
224 | } | ||
225 | |||
226 | return o.hashCode() == this.hashCode(); | ||
227 | } | ||
228 | } | ||