Skip to content

Commit cda848c

Browse files
committed
Tests for points
1 parent 03be09d commit cda848c

8 files changed

Lines changed: 293 additions & 64 deletions

File tree

src/main/java/com/github/refhumbold/algolib/geometry/dim2/Point2D.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,24 @@ private Point2D(double x, double y)
1818
this.y = y;
1919
}
2020

21-
public double[] getCoordinates()
21+
public static Point2D of(double x, double y)
2222
{
23-
return new double[]{ x, y };
23+
return new Point2D(x, y);
2424
}
2525

26-
public static Point2D of(double x, double y)
26+
public double[] coordinates()
2727
{
28-
return new Point2D(x, y);
28+
return new double[]{x, y};
29+
}
30+
31+
public double radius()
32+
{
33+
return Math.sqrt(x * x + y * y);
34+
}
35+
36+
public Angle angle()
37+
{
38+
return Angle.fromRadians(Math.atan2(y, x));
2939
}
3040

3141
@Override
@@ -48,14 +58,4 @@ public String toString()
4858

4959
return "(%s, %s)".formatted(df.format(x), df.format(y));
5060
}
51-
52-
public double radius()
53-
{
54-
return Math.sqrt(x * x + y * y);
55-
}
56-
57-
public Angle angle()
58-
{
59-
return Angle.fromRadians(Math.atan2(y, x));
60-
}
6161
}

src/main/java/com/github/refhumbold/algolib/geometry/dim2/Vector2D.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ private Vector2D(double x, double y)
1818
this.y = y;
1919
}
2020

21-
public double[] getCoordinates()
22-
{
23-
return new double[]{ x, y };
24-
}
25-
2621
public static Vector2D of(double x, double y)
2722
{
2823
return new Vector2D(x, y);
@@ -43,6 +38,11 @@ public static double area(Vector2D v1, Vector2D v2)
4338
return v1.x * v2.y - v1.y * v2.x;
4439
}
4540

41+
public double[] coordinates()
42+
{
43+
return new double[]{x, y};
44+
}
45+
4646
@Override
4747
public boolean equals(Object obj)
4848
{

src/main/java/com/github/refhumbold/algolib/geometry/dim3/Point3D.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@ private Point3D(double x, double y, double z)
2020
this.z = z;
2121
}
2222

23-
public double[] getCoordinates()
23+
public static Point3D of(double x, double y, double z)
2424
{
25-
return new double[]{ x, y, z };
25+
return new Point3D(x, y, z);
2626
}
2727

28-
public static Point3D of(double x, double y, double z)
28+
public double[] coordinates()
2929
{
30-
return new Point3D(x, y, z);
30+
return new double[]{x, y, z};
31+
}
32+
33+
public double radius()
34+
{
35+
return Math.sqrt(x * x + y * y + z * z);
3136
}
3237

3338
@Override
@@ -50,9 +55,4 @@ public String toString()
5055

5156
return "(%s, %s, %s)".formatted(df.format(x), df.format(y), df.format(z));
5257
}
53-
54-
public double radius()
55-
{
56-
return Math.sqrt(x * x + y * y + z * z);
57-
}
5858
}

src/main/java/com/github/refhumbold/algolib/geometry/dim3/Vector3D.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ private Vector3D(double x, double y, double z)
2020
this.z = z;
2121
}
2222

23-
public double[] getCoordinates()
24-
{
25-
return new double[]{ x, y, z };
26-
}
27-
2823
public static Vector3D of(double x, double y, double z)
2924
{
3025
return new Vector3D(x, y, z);
@@ -56,6 +51,11 @@ public static double volume(Vector3D v1, Vector3D v2, Vector3D v3)
5651
return dot(v1, cross(v2, v3));
5752
}
5853

