Skip to content

Commit e2c0752

Browse files
committed
Angle structure and GeometryComparator
1 parent a74dcdf commit e2c0752

15 files changed

Lines changed: 223 additions & 103 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.github.refhumbold.algolib.geometry;
2+
3+
import java.util.Comparator;
4+
5+
public class GeometryComparator
6+
implements Comparator<Double>
7+
{
8+
private static final double EPSILON = 1e-12;
9+
10+
@Override
11+
public int compare(Double d1, Double d2)
12+
{
13+
return Math.abs(d1 - d2) < EPSILON ? 0 : Double.compare(d1, d2);
14+
}
15+
}

src/main/java/com/github/refhumbold/algolib/geometry/GeometryObject.java

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.github.refhumbold.algolib.geometry.dim2;
2+
3+
import java.util.Objects;
4+
import com.github.refhumbold.algolib.geometry.GeometryComparator;
5+
6+
public final class Angle
7+
implements Comparable<Angle>
8+
{
9+
private static final GeometryComparator comparator = new GeometryComparator();
10+
private static final double fullAngleDeg = 360.0;
11+
private final double degrees;
12+
13+
private Angle(double degrees)
14+
{
15+
this.degrees = degrees;
16+
}
17+
18+
public double getDegrees()
19+
{
20+
return degrees;
21+
}
22+
23+
public double getRadians()
24+
{
25+
return Math.toRadians(degrees);
26+
}
27+
28+
public static Angle fromDegrees(double degrees)
29+
{
30+
return new Angle((degrees % fullAngleDeg + fullAngleDeg) % fullAngleDeg);
31+
}
32+
33+
public static Angle fromRadians(double radians)
34+
{
35+
return fromDegrees(Math.toDegrees(radians));
36+
}
37+
38+
@Override
39+
public boolean equals(Object obj)
40+
{
41+
return this == obj
42+
|| obj instanceof Angle other && comparator.compare(degrees, other.degrees) == 0;
43+
}
44+
45+
@Override
46+
public int hashCode()
47+
{
48+
return Objects.hashCode(degrees);
49+
}
50+
51+
@Override
52+
public int compareTo(Angle angle)
53+
{
54+
return angle == null ? 1 : comparator.compare(degrees, angle.degrees);
55+
}
56+
}

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
import java.util.Comparator;
44
import java.util.List;
5+
import com.github.refhumbold.algolib.geometry.GeometryComparator;
56

