ArcCAM

 From:  Michael Gibson
11543.50 In reply to 11543.49 
Hi Len,

re:
> Next question... can I do a similar calculation for clockwise/counterclockwise on a whole
> closed curve by picking 3 points along the curve, or will complex curves be problematic?

Unlike an arc, a general spline curve can have inflections in it so just 3 points isn't enough.

But if you evaluate a bunch of points along the curve so you have a closed polygon you can calculate the signed area of a 2D closed polygon like this:

code:
var area = 0;
var array_of_points = [];

// Get points to make a closed 2D polygon.

var numpoints = array_of_points.length;

for ( var i = 0; i < numpoints-1; i++ )
{
	var this_pt = array_of_points[i];
	var next_pt = array_of_points[i+1];

	area += this_pt.x * next_pt.y;
	area -= this_pt.y * next_pt.x;
}

// One more span to close it off, it is ok if the first and last point are coincident.

var this_pt = array_of_points[numpoints-1];
var next_pt = array_of_points[0];

area += this_pt.x * next_pt.y;
area -= this_pt.y * next_pt.x;

if area < 0 it's clockwise

if area > 0 it's counter-clockwise


- Michael