summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/util/Vector2D.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl/maze/util/Vector2D.java')
-rw-r--r--src/ch/epfl/maze/util/Vector2D.java426
1 files changed, 207 insertions, 219 deletions
diff --git a/src/ch/epfl/maze/util/Vector2D.java b/src/ch/epfl/maze/util/Vector2D.java
index 5e4b975..66b0216 100644
--- a/src/ch/epfl/maze/util/Vector2D.java
+++ b/src/ch/epfl/maze/util/Vector2D.java
@@ -2,227 +2,215 @@ package ch.epfl.maze.util;
2 2
3/** 3/**
4 * Immutable 2-dimensional vector (<i>x</i>, <i>y</i>). 4 * Immutable 2-dimensional vector (<i>x</i>, <i>y</i>).
5 *
6 */ 5 */
7 6
8public final class Vector2D { 7public final class Vector2D {
9 8
10 /* shift constant to compute the hash */ 9 /* shift constant to compute the hash */
11 private static final int SHIFT = 1000; 10 private static final int SHIFT = 1000;
12 11
13 /* 2-dimension coordinates */ 12 /* 2-dimension coordinates */
14 private final int mX, mY; 13 private final int mX, mY;
15 14
16 /** 15 /**
17 * Constructs a 2-dimensional vector. 16 * Constructs a 2-dimensional vector.
18 * 17 *
19 * @param x 18 * @param x Horizontal coordinate
20 * Horizontal coordinate 19 * @param y Vertical coordinate
21 * @param y 20 */
22 * Vertical coordinate 21
23 */ 22 public Vector2D(int x, int y) {
24 23 mX = x;
25 public Vector2D(int x, int y) { 24 mY = y;
26 mX = x; 25 }
27 mY = y; 26
28 } 27 /**
29 28 * Adds two coordinates to the vector.
30 /** 29 *
31 * Adds two coordinates to the vector. 30 * @param x Horizontal coordinate to add
32 * 31 * @param y Vertical coordinate to add
33 * @param x 32 * @return The result of an addition with two coordinates
34 * Horizontal coordinate to add 33 */
35 * @param y 34
36 * Vertical coordinate to add 35 public Vector2D add(int x, int y) {
37 * @return The result of an addition with two coordinates 36 return new Vector2D(mX + x, mY + y);
38 */ 37 }
39 38
40 public Vector2D add(int x, int y) { 39 /**
41 return new Vector2D(mX + x, mY + y); 40 * Adds a vector to the vector.
42 } 41 *
43 42 * @param v Vector to add
44 /** 43 * @return The result of the addition with the vector
45 * Adds a vector to the vector. 44 */
46 * 45
47 * @param v 46 public Vector2D add(Vector2D v) {
48 * Vector to add 47 return add(v.mX, v.mY);
49 * @return The result of the addition with the vector 48 }
50 */ 49
51 50 /**
52 public Vector2D add(Vector2D v) { 51 * Subtracts two coordinates to the vector.
53 return add(v.mX, v.mY); 52 *
54 } 53 * @param x Horizontal coordinate to subtract
55 54 * @param y Vertical coordinate to subtract
56 /** 55 * @return The result of the subtraction with the vector
57 * Subtracts two coordinates to the vector. 56 */
58 * 57
59 * @param x 58 public Vector2D sub(int x, int y) {
60 * Horizontal coordinate to subtract 59 return new Vector2D(mX - x, mY - y);
61 * @param y 60 }
62 * Vertical coordinate to subtract 61
63 * @return The result of the subtraction with the vector 62 /**
64 */ 63 * Subtracts a vector to the vector.
65 64 *
66 public Vector2D sub(int x, int y) { 65 * @param v Vector to subtract
67 return new Vector2D(mX - x, mY - y); 66 * @return The result of the subtraction with the vector
68 } 67 */
69 68
70 /** 69 public Vector2D sub(Vector2D v) {
71 * Subtracts a vector to the vector. 70 return sub(v.mX, v.mY);
72 * 71 }
73 * @param v 72
74 * Vector to subtract 73 /**
75 * @return The result of the subtraction with the vector 74 * Negates the vector.
76 */ 75 *
77 76 * @return The negated version of the vector
78 public Vector2D sub(Vector2D v) { 77 */
79 return sub(v.mX, v.mY); 78
80 } 79 public Vector2D negate() {
81 80 return new Vector2D(-mX, -mY);
82 /** 81 }
83 * Negates the vector. 82
84 * 83 /**
85 * @return The negated version of the vector 84 * Multiplies the coordinates of the vector by a scalar.
86 */ 85 *
87 86 * @param scalar Number to multiply the coordinates with
88 public Vector2D negate() { 87 * @return The result of the multiplication with a scalar
89 return new Vector2D(-mX, -mY); 88 */
90 } 89
91 90 public Vector2D mul(int scalar) {
92 /** 91 return new Vector2D(scalar * mX, scalar * mY);
93 * Multiplies the coordinates of the vector by a scalar. 92 }
94 * 93
95 * @param scalar 94 /**
96 * Number to multiply the coordinates with 95 * Divides the coordinates of the vector by a scalar.
97 * @return The result of the multiplication with a scalar 96 *
98 */ 97 * @param scalar Number to divide the coordinates with
99 98 * @return The result of the division with a scalar
100 public Vector2D mul(int scalar) { 99 */
101 return new Vector2D(scalar * mX, scalar * mY); 100
102 } 101 public Vector2D div(int scalar) {
103 102 return new Vector2D(scalar / mX, scalar / mY);
104 /** 103 }
105 * Divides the coordinates of the vector by a scalar. 104
106 * 105 /**
107 * @param scalar 106 * Normalizes the vector.
108 * Number to divide the coordinates with 107 *
109 * @return The result of the division with a scalar 108 * @return The normalized version of the vector
110 */ 109 */
111 110
112 public Vector2D div(int scalar) { 111 public Vector2D normalize() {
113 return new Vector2D(scalar / mX, scalar / mY); 112 double dist = dist();
114 } 113 return new Vector2D((int) (mX / dist), (int) (mY / dist));
115 114 }
116 /** 115
117 * Normalizes the vector. 116 /**
118 * 117 * The Euclidean distance of the vector.
119 * @return The normalized version of the vector 118 *
120 */ 119 * @return The length of the vector
121 120 */
122 public Vector2D normalize() { 121
123 double dist = dist(); 122 public double dist() {
124 return new Vector2D((int) (mX / dist), (int) (mY / dist)); 123 return Math.sqrt(mX * mX + mY * mY);
125 } 124 }
126 125
127 /** 126 /**
128 * The Euclidean distance of the vector. 127 * Adds a direction to the vector
129 * 128 *
130 * @return The length of the vector 129 * @param d Direction to add
131 */ 130 * @return The result of the addition with the direction
132 131 */
133 public double dist() { 132
134 return Math.sqrt(mX * mX + mY * mY); 133 public Vector2D addDirectionTo(Direction d) {
135 } 134 switch (d) {
136 135 case UP:
137 /** 136 return new Vector2D(mX, mY - 1);
138 * Adds a direction to the vector 137
139 * 138 case DOWN:
140 * @param d 139 return new Vector2D(mX, mY + 1);
141 * Direction to add 140
142 * @return The result of the addition with the direction 141 case LEFT:
143 */ 142 return new Vector2D(mX - 1, mY);
144 143
145 public Vector2D addDirectionTo(Direction d) { 144 case RIGHT:
146 switch (d) { 145 return new Vector2D(mX + 1, mY);
147 case UP: 146
148 return new Vector2D(mX, mY - 1); 147 case NONE:
149 148 default:
150 case DOWN: 149 return new Vector2D(mX, mY);
151 return new Vector2D(mX, mY + 1); 150 }
152 151 }
153 case LEFT: 152