67
/** Algorithms for basic geometrical operations in 2D. */
78
public final class Geometry2D
89
{
10+
private static final GeometryComparator comparator = new GeometryComparator();
11+
912
/**
1013
* Mutably sorts given points by their X coordinate. Sorting is guaranteed to be stable.
1114
* @param points the points
1215
*/
1316
public static void sortByX(List<Point2D> points)
1417
{
15-
points.sort(Comparator.comparingDouble(pt -> pt.x));
18+
points.sort(Comparator.comparing(pt -> pt.x, comparator));
1619
}
1720

1821
/**
@@ -21,7 +24,7 @@ public static void sortByX(List<Point2D> points)
2124
*/
2225
public static void sortByY(List<Point2D> points)
2326
{
24-
points.sort(Comparator.comparingDouble(pt -> pt.y));
27+
points.sort(Comparator.comparing(pt -> pt.y, comparator));
2528
}
2629

2730
/**
@@ -31,8 +34,19 @@ public static void sortByY(List<Point2D> points)
3134
*/
3235
public static void sortByAngle(List<Point2D> points)
3336
{
34-
points.sort((pt1, pt2) -> pt1.angleDeg() == pt2.angleDeg() ? Double.compare(pt1.radius(),
35-
pt2.radius()) : Double.compare(pt1.angleDeg(), pt2.angleDeg()));
37+
points.sort((pt1, pt2) -> pt1.angle().equals(pt2.angle()) ? comparator.compare(pt1.radius(),
38+
pt2.radius()) : pt1.angle().compareTo(pt2.angle()));
39+
}
40+
41+
/**
42+
* Mutably sorts given points by their polar coordinates around given central point.
43+
* First sorts by angle, then by radius. Sorting is guaranteed to be stable.
44+
* @param points the points
45+
* @param centre the central point
46+
*/
47+
public static void sortByAngleAround(List<Point2D> points, Point2D centre)
48+
{
49+
Vector2D translation = Vector2D.between(centre, Point2D.ZERO);
3650
}
3751

3852
/**

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
package com.github.refhumbold.algolib.geometry.dim2;
22

33
import java.util.Objects;
4-
import com.github.refhumbold.algolib.geometry.GeometryObject;
4+
import com.github.refhumbold.algolib.geometry.GeometryComparator;
55

66
/** Structure of point in 2D. */
77
public final class Point2D
8-
extends GeometryObject
98
{
9+
public static final Point2D ZERO = of(0, 0);
10+
private static final GeometryComparator comparator = new GeometryComparator();
1011
public final double x;
1112
public final double y;
1213

1314
private Point2D(double x, double y)
1415
{
15-
super();
1616
this.x = x;
1717
this.y = y;
1818
}
1919

20-
@Override
2120
public double[] getCoordinates()
2221
{
2322
return new double[]{ x, y };
@@ -31,8 +30,8 @@ public static Point2D of(double x, double y)
3130
@Override
3231
public boolean equals(Object obj)
3332
{
34-
return this == obj || obj instanceof Point2D other && areEqual(x, other.x) && areEqual(y,
35-
other.y);
33+
return this == obj || obj instanceof Point2D other && comparator.compare(x, other.x) == 0
34+
&& comparator.compare(y, other.y) == 0;
3635
}
3736

3837
@Override
@@ -52,15 +51,8 @@ public double radius()
5251
return Math.sqrt(x * x + y * y);
5352
}
5453

55-
public double angleRad()
54+
public Angle angle()
5655
{
57-
return Math.atan2(y, x);
58-
}
59-
60-
public double angleDeg()
61-
{
62-
double ang = angleRad() * 180.0 / Math.PI;
63-
64-
return y >= 0.0 ? ang : ang + 360.0;
56+
return Angle.fromRadians(Math.atan2(y, x));
6557
}
6658
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
package com.github.refhumbold.algolib.geometry.dim2;
22

33
import java.util.Objects;
4-
import com.github.refhumbold.algolib.geometry.GeometryObject;
4+
import com.github.refhumbold.algolib.geometry.GeometryComparator;
55

66
/** Structure of vector in 2D. */
77
public final class Vector2D
8-
extends GeometryObject
98
{
9+
public static final Vector2D ZERO = of(0, 0);
10+
private static final GeometryComparator comparator = new GeometryComparator();
1011
public final double x;
1112
public final double y;
1213

1314
private Vector2D(double x, double y)
1415
{
15-
super();
1616
this.x = x;
1717
this.y = y;
1818
}
1919

20-
@Override
2120
public double[] getCoordinates()
2221
{
2322
return new double[]{ x, y };
@@ -46,8 +45,8 @@ public static double area(Vector2D v1, Vector2D v2)
4645
@Override
4746
public boolean equals(Object obj)
4847
{
49-
return this == obj || obj instanceof Vector2D other && areEqual(x, other.x) && areEqual(y,
50-
other.y);
48+
return this == obj || obj instanceof Vector2D other && comparator.compare(x, other.x) == 0
49+
&& comparator.compare(y, other.y) == 0;
5150
}
5251

5352
@Override

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
import java.util.Comparator;
44
import java.util.List;
5+
import com.github.refhumbold.algolib.geometry.GeometryComparator;
56

67
/** Algorithms for basic geometrical operations in 3D. */
78
public final class Geometry3D
89
{
10+
private static final GeometryComparator comparator = new GeometryComparator();
11+
912
/**
1013
* Mutably sorts given points by their X coordinate. Sorting is guaranteed to be stable.
1114
* @param points the points
1215
*/
1316
public static void sortByX(List<Point3D> points)
1417
{
15-
points.sort(Comparator.comparingDouble(pt -> pt.x));
18+
points.sort(Comparator.comparing(pt -> pt.x, comparator));
1619
}
1720

1821
/**
@@ -21,7 +24,7 @@ public static void sortByX(List<Point3D> points)
2124
*/
2225
public static void sortByY(List<Point3D> points)
2326
{
24-
points.sort(Comparator.comparingDouble(pt -> pt.y));
27+
points.sort(Comparator.comparing(pt -> pt.y, comparator));
2528
}
2629

2730
/**
@@ -30,7 +33,7 @@ public static void sortByY(List<Point3D> points)
3033
*/
3134
public static void sortByZ(List<Point3D> points)
3235
{
33-
points.sort(Comparator.comparingDouble(pt -> pt.z));
36+
points.sort(Comparator.comparing(pt -> pt.z, comparator));
3437
}
3538

3639
/**

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
package com.github.refhumbold.algolib.geometry.dim3;
22

33
import java.util.Objects;
4-
import com.github.refhumbold.algolib.geometry.GeometryObject;
4+
import com.github.refhumbold.algolib.geometry.GeometryComparator;
55

66
/** Structure of point in 3D. */
77
public final class Point3D
8-
extends GeometryObject
98
{
9+
public static final Point3D ZERO = of(0, 0, 0);
10+
private static final GeometryComparator comparator = new GeometryComparator();
1011
public final double x;
1112
public final double y;
1213
public final double z;
1314

1415
private Point3D(double x, double y, double z)
1516
{
16-
super();
1717
this.x = x;
1818
this.y = y;
1919
this.z = z;
2020
}
2121

22-
@Override
2322
public double[] getCoordinates()
2423
{
2524
return new double[]{ x, y, z };
@@ -33,8 +32,8 @@ public static Point3D of(double x, double y, double z)
3332
@Override
3433
public boolean equals(Object obj)
3534
{
36-
return this == obj || obj instanceof Point3D other && areEqual(x, other.x) && areEqual(y,
37-
other.y) && areEqual(z, other.z);
35+
return this == obj || obj instanceof Point3D other && comparator.compare(x, other.x) == 0
36+
&& comparator.compare(y, other.y) == 0 && comparator.compare(z, other.z) == 0;
3837
}
3938

4039
@Override

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
package com.github.refhumbold.algolib.geometry.dim3;
22

33
import java.util.Objects;
4-
import com.github.refhumbold.algolib.geometry.GeometryObject;
4+
import com.github.refhumbold.algolib.geometry.GeometryComparator;
55

66
/** Structure of vector in 3D. */
77
public final class Vector3D
8-
extends GeometryObject
98
{
9+
public static final Vector3D ZERO = of(0, 0, 0);
10+
private static final GeometryComparator comparator = new GeometryComparator();
1011
public final double x;
1112
public final double y;
1213
public final double z;
1314

1415
private Vector3D(double x, double y, double z)
1516
{
16-
super();
1717
this.x = x;
1818
this.y = y;
1919
this.z = z;
2020
}
2121

22-
@Override
2322
public double[] getCoordinates()
2423
{
2524
return new double[]{ x, y, z };
@@ -59,8 +58,8 @@ public static double volume(Vector3D v1, Vector3D v2, Vector3D v3)
5958
@Override
6059
public boolean equals(Object obj)
6160
{
62-
return this == obj || obj instanceof Vector3D other && areEqual(x, other.x) && areEqual(y,
63-
other.y) && areEqual(z, other.z);
61+
return this == obj || obj instanceof Vector3D other && comparator.compare(x, other.x) == 0
62+
&& comparator.compare(y, other.y) == 0 && comparator.compare(z, other.z) == 0;
6463
}
6564

6665
@Override

0 commit comments

Comments
 (0)