Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix is convex negated #241

Merged
merged 1 commit into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,20 @@ public static Polygon createPolygon(LinearRing exteriorRing, List<LinearRing> ri
return JTS_FACTORY.createPolygon(exteriorRing, rings.toArray(LinearRing[]::new));
}


/**
* Returns {@code true} if the signed area of the triangle formed by 3 sequential points changes sign anywhere along
* Returns {@code false} if the signed area of the triangle formed by 3 sequential points changes sign anywhere along
* {@code ring}, ignoring repeated and collinear points.
*/
public static boolean isConvex(LinearRing ring) {
return !isConcave(ring);
}

/**
* Returns {@code true} if the signed area of the triangle formed by 3 sequential points changes sign anywhere along
* {@code ring}, ignoring repeated and collinear points.
*/
public static boolean isConcave(LinearRing ring) {
CoordinateSequence seq = ring.getCoordinateSequence();
if (seq.size() <= 3) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void testMetersPerPixel(int zoom, double meters) {

@Test
void testIsConvexTriangle() {
assertConvex(true, newLinearRing(
assertConcave(true, newLinearRing(
0, 0,
1, 0,
0, 1,
Expand All @@ -127,7 +127,7 @@ void testIsConvexTriangle() {

@Test
void testIsConvexRectangle() {
assertConvex(true, newLinearRing(
assertConcave(true, newLinearRing(
0, 0,
1, 0,
1, 1,
Expand All @@ -138,21 +138,21 @@ void testIsConvexRectangle() {

@Test
void testBarelyConvexRectangle() {
assertConvex(true, newLinearRing(
assertConcave(true, newLinearRing(
0, 0,
1, 0,
1, 1,
0.5, 0.5,
0, 0
));
assertConvex(true, newLinearRing(
assertConcave(true, newLinearRing(
0, 0,
1, 0,
1, 1,
0.4, 0.4,
0, 0
));
assertConvex(true, newLinearRing(
assertConcave(true, newLinearRing(
0, 0,
1, 0,
1, 1,
Expand All @@ -163,39 +163,39 @@ void testBarelyConvexRectangle() {

@Test
void testConcaveRectangleDoublePoints() {
assertConvex(true, newLinearRing(
assertConcave(true, newLinearRing(
0, 0,
0, 0,
1, 0,
1, 1,
0, 1,
0, 0
));
assertConvex(true, newLinearRing(
assertConcave(true, newLinearRing(
0, 0,
1, 0,
1, 0,
1, 1,
0, 1,
0, 0
));
assertConvex(true, newLinearRing(
assertConcave(true, newLinearRing(
0, 0,
1, 0,
1, 1,
1, 1,
0, 1,
0, 0
));
assertConvex(true, newLinearRing(
assertConcave(true, newLinearRing(
0, 0,
1, 0,
1, 1,
0, 1,
0, 1,
0, 0
));
assertConvex(true, newLinearRing(
assertConcave(true, newLinearRing(
0, 0,
1, 0,
1, 1,
Expand All @@ -207,7 +207,7 @@ void testConcaveRectangleDoublePoints() {

@Test
void testBarelyConcaveRectangle() {
assertConvex(false, newLinearRing(
assertConcave(false, newLinearRing(
0, 0,
1, 0,
1, 1,
Expand All @@ -218,31 +218,31 @@ void testBarelyConcaveRectangle() {

@Test
void test5PointsConcave() {
assertConvex(false, newLinearRing(
assertConcave(false, newLinearRing(
0, 0,
0.5, 0.1,
1, 0,
1, 1,
0, 1,
0, 0
));
assertConvex(false, newLinearRing(
assertConcave(false, newLinearRing(
0, 0,
1, 0,
0.9, 0.5,
1, 1,
0, 1,
0, 0
));
assertConvex(false, newLinearRing(
assertConcave(false, newLinearRing(
0, 0,
1, 0,
1, 1,
0.5, 0.9,
0, 1,
0, 0
));
assertConvex(false, newLinearRing(
assertConcave(false, newLinearRing(
0, 0,
1, 0,
1, 1,
Expand All @@ -254,31 +254,31 @@ void test5PointsConcave() {

@Test
void test5PointsColinear() {
assertConvex(true, newLinearRing(
assertConcave(true, newLinearRing(
0, 0,
0.5, 0,
1, 0,
1, 1,
0, 1,
0, 0
));
assertConvex(true, newLinearRing(
assertConcave(true, newLinearRing(
0, 0,
1, 0,
1, 0.5,
1, 1,
0, 1,
0, 0
));
assertConvex(true, newLinearRing(
assertConcave(true, newLinearRing(
0, 0,
1, 0,
1, 1,
0.5, 1,
0, 1,
0, 0
));
assertConvex(true, newLinearRing(
assertConcave(true, newLinearRing(
0, 0,
1, 0,
1, 1,
Expand All @@ -288,14 +288,17 @@ void test5PointsColinear() {
));
}

private static void assertConvex(boolean isConvex, LinearRing ring) {
private static void assertConcave(boolean isConcave, LinearRing ring) {
for (double rotation : new double[]{0, 90, 180, 270}) {
LinearRing rotated = (LinearRing) AffineTransformation.rotationInstance(Math.toRadians(rotation)).transform(ring);
for (boolean flip : new boolean[]{false, true}) {
LinearRing flipped = flip ? (LinearRing) AffineTransformation.scaleInstance(-1, 1).transform(rotated) : rotated;
for (boolean reverse : new boolean[]{false, true}) {
LinearRing reversed = reverse ? flipped.reverse() : flipped;
assertEquals(isConvex, isConvex(reversed), "rotation=" + rotation + " flip=" + flip + " reverse=" + reverse);
assertEquals(isConcave, isConcave(reversed),
"rotation=" + rotation + " flip=" + flip + " reverse=" + reverse);
assertEquals(!isConcave, isConvex(reversed),
"rotation=" + rotation + " flip=" + flip + " reverse=" + reverse);
}
}
}
Expand Down