OSDN Git Service

Optimized intersectLinePolygon. Added intersectSegmentPolygon.
authorNathanSweet <nathan.sweet@gmail.com>
Sat, 17 Aug 2013 16:02:35 +0000 (18:02 +0200)
committerNathanSweet <nathan.sweet@gmail.com>
Sat, 17 Aug 2013 16:02:35 +0000 (18:02 +0200)
gdx/src/com/badlogic/gdx/math/Intersector.java

index 55f20e0..5d96a4a 100644 (file)
@@ -639,31 +639,56 @@ public final class Intersector {
        }
 
        /** Check whether the given line and {@link Polygon} intersect.
-        * 
         * @param p1 The first point of the line
         * @param p2 The second point of the line
         * @param polygon The polygon
-        * @return Whether polygon and line intersects
-        */
-       public static boolean intersectLinePolygon(Vector2 p1, Vector2 p2, Polygon polygon) {
+        * @return Whether polygon and line intersects */
+       public static boolean intersectLinePolygon (Vector2 p1, Vector2 p2, Polygon polygon) {
                float[] vertices = polygon.getTransformedVertices();
-               int i = 0;
                float x1 = p1.x, y1 = p1.y, x2 = p2.x, y2 = p2.y;
-               float det1 = det(x1, y1, x2, y2);
-               while (i < vertices.length - 2) {
-                       float x3 = vertices[i], y3 = vertices[i + 1], x4 = vertices[(i + 2)], y4 = vertices[(i + 3)];
-
-                       float det2 = det(x3, y3, x4, y4);
-                       float det3 = det(x1 - x2, y1 - y2, x3 - x4, y3 - y4);
-
-                       float x = det(det1, x1 - x2, det2, x3 - x4) / det3;
-                       float y = det(det1, y1 - y2, det2, y3 - y4) / det3;
-
-                       if (((x >= x3 && x <= x4) || (x >= x4 && x <= x3))
-                                       && ((y >= y3 && y <= y4) || (y >= y4 && y <= y3)))
-                               return true;
+               float width12 = x1 - x2, height12 = y1 - y2;
+               float det1 = x1 * y2 - y1 * x2;
+               int n = vertices.length;
+               float x3 = vertices[n - 2], y3 = vertices[n - 1];
+               for (int i = 0; i < n; i += 2) {
+                       float x4 = vertices[i], y4 = vertices[i + 1];
+                       float det2 = x3 * y4 - y3 * x4;
+                       float width34 = x3 - x4, height34 = y3 - y4;
+                       float det3 = width12 * height34 - height12 * width34;
+                       float x = (det1 * width34 - width12 * det2) / det3;
+                       if ((x >= x3 && x <= x4) || (x >= x4 && x <= x3)) {
+                               float y = (det1 * height34 - height12 * det2) / det3;
+                               if ((y >= y3 && y <= y4) || (y >= y4 && y <= y3)) return true;
+                       }
+                       x3 = x4;
+                       y3 = y4;
+               }
+               return false;
+       }
 
-                       i += 2;
+       /** Check whether the given line segment and {@link Polygon} intersect.
+        * @param p1 The first point of the segment
+        * @param p2 The second point of the segment
+        * @return Whether polygon and line intersects */
+       public static boolean intersectSegmentPolygon (Vector2 p1, Vector2 p2, Polygon polygon) {
+               float[] vertices = polygon.getTransformedVertices();
+               float x1 = p1.x, y1 = p1.y, x2 = p2.x, y2 = p2.y;
+               float width12 = x1 - x2, height12 = y1 - y2;
+               float det1 = x1 * y2 - y1 * x2;
+               int n = vertices.length;
+               float x3 = vertices[n - 2], y3 = vertices[n - 1];
+               for (int i = 0; i < n; i += 2) {
+                       float x4 = vertices[i], y4 = vertices[i + 1];
+                       float det2 = x3 * y4 - y3 * x4;
+                       float width34 = x3 - x4, height34 = y3 - y4;
+                       float det3 = width12 * height34 - height12 * width34;
+                       float x = (det1 * width34 - width12 * det2) / det3;
+                       if (((x >= x3 && x <= x4) || (x >= x4 && x <= x3)) && ((x >= x1 && x <= x2) || (x >= x2 && x <= x1))) {
+                               float y = (det1 * height34 - height12 * det2) / det3;
+                               if (((y >= y3 && y <= y4) || (y >= y4 && y <= y3)) && ((y >= y1 && y <= y2) || (y >= y2 && y <= y1))) return true;
+                       }
+                       x3 = x4;
+                       y3 = y4;
                }
                return false;
        }