I have 4 points definiing a simple quad(2D), but i need to order them in a counter clockwise format in order to render them properly
Paul
I have 4 points definiing a simple quad(2D), but i need to order them in a counter clockwise format in order to render them properly
Paul
so, what’s the question?
I made the following code to make sure a Polygon has its array of points listed in an anti-clockwise direction. Basically it uses the fact that any polygon of n-points should have an inner angle sum of (n - 2)*Math.PI.
It uses three methods. I made this when i was in yr 10 school so it really isn’t fast, but it works :). If you want to see the full code see the net.phys2d.raw.shapes.KPolygon class in the source here: http://www.freewebs.com/commanderkeith/PolygonPhysicsSource.zip
public Point2D.Float[] points;
public void ensureAntiClockwisePath()
{
// to establish whether or not the polygon path is anticlockwise or not.
// proper angle sum
float properAngleSum = (float)((points.length - 2)*Math.PI);
// angle sum assuming the points are listed in anti-clockwise direction
float angleSum = 0;
Point2D.Float currentPoint;
for (int i = 0; i < points.length; i++)
{
angleSum += findAngle(i, points);
}
// reverses the order of the polygon so that the points go anti-clockwise
// if they weren't doing that already.
// System.out.println("properAngleSum = " + properAngleSum);
// System.out.println("angleSum = " + angleSum);
if (!(angleSum > (properAngleSum - 0.001f) && angleSum < (properAngleSum + 0.001f)))
{
// System.out.println("reversing");
Point2D.Float[] reversed = new Point2D.Float[points.length];
int countDown = points.length-1;
for (int i = 0; i < points.length; i++)
{
reversed[countDown] = points[i];
countDown--;
}
points = reversed;
}
}
public float findAngle(int i, Point2D.Float[] decreasingPoints)
{
// this only works where the polygon has an anti-clockwise path.
// returns angle in radians, not degrees.
float currentAngle;
float nextPointAngle;
float prevPointAngle;
prevPointAngle = findAngle(i, (i - 1 < 0 ? decreasingPoints.length-1 : i-1), decreasingPoints);
nextPointAngle = findAngle(i, (i + 1 > decreasingPoints.length-1 ? 0 : i+1), decreasingPoints);
currentAngle = prevPointAngle - nextPointAngle;
if (currentAngle < 0)
{
currentAngle += (float)(2*Math.PI);
}
return currentAngle;
}
public float findAngle(int start, int dest, Point2D.Float[] decreasingPoints)
{
// returns angle that dest is relative to start, measured anti-clockwise from the x-axis
// returns angle in radians, not degrees.
float angle;
float x;
float y;
x = decreasingPoints[dest].x - decreasingPoints[start].x;
y = decreasingPoints[dest].y - decreasingPoints[start].y;
if (x == 0.0f)
{
return (y > 0 ? (float)(Math.PI/2) : (float)(Math.PI*3/2));
}
angle = (float)(Math.atan(y/x) + (x < 0 ? Math.PI : 0));
if (angle < 0)
{
angle += (float)2*Math.PI;
}
return angle;
}
Keith
pretty funny jar:) been playing with it for a half hour
glad u like it!
Its part of the 2D Physics System that Kev knocked up… http://www.java-gaming.org/forums/index.php?topic=14461.90
Its under development, but slowly…