OSDN Git Service

Added GeometryUtils so we don't stick everything in Intersector.
[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 getLowestPositiveRoot (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 }