From 16132f7ae3114d0104096b7f2191a2eaa0c01516 Mon Sep 17 00:00:00 2001 From: Gustl22 Date: Tue, 19 Feb 2019 11:51:02 +0100 Subject: [PATCH] GeometryUtils: Fix reverse clockwise check --- .../test/org/oscim/utils/GeometryTest.java | 45 +++++++++++++++++++ .../org/oscim/utils/geom/GeometryUtils.java | 15 +------ 2 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 vtm-tests/test/org/oscim/utils/GeometryTest.java diff --git a/vtm-tests/test/org/oscim/utils/GeometryTest.java b/vtm-tests/test/org/oscim/utils/GeometryTest.java new file mode 100644 index 00000000..522dded0 --- /dev/null +++ b/vtm-tests/test/org/oscim/utils/GeometryTest.java @@ -0,0 +1,45 @@ +package org.oscim.utils; + +import org.junit.Assert; +import org.junit.Test; +import org.oscim.utils.geom.GeometryUtils; + +public class GeometryTest { + + @Test + public void testIsTrisClockwise() { + float[] pA = new float[]{0, 0}; + float[] pB = new float[]{1, 1}; + float[] pC = new float[]{1, 0}; + + float area = GeometryUtils.isTrisClockwise(pA, pB, pC); + Assert.assertTrue(area > 0); + + area = GeometryUtils.isTrisClockwise(pA, pC, pB); + Assert.assertTrue(area < 0); + } + + @Test + public void testIsClockwise() { + float[] points = new float[]{0, 0, 1, 1, 1, 0}; + + float area = GeometryUtils.isClockwise(points, points.length); + Assert.assertTrue(area > 0); + + points = new float[]{0, 0, 1, 0, 1, 1}; + area = GeometryUtils.isClockwise(points, points.length); + Assert.assertTrue(area < 0); + } + + @Test + public void testDotProduct() { + float[] p = {-1, 0, 0, 0, 0, 0}; + + for (int i = 0; i < 9; i++) { + p[4] = (float) Math.cos(Math.toRadians(i * 45)); + p[5] = (float) Math.sin(Math.toRadians(i * 45)); + System.out.println("\n> " + (i * 45) + " " + p[3] + ":" + p[4] + "\n=" + + GeometryUtils.dotProduct(p, 0, 2, 4)); + } + } +} diff --git a/vtm/src/org/oscim/utils/geom/GeometryUtils.java b/vtm/src/org/oscim/utils/geom/GeometryUtils.java index ec4e5006..e7ced26f 100644 --- a/vtm/src/org/oscim/utils/geom/GeometryUtils.java +++ b/vtm/src/org/oscim/utils/geom/GeometryUtils.java @@ -322,9 +322,9 @@ public final class GeometryUtils { public static float isClockwise(float[] points, int n) { float area = 0f; for (int i = 0; i < n - 2; i += 2) { - area += (points[i] * points[i + 3]) - (points[i + 1] * points[i + 2]); + area += (points[i + 1] * points[i + 2]) - (points[i] * points[i + 3]); } - area += (points[n - 2] * points[1]) - (points[n - 1] * points[0]); + area += (points[n - 1] * points[0]) - (points[n - 2] * points[1]); return 0.5f * area; } @@ -400,15 +400,4 @@ public final class GeometryUtils { } return scaled; } - - public static void main(String[] args) { - float[] p = {-1, 0, 0, 0, 0, 0}; - - for (int i = 0; i < 9; i++) { - p[4] = (float) Math.cos(Math.toRadians(i * 45)); - p[5] = (float) Math.sin(Math.toRadians(i * 45)); - System.out.println("\n> " + (i * 45) + " " + p[3] + ":" + p[4] + "\n=" - + dotProduct(p, 0, 2, 4)); - } - } }