Clockwise-p
Function Syntax | (LM:Clockwise-p <p1> <p2> <p3>) |
Current Version | 1.0 |
Donate |
Arguments | ||
---|---|---|
Symbol | Type | Description |
p1, p2, p3 | List | Points to test |
Returns | ||
Type | Description | |
Boolean | Returns T if the path through p1, p2, & p3 follows a clockwise direction. |
Function Description
Here I present a few variations of a predicate function to determine whether three supplied points are oriented clockwise, i.e. whether the path through points p1, p2, & p3 follows a clockwise direction.
Version 1
This solution defines a coordinate system with normal vector in the direction of the vector formed by (p3, p1). If the point p1 has a greater x-value than that of p2 relative to this coordinate system, the points are clockwise oriented.
;; Clockwise-p - Lee Mac ;; Returns T if p1,p2,p3 are clockwise oriented (defun LM:Clockwise-p ( p1 p2 p3 ) ((lambda ( n ) (< (car (trans p2 0 n)) (car (trans p1 0 n)))) (mapcar '- p1 p3)) )
Version 2
This solution uses an expression relating to the direction of the cross-product of the vectors formed by (p1, p2) and (p1, p3).
;; Clockwise-p - Lee Mac ;; Returns T if p1,p2,p3 are clockwise oriented (defun LM:Clockwise-p ( p1 p2 p3 ) (< (* (- (car p2) (car p1)) (- (cadr p3) (cadr p1))) (* (- (cadr p2) (cadr p1)) (- (car p3) (car p1))) ) )
Version 3
This elegant solution by Gilles Chanteau (gile) uses the left-hand angle formed by the three points: if this angle is greater than pi radians, the sin of the angle will be negative - indicating the points are clockwise.
;; Clockwise-p - gile ;; Returns T if p1,p2,p3 are clockwise oriented (defun gc:clockwise-p ( p1 p2 p3 ) (< (sin (- (angle p1 p3) (angle p1 p2))) -1e-14) )