/** * Calculate the centroid of a polygon defined by its vertices * @param vertices Array of polygon vertices * @return Vector2 containing x and y coordinates of the centroid */ private Vector2 calculatePolygonCentroid(float[] vertices) { Vector2 polygonCentroid = new Vector2(); GeometryUtils.polygonCentroid(vertices, 0, vertices.length, polygonCentroid); return polygonCentroid; }
public TrapOriginator(TrapLocation trapLocation, float[] vertices) { this(trapLocation.getInternalId(), trapLocation.getTrap().getType().getName(), trapLocation.getMap(), trapLocation); isTrapLocation = true; Vector2 polygonCenter = GeometryUtils.polygonCentroid(vertices, 0, vertices.length, MathUtil.getVector2()); position().set(polygonCenter); MathUtil.freeVector2(polygonCenter); }
static public void ensureClockWise (float[] polygon, int offset, int count) { if (GeometryUtils.isClockwise(polygon, offset, count)) return; int lastX = offset + count - 2; for (int i = offset, n = offset + count / 2; i < n; i += 2) { int other = lastX - i; float x = polygon[i]; float y = polygon[i + 1]; polygon[i] = polygon[other]; polygon[i + 1] = polygon[other + 1]; polygon[other] = x; polygon[other + 1] = y; } }
@Override public float getCenterX() { if(centroidDirty) { GeometryUtils.polygonCentroid(vertices, 0, vertices.length, centroid); centroidDirty = false; } return centroid.x; }
@Override public float getCenterY() { if(centroidDirty) { GeometryUtils.polygonCentroid(vertices, 0, vertices.length, centroid); centroidDirty = false; } return centroid.y; }