Little function that I think the need probably frequently arises for, and is not part of v1.4.3. Needed it for a drawing app - thought I might as well post it here.
Code:
//wrapCoordsCC - orders arbitrarily ordered coords counterclockwise about specified origin point.
//coords - vertices as array of (world) coords (b2Vec2's).
//origin - a point (b2Vec2) within the coords (i.e. average x, average y).
//modifies passed array.
function wrapCoordsCC(coords:Array, origin:b2Vec2):void {
var nTheta:Number;
var arySorted:Array = new Array();
var ii:int;
//pair angle of parametric form with rect coords in new array
for(ii = 0; ii < coords.length; ii++) {
nTheta = Math.atan2(coords[ii].x - origin.x, coords[ii].y - origin.y);
arySorted.push({theta:nTheta, xRect:coords[ii].x, yRect:coords[ii].y});
}
//sort by angle
arySorted = arySorted.sortOn("theta", Array.NUMERIC);
//copy rect coords back to passed array
for(ii = 0; ii < coords.length; ii++) { //get angle of parametric form
coords[ii] = new b2Vec2(arySorted[ii].xRect, arySorted[ii].yRect);
}
}
//coords - vertices as array of (world) coords (b2Vec2's).
//origin - a point (b2Vec2) within the coords (i.e. average x, average y).
//modifies passed array.
function wrapCoordsCC(coords:Array, origin:b2Vec2):void {
var nTheta:Number;
var arySorted:Array = new Array();
var ii:int;
//pair angle of parametric form with rect coords in new array
for(ii = 0; ii < coords.length; ii++) {
nTheta = Math.atan2(coords[ii].x - origin.x, coords[ii].y - origin.y);
arySorted.push({theta:nTheta, xRect:coords[ii].x, yRect:coords[ii].y});
}
//sort by angle
arySorted = arySorted.sortOn("theta", Array.NUMERIC);
//copy rect coords back to passed array
for(ii = 0; ii < coords.length; ii++) { //get angle of parametric form
coords[ii] = new b2Vec2(arySorted[ii].xRect, arySorted[ii].yRect);
}
}