# Developers Planet

Donovan Phoenix February 2016

### Always return a CCW rotation from a 3 point arc method

I'm trying to create 3 point arcs from a table into a autocad (R12/lt2) DXF file. the problem comes is that the file seems to ALWAYS save arcs in a CCW rotation. so the method below works properly if the pont data i feed it is CCW but does not work if the rotation is CW the arc's angles gets 'flipped'.I can't seem to figure out a way to detect when a CW arc's data is detected to rectify the error

The method calculates the arc with 3 points on the arc's circumference (not the start, center and end point)

Here is My method:

``````public static List<double> threePointArch(double startX, double startY, double midX, double midY, double endX, double endY)
{
List<double> returnValues = new List<double>();
//Calculate center point from 3 points on a circle
//calculate Line R
double lineR = (midY - endY) / (midX - endX);
//Calculate Line T
double lineT = (startY - midY) / (startX - midX);
//calculate x Center
double centerX = ((lineR * lineT * (startY - endY)) + (lineR * (midX + startX)) - (lineT * (endX + midX))) / (2 * (lineR - lineT));
//calculate y Center by subsitution
double centerY = ((endY + midY) / 2) + ((centerX - ((endX + midX) / 2)) * (-1 / lineR));
double raduis = Math.Sqrt((startX - centerX) * (startX - centerX) + (startY - centerY) * (startY - centerY));

//Calculate Start Angle
double startAngle = Math.Atan2(startY - centerY, startX - centerX) * (180 / Math.PI);
//Calculate End Angle
double endtAngle = Math.Atan2(endY - centerY, endX - centerX) * (180 / Math.PI);

//if (endtAngle < 0)
//{
//    endtAngle = endtAngle + 360;
//}

return returnValues;
}
``````

I have been trying for 3 weeks now with every fix i could think of, i tried calculatin

Spektre March 2016

I assume this is in 2D so use cross product to detect CW/CCW

let `p0,p1,p2` be your arcs points as 3D vectors `(x,y,z=0.0)` then:

``````v0=p1-p0
v1=p2-p1
``````

are the edge vectors. now compute the cross product

``````n=cross(v0,v1)
``````

returning normal vector `n` of plane your arc lies on now just test the sign of its `z` coordinate:

``````if (n.z<0.0) CW else CCW
``````

the sign depends on the configuration of your coordinate system so if it does not work use

``````if (n.z>0.0) CW else CCW
``````

if `(n.z==0.0)` then you are in trouble and try to avoid that at all costs by dividing such entities to 2 arc's.