Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

clockwise or counterclockwise 2

Status
Not open for further replies.

dan2

Programmer
Mar 13, 2000
16
US
I'm parsing points to generate a gcode program. When I encounter arcs I need to determine wether the points are given in a clockwise or counterwise direction.

I'm looking for an easier way to determine this than determining the quadrant of a portion of the span, then comparing x&y changes relative the center point.

Any suggestions.
 
Maybe a pre-parser that orders the points along the line of the cut?
 
I don't know exactly what kind of data do you have to analyze the curvature of the arc.

If you have 3 consecutive points lying on the arc, then you can use the determinant expansion to find the area of the triangle formed by these three points (lying in x-y plane). If the points lie in clockwise direction, the area is positive. If points lie in counter-clockwise direction, the area is negative. If points lie on a straight line, the area is 0.

Below is the formula for area of the triangle.
[tt]
1 |x1 y1 1|
A = --- |x2 y2 1|
2 |x3 y3 1|
[/tt]
The determinant on the right can be expanded like this.
[tt]
A = 1 / 2 * ((x1 * y2 + x2 * y3 + x3 * y1) - (x1 * y3 + x2 * y1 + x3 * y2))
[/tt]
Based on the value of A, you can determine whether the arc is clockwise or counter-clockwise.

For example the points (0,0), (1,0) and (1,1) give an area of 0.5 indicating that the arc is clockwise. If the third point is changed to (1,-1), the area becomes -0.5, indicating a counter-clockwise arc.
 
Stealing from Hypetia
Code:
Dim ClockWise As Boolean
ClockWise = (x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2)) > 0


[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
Oh, I confused myself in clockwise and counter-clockwise!

Positive area indicates a counter-clockwise direction.
Negative area indicates a clockwise direction.
 
Awesome! Thanks! Much easier to code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top