package com.badlogic.gdx.math;
+/** @author Nathan Sweet */
public class GeometryUtils {
+ static private final Vector2 tmp1 = new Vector2(), tmp2 = new Vector2(), tmp3 = new Vector2();
+
/** Computes the barycentric coordinates v,w for the specified point in the triangle.
+ * <p>
+ * If barycentric.x >= 0 && barycentric.y >= 0 && barycentric.x + barycentric.y <= 1 then the point is inside the triangle.
+ * <p>
+ * If vertices a,b,c have values aa,bb,cc then to get an interpolated value at point p:
+ *
+ * <pre>
+ * GeometryUtils.barycentric(p, a, b, c, barycentric);
+ * float u = 1.f - barycentric.x - barycentric.y;
+ * float x = u * aa.x + barycentric.x * bb.x + barycentric.y * cc.x;
+ * float y = u * aa.y + barycentric.x * bb.y + barycentric.y * cc.y;
+ * </pre>
* @return barycentricOut */
static public Vector2 barycentric (Vector2 p, Vector2 a, Vector2 b, Vector2 c, Vector2 barycentricOut) {
- Vector2 v0 = b.sub(a);
- Vector2 v1 = c.sub(a);
- Vector2 v2 = p.sub(a);
+ Vector2 v0 = tmp1.set(b).sub(a);
+ Vector2 v1 = tmp2.set(c).sub(a);
+ Vector2 v2 = tmp3.set(p).sub(a);
float d00 = v0.dot(v0);
float d01 = v0.dot(v1);
float d11 = v1.dot(v1);
}
/** Returns true if the given point is inside the triangle. */
+ public static boolean isPointInTriangle (Vector2 p, Vector2 a, Vector2 b, Vector2 c) {
+ float px1 = p.x - a.x;
+ float py1 = p.y - a.y;
+ boolean side12 = (b.x - a.x) * py1 - (b.y - a.y) * px1 > 0;
+ if ((c.x - a.x) * py1 - (c.y - a.y) * px1 > 0 == side12) return false;
+ if ((c.x - b.x) * (p.y - b.y) - (c.y - b.y) * (p.x - b.x) > 0 != side12) return false;
+ return true;
+ }
+
+ /** Returns true if the given point is inside the triangle. */
public static boolean isPointInTriangle (float px, float py, float ax, float ay, float bx, float by, float cx, float cy) {
float px1 = px - ax;
float py1 = py - ay;
* (pointX - linePoint1X));
}
- /** Checks whether the given point is in the polygon.
- * @param polygon The polygon vertices passed as an array
- * @param point The point
- * @return true if the point is in the polygon */
- public static boolean isPointInPolygon (Array<Vector2> polygon, Vector2 point) {
- Vector2 lastVertice = polygon.peek();
- boolean oddNodes = false;
- for (int i=0; i<polygon.size; i++) {
- Vector2 vertice = polygon.get(i);
- if (vertice.y < point.y && lastVertice.y >= point.y || lastVertice.y < point.y
- && vertice.y >= point.y) {
- if (vertice.x + (point.y - vertice.y) / (lastVertice.y - vertice.y)
- * (lastVertice.x - vertice.x) < point.x) {
- oddNodes = !oddNodes;
- }
- }
- lastVertice = vertice;
- }
- return oddNodes;
- }
+ /** Checks whether the given point is in the polygon.
+ * @param polygon The polygon vertices passed as an array
+ * @param point The point
+ * @return true if the point is in the polygon */
+ public static boolean isPointInPolygon (Array<Vector2> polygon, Vector2 point) {
+ Vector2 lastVertice = polygon.peek();
+ boolean oddNodes = false;
+ for (int i = 0; i < polygon.size; i++) {
+ Vector2 vertice = polygon.get(i);
+ if (vertice.y < point.y && lastVertice.y >= point.y || lastVertice.y < point.y && vertice.y >= point.y) {
+ if (vertice.x + (point.y - vertice.y) / (lastVertice.y - vertice.y) * (lastVertice.x - vertice.x) < point.x) {
+ oddNodes = !oddNodes;
+ }
+ }
+ lastVertice = vertice;
+ }
+ return oddNodes;
+ }
/** Returns true if the specified point is in the polygon. */
public static boolean isPointInPolygon (float[] polygon, int offset, int count, float x, float y) {