2 package com.badlogic.gdx.math;
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) {
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;
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;
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;
43 if (r1 > 0) return r1;
44 if (r2 > 0) return r2;
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;
54 public static Vector2 quadrilateralCentroid (float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4,
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;