54+
public double[] coordinates()
55+
{
56+
return new double[]{x, y, z};
57+
}
58+
5959
@Override
6060
public boolean equals(Object obj)
6161
{
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.github.refhumbold.algolib.geometry.dim2;
2+
3+
import java.util.stream.Stream;
4+
import org.assertj.core.api.Assertions;
5+
import org.assertj.core.data.Offset;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
public class Point2DTest
12+
{
13+
private static final Offset<Double> OFFSET = Offset.offset(1e-12);
14+
15+
@Test
16+
public void coordinates_ThenArray()
17+
{
18+
// when
19+
double[] result = Point2D.of(150.123456789, -3700.987654321).coordinates();
20+
21+
// then
22+
Assertions.assertThat(result).isEqualTo(new double[]{150.123456789, -3700.987654321});
23+
}
24+
25+
@ParameterizedTest
26+
@MethodSource("paramsFor_angle")
27+
public void angle_ThenCounterClockwiseAngleFromXAxis(Point2D point, double expected)
28+
{
29+
// when
30+
Angle result = point.angle();
31+
32+
// then
33+
Assertions.assertThat(result).isEqualTo(Angle.fromDegrees(expected));
34+
}
35+
36+
@ParameterizedTest
37+
@MethodSource("paramsFor_radius")
38+
public void radius_ThenDistanceFromZeroPoint(Point2D point, double expected)
39+
{
40+
// when
41+
double result = point.radius();
42+
43+
// then
44+
Assertions.assertThat(result).isCloseTo(expected, OFFSET);
45+
}
46+
47+
@Test
48+
public void toString_ThenStringRepresentation()
49+
{
50+
// when
51+
String result = Point2D.of(150.123456789, -3700.987654321).toString();
52+
53+
// then
54+
Assertions.assertThat(result).isEqualTo("(150.123456789, -3700.987654321)");
55+
}
56+
57+
private static Stream<Arguments> paramsFor_angle()
58+
{
59+
return Stream.of(Arguments.of(Point2D.ZERO, 0.0), Arguments.of(Point2D.of(7.0, 0.0), 0.0),
60+
Arguments.of(Point2D.of(7.0, 7.0), 45.0), Arguments.of(Point2D.of(0.0, 7.0), 90.0),
61+
Arguments.of(Point2D.of(-7.0, 7.0), 135.0),
62+
Arguments.of(Point2D.of(-7.0, 0.0), 180.0),
63+
Arguments.of(Point2D.of(-7.0, -7.0), 225.0),
64+
Arguments.of(Point2D.of(0.0, -7.0), 270.0),
65+
Arguments.of(Point2D.of(7.0, -7.0), 315.0));
66+
}
67+
68+
private static Stream<Arguments> paramsFor_radius()
69+
{
70+
return Stream.of(Arguments.of(Point2D.ZERO, 0.0), Arguments.of(Point2D.of(14.0, 0.0), 14.0),
71+
Arguments.of(Point2D.of(-14.0, 0.0), 14.0),
72+
Arguments.of(Point2D.of(0.0, 14.0), 14.0),
73+
Arguments.of(Point2D.of(0.0, -14.0), 14.0),
74+
Arguments.of(Point2D.of(8.0, 6.0), 10.0), Arguments.of(Point2D.of(8.0, -6.0), 10.0),
75+
Arguments.of(Point2D.of(-8.0, 6.0), 10.0),
76+
Arguments.of(Point2D.of(-8.0, -6.0), 10.0));
77+
}
78+
}

src/test/java/com/github/refhumbold/algolib/geometry/dim2/Vector2DTest.java

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,47 @@
11
package com.github.refhumbold.algolib.geometry.dim2;
22

3+
import java.util.stream.Stream;
34
import org.assertj.core.api.Assertions;
45
import org.assertj.core.data.Offset;
56
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
610

711
// Tests: Structure of vector in 2D.
812
public class Vector2DTest
913
{
1014
private static final Offset<Double> OFFSET = Offset.offset(1e-12);
1115

1216
@Test
13-
public void between_ThenVectorFromBeginToEnd()
17+
public void coordinates_ThenArray()
1418
{
1519
// when
16-
Vector2D result = Vector2D.between(Point2D.of(2.4, 7.8), Point2D.of(-1.5, 13.2));
20+
double[] result = Vector2D.of(150.123456789, -3700.987654321).coordinates();
1721

1822
// then
19-
Assertions.assertThat(result).isEqualTo(Vector2D.of(-3.9, 5.4));
23+
Assertions.assertThat(result).containsExactly(150.123456789, -3700.987654321);
24+
}
25+
26+
@ParameterizedTest
27+
@MethodSource("paramsFor_length")
28+
public void length_ThenLengthOfVector()
29+
{
30+
// when
31+
double result = Vector2D.of(8.0, -6.0).length();
32+
33+
// then
34+
Assertions.assertThat(result).isCloseTo(10.0, OFFSET);
2035
}
2136

2237
@Test
23-
public void getCoordinates_ThenArray()
38+
public void between_ThenVectorFromBeginToEnd()
2439
{
2540
// when
26-
double[] result = Vector2D.of(5.0, -19.0).getCoordinates();
41+
Vector2D result = Vector2D.between(Point2D.of(2.4, 7.8), Point2D.of(-1.5, 13.2));
2742

2843
// then
29-
Assertions.assertThat(result).containsExactly(5.0, -19.0);
44+
Assertions.assertThat(result).isEqualTo(Vector2D.of(-3.9, 5.4));
3045
}
3146

3247
@Test
@@ -69,16 +84,6 @@ public void area_WhenParallel_ThenZero()
6984
Assertions.assertThat(result).isCloseTo(0.0, OFFSET);
7085
}
7186

72-
@Test
73-
public void length_ThenLengthOfVector()
74-
{
75-
// when
76-
double result = Vector2D.of(8.0, -6.0).length();
77-
78-
// then
79-
Assertions.assertThat(result).isCloseTo(10.0, OFFSET);
80-
}
81-
8287
@Test
8388
public void negate_ThenNegateEachCoordinate()
8489
{
@@ -126,7 +131,7 @@ public void multiply_WhenMultiplicationByZero_ThenZeroVector()
126131
Vector2D result = Vector2D.of(5.4, 9.0).multiply(0);
127132

128133
// then
129-
Assertions.assertThat(result).isEqualTo(Vector2D.of(0, 0));
134+
Assertions.assertThat(result).isEqualTo(Vector2D.ZERO);
130135
}
131136

132137
@Test
@@ -145,4 +150,27 @@ public void divide_WhenDivisionByZero_ThenArithmeticException()
145150
Assertions.assertThatThrownBy(() -> Vector2D.of(1.0, 1.0).divide(0))
146151
.isInstanceOf(ArithmeticException.class);
147152
}
153+
154+
@Test
155+
public void toString_ThenStringRepresentation()
156+
{
157+
// when
158+
String result = Vector2D.of(150.123456789, -3700.987654321).toString();
159+
160+
// then
161+
Assertions.assertThat(result).isEqualTo("[150.123456789, -3700.987654321]");
162+
}
163+
164+
private static Stream<Arguments> paramsFor_length()
165+
{
166+
return Stream.of(Arguments.of(Vector2D.ZERO, 0.0),
167+
Arguments.of(Vector2D.of(14.0, 0.0), 14.0),
168+
Arguments.of(Vector2D.of(-14.0, 0.0), 14.0),
169+
Arguments.of(Vector2D.of(0.0, 14.0), 14.0),
170+
Arguments.of(Vector2D.of(0.0, -14.0), 14.0),
171+
Arguments.of(Vector2D.of(8.0, 6.0), 10.0),
172+
Arguments.of(Vector2D.of(8.0, -6.0), 10.0),
173+
Arguments.of(Vector2D.of(-8.0, 6.0), 10.0),
174+
Arguments.of(Vector2D.of(-8.0, -6.0), 10.0));
175+
}
148176
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.github.refhumbold.algolib.geometry.dim3;
2+
3+
import java.util.stream.Stream;
4+
import org.assertj.core.api.Assertions;
5+
import org.assertj.core.data.Offset;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
public class Point3DTest
12+
{
13+
private static final Offset<Double> OFFSET = Offset.offset(1e-12);
14+
15+
@Test
16+
public void coordinates_ThenArray()
17+
{
18+
// when
19+
double[] result = Point3D.of(150.123456789, -3700.987654321, 0.55555555).coordinates();
20+
21+
// then
22+
Assertions.assertThat(result)
23+
.isEqualTo(new double[]{150.123456789, -3700.987654321, 0.55555555});
24+
}
25+
26+
@ParameterizedTest
27+
@MethodSource("paramsFor_radius")
28+
public void radius_ThenDistanceFromZeroPoint(Point3D point, double expected)
29+
{
30+
// when
31+
double result = point.radius();
32+
33+
// then
34+
Assertions.assertThat(result).isCloseTo(expected, OFFSET);
35+
}
36+
37+
@Test
38+
public void toString_ThenStringRepresentation()
39+
{
40+
// when
41+
String result = Point3D.of(150.123456789, -3700.987654321, 0.55555555).toString();
42+
43+
// then
44+
Assertions.assertThat(result).isEqualTo("(150.123456789, -3700.987654321, 0.55555555)");
45+
}
46+
47+
private static Stream<Arguments> paramsFor_radius()
48+
{
49+
return Stream.of(Arguments.of(Point3D.ZERO, 0.0),
50+
Arguments.of(Point3D.of(14.0, 0.0, 0.0), 14.0),
51+
Arguments.of(Point3D.of(-14.0, 0.0, 0.0), 14.0),
52+
Arguments.of(Point3D.of(0.0, 14.0, 0.0), 14.0),
53+
Arguments.of(Point3D.of(0.0, -14.0, 0.0), 14.0),
54+
Arguments.of(Point3D.of(0.0, 0.0, 14.0), 14.0),
55+
Arguments.of(Point3D.of(0.0, 0.0, -14.0), 14.0),
56+
Arguments.of(Point3D.of(8.0, 6.0, 0.0), 10.0),
57+
Arguments.of(Point3D.of(8.0, -6.0, 0.0), 10.0),
58+
Arguments.of(Point3D.of(-8.0, 6.0, 0.0), 10.0),
59+
Arguments.of(Point3D.of(-8.0, -6.0, 0.0), 10.0),
60+
Arguments.of(Point3D.of(8.0, 0.0, 6.0), 10.0),
61+
Arguments.of(Point3D.of(8.0, 0.0, -6.0), 10.0),
62+
Arguments.of(Point3D.of(-8.0, 0.0, 6.0), 10.0),
63+
Arguments.of(Point3D.of(-8.0, 0.0, -6.0), 10.0),
64+
Arguments.of(Point3D.of(0.0, 8.0, 6.0), 10.0),
65+
Arguments.of(Point3D.of(0.0, 8.0, -6.0), 10.0),
66+
Arguments.of(Point3D.of(0.0, -8.0, 6.0), 10.0),
67+
Arguments.of(Point3D.of(0.0, -8.0, -6.0), 10.0),
68+
Arguments.of(Point3D.of(18.0, 6.0, 13.0), 23.0),
69+
Arguments.of(Point3D.of(18.0, 6.0, -13.0), 23.0),
70+
Arguments.of(Point3D.of(18.0, -6.0, 13.0), 23.0),
71+
Arguments.of(Point3D.of(18.0, -6.0, -13.0), 23.0),
72+
Arguments.of(Point3D.of(-18.0, 6.0, 13.0), 23.0),
73+
Arguments.of(Point3D.of(-18.0, 6.0, -13.0), 23.0),
74+
Arguments.of(Point3D.of(-18.0, -6.0, 13.0), 23.0),
75+
Arguments.of(Point3D.of(-18.0, -6.0, -13.0), 23.0));
76+
}
77+
}

0 commit comments

Comments
 (0)