|
1 | 1 | package com.group.golf.math;
|
2 | 2 |
|
| 3 | +/** |
| 4 | + * A class to represent a vector in R2 (2D space) |
| 5 | + * @author Julian Marrades |
| 6 | + */ |
3 | 7 | public class JVector2 {
|
4 | 8 |
|
5 | 9 | private float x;
|
6 | 10 | private float y;
|
7 | 11 | private float magnitude;
|
8 | 12 |
|
| 13 | + /** |
| 14 | + * Create a new instance of JVector2 |
| 15 | + * @param x the x-component |
| 16 | + * @param y the y-component |
| 17 | + */ |
9 | 18 | public JVector2(float x, float y) {
|
10 | 19 | this.x = x;
|
11 | 20 | this.y = y;
|
12 | 21 | this.updateMagnitude();
|
13 | 22 | }
|
14 | 23 |
|
| 24 | + /** |
| 25 | + * Create a new instace of JVector2 from another instance |
| 26 | + * @param other the JVector2 template |
| 27 | + */ |
15 | 28 | public JVector2(JVector2 other) {
|
16 | 29 | this.x = other.x;
|
17 | 30 | this.y = other.y;
|
18 | 31 | this.updateMagnitude();
|
19 | 32 | }
|
20 | 33 |
|
| 34 | + /** |
| 35 | + * Update the magnitude using the components |
| 36 | + */ |
21 | 37 | private void updateMagnitude() {
|
22 | 38 | this.magnitude = (float)Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
|
23 | 39 | }
|
24 | 40 |
|
| 41 | + /** |
| 42 | + * Get access to the x-component |
| 43 | + * @return the x-component |
| 44 | + */ |
25 | 45 | public float getX() {
|
26 | 46 | return x;
|
27 | 47 | }
|
28 | 48 |
|
29 |
| - public float floatX() { |
30 |
| - return (float) this.x; |
31 |
| - } |
32 |
| - |
| 49 | + /** |
| 50 | + * Get access to the x-component truncated as an integer |
| 51 | + * @return the integer value of the x-component |
| 52 | + */ |
33 | 53 | public int intX() {
|
34 | 54 | return (int) x;
|
35 | 55 | }
|
36 | 56 |
|
| 57 | + /** |
| 58 | + * Get access to the y-component |
| 59 | + * @return the y-component |
| 60 | + */ |
37 | 61 | public float getY() {
|
38 | 62 | return y;
|
39 | 63 | }
|
40 | 64 |
|
41 |
| - public float floatY() { |
42 |
| - return (float) this.y; |
43 |
| - } |
44 |
| - |
| 65 | + /** |
| 66 | + * Get access to the y-component truncated as an integer |
| 67 | + * @return the integer value of the y-component |
| 68 | + */ |
45 | 69 | public int intY() {
|
46 | 70 | return (int) y;
|
47 | 71 | }
|
48 | 72 |
|
| 73 | + /** |
| 74 | + * Get the magnitute of the vector |
| 75 | + * @return the magnitude of the vector |
| 76 | + */ |
49 | 77 | public float getMagnitude() {
|
50 | 78 | return this.magnitude;
|
51 | 79 | }
|
52 | 80 |
|
| 81 | + /** |
| 82 | + * Scale (mulitiply) the vector |
| 83 | + * @param scalar the scalar value to apply |
| 84 | + */ |
53 | 85 | public void multiply(float scalar) {
|
54 | 86 | x = x * scalar;
|
55 | 87 | y = y * scalar;
|
56 | 88 | magnitude = magnitude * scalar;
|
57 | 89 | }
|
58 | 90 |
|
| 91 | + /** |
| 92 | + * Scale (divide) the vector |
| 93 | + * @param scalar the scalar value to apply |
| 94 | + */ |
59 | 95 | public void divide(float scalar) {
|
60 | 96 | x = x / scalar;
|
61 | 97 | y = y / scalar;
|
62 | 98 | magnitude = magnitude / scalar;
|
63 | 99 | }
|
64 | 100 |
|
| 101 | + /** |
| 102 | + * Normalize the vector, set its magnitude to 1 |
| 103 | + */ |
65 | 104 | public void normalize() {
|
66 | 105 | x = x / magnitude;
|
67 | 106 | y = y / magnitude;
|
68 | 107 | magnitude = 1;
|
69 | 108 | }
|
70 | 109 |
|
| 110 | + /** |
| 111 | + * Set a new magnitude for the vector |
| 112 | + * @param number the new magnitude |
| 113 | + */ |
71 | 114 | public void setMagnitude(float number) {
|
72 | 115 | this.normalize();
|
73 | 116 | this.multiply(number);
|
74 | 117 | }
|
75 | 118 |
|
| 119 | + /** |
| 120 | + * Limit the magnitude of the vector |
| 121 | + * @param value upper limit for the magnitude |
| 122 | + */ |
76 | 123 | public void limit(float value) {
|
77 | 124 | if (this.getMagnitude() > value) {
|
78 | 125 | this.setMagnitude(value);
|
79 | 126 | }
|
80 | 127 | }
|
81 | 128 |
|
| 129 | + /** |
| 130 | + * Swap the components of the vector |
| 131 | + */ |
82 | 132 | public void swap() {
|
83 | 133 | float tmp = this.x;
|
84 | 134 | this.x = this.y;
|
85 | 135 | this.y = tmp;
|
86 | 136 | }
|
87 | 137 |
|
| 138 | + /** |
| 139 | + * Set a new position for the vector |
| 140 | + * @param x the new x-coordinate |
| 141 | + * @param y the new y-coordinate |
| 142 | + */ |
88 | 143 | public void setPosition(float x, float y) {
|
89 | 144 | this.x = x; this.y = y;
|
90 | 145 | }
|
91 | 146 |
|
| 147 | + /** |
| 148 | + * Invert a vector |
| 149 | + * @param vector the vector to invert |
| 150 | + * @return the a new instance with the swapped components of vector |
| 151 | + */ |
92 | 152 | public static JVector2 opposite(JVector2 vector) {
|
93 | 153 | float invX = vector.y;
|
94 | 154 | float invY = vector.x;
|
95 | 155 | return new JVector2(invX, invY);
|
96 | 156 | }
|
97 | 157 |
|
| 158 | + /** |
| 159 | + * Perform vector sum |
| 160 | + * @param vector the vector to add to the current instance |
| 161 | + */ |
98 | 162 | public void add(JVector2 vector) {
|
99 | 163 | x += vector.x;
|
100 | 164 | y += vector.y;
|
101 |
| - magnitude = getMagnitude(); |
| 165 | + this.updateMagnitude(); |
102 | 166 | }
|
103 | 167 |
|
| 168 | + /** |
| 169 | + * Perform vector subtraction |
| 170 | + * @param vector the vector to subtract to the current instance |
| 171 | + */ |
104 | 172 | public void sub(JVector2 vector) {
|
105 | 173 | x -= vector.x;
|
106 | 174 | y -= vector.y;
|
107 |
| - magnitude = getMagnitude(); |
| 175 | + this.updateMagnitude(); |
108 | 176 | }
|
109 | 177 |
|
| 178 | + /** |
| 179 | + * Static vector sum |
| 180 | + * @param vector1 the first vector |
| 181 | + * @param vector2 the second vector |
| 182 | + * @return a vector which is the sum of vector1 and vector2 |
| 183 | + */ |
110 | 184 | public static JVector2 add2Vecs(JVector2 vector1, JVector2 vector2) {
|
111 | 185 | float sumX = vector1.getX() + vector2.getX();
|
112 | 186 | float sumY = vector1.getY() + vector2.getY();
|
113 | 187 | return new JVector2(sumX, sumY);
|
114 | 188 | }
|
115 | 189 |
|
| 190 | + /** |
| 191 | + * Static vector subtraction |
| 192 | + * @param vector1 the first vector |
| 193 | + * @param vector2 the second vector |
| 194 | + * @return a vector which is the subtraction of vector2 to vector1 |
| 195 | + */ |
116 | 196 | public static JVector2 sub2Vecs(JVector2 vector1, JVector2 vector2) {
|
117 | 197 | float subX = vector1.getX() - vector2.getX();
|
118 | 198 | float subY = vector1.getY() - vector2.getY();
|
119 | 199 | return new JVector2(subX, subY);
|
120 | 200 | }
|
121 | 201 |
|
| 202 | + /** |
| 203 | + * Euclidean distance between the tips of two vectors |
| 204 | + * @param vector1 the first vector |
| 205 | + * @param vector2 the second vector |
| 206 | + * @return euclidean distance between the tips of vector1 and vector2 |
| 207 | + */ |
122 | 208 | public static float dist2Vecs(JVector2 vector1, JVector2 vector2) {
|
123 | 209 | float intervalX = vector2.getX() - vector1.getX();
|
124 | 210 | float intervalY = vector2.getY() - vector1.getY();
|
125 | 211 | float distance = (float)Math.sqrt(Math.pow(intervalX, 2) + Math.pow(intervalY, 2));
|
126 | 212 | return distance;
|
127 | 213 | }
|
128 | 214 |
|
| 215 | + /** |
| 216 | + * Euclidean distance between two points in R2 |
| 217 | + * @param x1 first x-coordinate |
| 218 | + * @param y1 first y-coordinate |
| 219 | + * @param x2 second x-coordinate |
| 220 | + * @param y2 second y-coordinate |
| 221 | + * @return euclidean distance between (x1, y1) and (x2, y2) |
| 222 | + */ |
129 | 223 | public static float dist(float x1, float y1, float x2, float y2) {
|
130 | 224 | float intervalX = x2 - x1;
|
131 | 225 | float intervalY = y2 - y1;
|
132 | 226 | float distance = (float)Math.sqrt(Math.pow(intervalX, 2) + Math.pow(intervalY, 2));
|
133 | 227 | return distance;
|
134 | 228 | }
|
135 | 229 |
|
| 230 | + /** |
| 231 | + * Dot product of two vectors |
| 232 | + * @param vector1 the first vector |
| 233 | + * @param vector2 the second vector |
| 234 | + * @return the dot product of vector1 and vector 2 |
| 235 | + */ |
136 | 236 | public static float dotProduct(JVector2 vector1, JVector2 vector2) {
|
137 | 237 | float result = vector1.getX() * vector2.getX() + vector1.getY() * vector2.getY();
|
138 | 238 | return result;
|
139 | 239 | }
|
140 | 240 |
|
| 241 | + /** |
| 242 | + * Euclidean squared distance between two points in R2 |
| 243 | + * @param x1 first x-coordinate |
| 244 | + * @param y1 first y-coordinate |
| 245 | + * @param x2 second x-coordinate |
| 246 | + * @param y2 second y-coordinate |
| 247 | + * @return euclidean squared distance between (x1, y1) and (x2, y2) |
| 248 | + */ |
141 | 249 | public static float dist2(float x1, float y1, float x2, float y2) {
|
142 | 250 | float intervalX = x2 - x1;
|
143 | 251 | float intervalY = y2 - y1;
|
144 | 252 | float distance = (float)(Math.pow(intervalX, 2) + Math.pow(intervalY, 2));
|
145 | 253 | return distance;
|
146 | 254 | }
|
147 | 255 |
|
| 256 | + /** |
| 257 | + * Compute the angle (in radians) between two vectors |
| 258 | + * @param vector1 the first vector |
| 259 | + * @param vector2 the second vector |
| 260 | + * @return the angle between vector1 and vector2 |
| 261 | + */ |
148 | 262 | public static float angleBetween(JVector2 vector1, JVector2 vector2) { //Gives the angle in radians
|
149 | 263 | float topPart = dotProduct(vector1, vector2);
|
150 | 264 | float botPart = vector1.getMagnitude() * vector2.getMagnitude();
|
|
0 commit comments