public Shape prune() { Polygon result = new Polygon(); for (int i = 0; i < getPointCount(); i++) { int next = i + 1 >= getPointCount() ? 0 : i + 1; int prev = i - 1 < 0 ? getPointCount() - 1 : i - 1; float dx1 = getPoint(i)[0] - getPoint(prev)[0]; float dy1 = getPoint(i)[1] - getPoint(prev)[1]; float dx2 = getPoint(next)[0] - getPoint(i)[0]; float dy2 = getPoint(next)[1] - getPoint(i)[1]; float len1 = MathUtils.sqrt((dx1 * dx1) + (dy1 * dy1)); float len2 = MathUtils.sqrt((dx2 * dx2) + (dy2 * dy2)); dx1 /= len1; dy1 /= len1; dx2 /= len2; dy2 /= len2; if ((dx1 != dx2) || (dy1 != dy2)) { result.addPoint(getPoint(i)[0], getPoint(i)[1]); } } return result; }
protected final synchronized void checkPoints() { if (pointsDirty) { createPoints(); findCenter(); calculateRadius(); if (points == null) { return; } synchronized (points) { final int size = points.length; if (size > 0) { maxX = points[0]; maxY = points[1]; minX = points[0]; minY = points[1]; for (int i = 0; i < size / 2; i++) { maxX = MathUtils.max(points[i * 2], maxX); maxY = MathUtils.max(points[(i * 2) + 1], maxY); minX = MathUtils.min(points[i * 2], minX); minY = MathUtils.min(points[(i * 2) + 1], minY); } } pointsDirty = false; trianglesDirty = true; } } }
private float[] getNormal(float[] start, float[] end) { float dx = start[0] - end[0]; float dy = start[1] - end[1]; float len = MathUtils.sqrt((dx * dx) + (dy * dy)); dx /= len; dy /= len; return new float[] {-dy, dx}; }
protected void calculateRadius() { boundingCircleRadius = 0; for (int i = 0; i < points.length; i += 2) { float temp = ((points[i] - center[0]) * (points[i] - center[0])) + ((points[i + 1] - center[1]) * (points[i + 1] - center[1])); boundingCircleRadius = (boundingCircleRadius > temp) ? boundingCircleRadius : temp; } boundingCircleRadius = MathUtils.sqrt(boundingCircleRadius); }
public float[] getNormal(int index) { float[] current = getPoint(index); float[] prev = getPoint(index - 1 < 0 ? getPointCount() - 1 : index - 1); float[] next = getPoint(index + 1 >= getPointCount() ? 0 : index + 1); float[] t1 = getNormal(prev, current); float[] t2 = getNormal(current, next); if ((index == 0) && (!closed())) { return t2; } if ((index == getPointCount() - 1) && (!closed())) { return t1; } float tx = (t1[0] + t2[0]) / 2; float ty = (t1[1] + t2[1]) / 2; float len = MathUtils.sqrt((tx * tx) + (ty * ty)); return new float[] {tx / len, ty / len}; }
public float length() { return MathUtils.sqrt(x * x + y * y); }