OSDN Git Service

20dc47ce0e3b737ac4580ef58a510d2a1f5acfc2
[mikumikustudio/libgdx-mikumikustudio.git] / gdx / src / com / badlogic / gdx / math / GeometryUtils.java
1
2 package com.badlogic.gdx.math;
3
4 public class GeometryUtils {
5         /** Computes the barycentric coordinates v,w for the specified point in the triangle.
6          * @return barycentricOut */
7         static public Vector2 barycentric (Vector2 p, Vector2 a, Vector2 b, Vector2 c, Vector2 barycentricOut) {
8                 Vector2 v0 = b.sub(a);
9                 Vector2 v1 = c.sub(a);
10                 Vector2 v2 = p.sub(a);
11                 float d00 = v0.dot(v0);
12                 float d01 = v0.dot(v1);
13                 float d11 = v1.dot(v1);
14                 float d20 = v2.dot(v0);
15                 float d21 = v2.dot(v1);
16                 float denom = d00 * d11 - d01 * d01;
17                 barycentricOut.x = (d11 * d20 - d01 * d21) / denom;
18                 barycentricOut.y = (d00 * d21 - d01 * d20) / denom;
19                 return barycentricOut;
20         }
21
22         /** Returns the lowest positive root of the quadric equation given by a* x * x + b * x + c = 0. If no solution is given
23          * Float.Nan is returned.
24          * @param a the first coefficient of the quadric equation
25          * @param b the second coefficient of the quadric equation
26          * @param c the third coefficient of the quadric equation
27          * @return the lowest positive root or Float.Nan */
28         static public float lowestPositiveRoot (float a, float b, float c) {
29                 float det = b * b - 4 * a * c;
30                 if (det < 0) return Float.NaN;
31
32                 float sqrtD = (float)Math.sqrt(det);
33                 float invA = 1 / (2 * a);
34                 float r1 = (-b - sqrtD) * invA;
35                 float r2 = (-b + sqrtD) * invA;
36
37                 if (r1 > r2) {
38                         float tmp = r2;
39                         r2 = r1;
40                         r1 = tmp;
41                 }
42
43                 if (r1 > 0) return r1;
44                 if (r2 > 0) return r2;
45                 return Float.NaN;
46         }
47
48         public static Vector2 triangleCentroid (float x1, float y1, float x2, float y2, float x3, float y3, Vector2 centroid) {
49                 centroid.x = (x1 + x2 + x3) / 3;
50                 centroid.y = (y1 + y2 + y3) / 3;
51                 return centroid;
52         }
53
54         public static Vector2 quadrilateralCentroid (float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4,
55                 Vector2 centroid) {
56                 float avgX1 = (x1 + x2 + x3) / 3;
57                 float avgY1 = (y1 + y2 + y3) / 3;
58                 float avgX2 = (x1 + x4 + x3) / 3;
59                 float avgY2 = (y1 + y4 + y3) / 3;
60                 centroid.x = avgX1 - (avgX1 - avgX2) / 2;
61                 centroid.y = avgY1 - (avgY1 - avgY2) / 2;
62                 return centroid;
63         }
64
65         /** Returns the centroid for the specified non-self-intersecting polygon. */
66         public static Vector2 polygonCentroid (float[] polygon, int offset, int count, Vector2 centroid) {
67                 if (polygon.length < 6) throw new IllegalArgumentException("A polygon must have 3 or more coordinate pairs.");
68                 float x = 0, y = 0;
69
70                 float signedArea = 0;
71                 int i = offset;
72                 for (int n = offset + count - 2; i < n; i += 2) {
73                         float x0 = polygon[i];
74                         float y0 = polygon[i + 1];
75                         float x1 = polygon[i + 2];
76                         float y1 = polygon[i + 3];
77                         float a = x0 * y1 - x1 * y0;
78                         signedArea += a;
79                         x += (x0 + x1) * a;
80                         y += (y0 + y1) * a;
81                 }
82
83                 float x0 = polygon[i];
84                 float y0 = polygon[i + 1];
85                 float x1 = polygon[offset];
86                 float y1 = polygon[offset + 1];
87                 float a = x0 * y1 - x1 * y0;
88                 signedArea += a;
89                 x += (x0 + x1) * a;
90                 y += (y0 + y1) * a;
91
92                 signedArea *= 0.5f;
93                 centroid.x = x / (6 * signedArea);
94                 centroid.y = y / (6 * signedArea);
95                 return centroid;
96         }
97 }