Skip to content

Commit 770ceeb

Browse files
committed
feat: add distance method to Vector3
1 parent b6b45e6 commit 770ceeb

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

src/main/java/com/adventofcode/flashk/common/Vector3.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,16 @@ public static Vector3 transform(Vector3 start, Vector3 end) {
5151

5252
return new Vector3(x,y,z);
5353
}
54-
54+
55+
public static double distance(Vector3 a, Vector3 b) {
56+
57+
int xDiff = b.x - a.x;
58+
int yDiff = b.y - a.y;
59+
int zDiff = b.z - a.z;
60+
61+
return Math.sqrt(Math.pow(xDiff, 2) + Math.pow(yDiff, 2) + Math.pow(zDiff,2));
62+
}
63+
5564
/// Shorthand for `Vector3(-1,0,0)`.
5665
/// @return A unitary vector that points to the left.
5766
public static Vector3 left() {

src/test/java/com/adventofcode/flashk/common/Vector3Test.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,95 @@ void transformStaticTest() {
6969
assertEquals(-1, direction.getZ());
7070
}
7171

72+
@Test
73+
void testDistanceMainAxis() {
74+
75+
// Arrange - X axis
76+
Vector3 a = new Vector3(0, 0, 0);
77+
Vector3 b = new Vector3(5, 0, 0);
78+
double expectedX = 5.0;
79+
80+
// Act & Assert - X Axis
81+
double actualX = Vector3.distance(a, b);
82+
assertEquals(expectedX, actualX);
83+
84+
// Arrange - Y axis
85+
a = new Vector3(1, 1, 1);
86+
b = new Vector3(1, 11, 1);
87+
double expectedY = 10.0;
88+
89+
// Act & Assert - Y axis
90+
double actualY = Vector3.distance(a, b);
91+
assertEquals(expectedY, actualY);
92+
93+
// Arrange - Z axis
94+
Vector3 v1 = new Vector3(2,5,8);
95+
Vector3 v2 = new Vector3(2,5,5);
96+
double expectedZ = 3;
97+
98+
// Act & Assert - Z axis
99+
double actualZ = Vector3.distance(v1, v2);
100+
assertEquals(expectedZ, actualZ);
101+
}
102+
103+
@Test
104+
void testDistance2D() {
105+
// 1. Arrange
106+
Vector3 a = new Vector3(0, 0, 0);
107+
Vector3 b = new Vector3(3, 4, 0);
108+
double expected = 5.0;
109+
110+
// 2. Act
111+
double actual = Vector3.distance(a, b);
112+
113+
// 3. Assert
114+
assertEquals(expected, actual);
115+
}
116+
117+
@Test
118+
void testDistanceSame() {
119+
120+
// 1. Arrange
121+
Vector3 a = new Vector3(100, 50, -25);
122+
Vector3 b = new Vector3(100, 50, -25);
123+
double expected = 0;
124+
125+
// 2. Act
126+
double actual = Vector3.distance(a, b);
127+
128+
// 3. Assert
129+
assertEquals(expected, actual);
130+
}
131+
132+
@Test
133+
void testDistance() {
134+
// 1. Arrange
135+
Vector3 a = new Vector3(2, 8, 3);
136+
Vector3 b = new Vector3(7, 1, 9);
137+
138+
double expected = Math.sqrt(110);
139+
140+
// 2. Act
141+
double actual = Vector3.distance(a, b);
142+
143+
// 3. Assert
144+
assertEquals(expected, actual);
145+
}
146+
147+
@Test
148+
void testDistanceNegatives() {
149+
// 1. Arrange
150+
Vector3 a = new Vector3(1, 1, 1);
151+
Vector3 b = new Vector3(-3, -2, 7);
152+
double expected = Math.sqrt(61);
153+
154+
// 2. Act
155+
double actual = Vector3.distance(a, b);
156+
157+
// 3. Assert
158+
assertEquals(expected, actual);
159+
}
160+
72161
@Test
73162
void leftTest() {
74163

0 commit comments

Comments
 (0)