Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Active Contributor
leipogs23
Posts: 34
Registered: ‎08-15-2008
Message 1 of 14 (2,139 Views)

polyline clockwise or counter clockwise

2139 Views, 13 Replies
03-23-2010 10:26 PM
How can i test a polyline if its clockwise or counterclockwise?
I have a routine that gets the area of polylines then if clockwise,the area is positive and negative if not. But I dont want to get the area just to test if its clockwise or not.
Any idea?
*Laurie
Message 2 of 14 (2,139 Views)

Re: polyline clockwise or counter clockwise

03-23-2010 11:56 PM in reply to: leipogs23
Hi,

Compute the areas from the coordinates. Positive areas will be
clockwise, negative anti-clockwise.
For each leg: area = (end X - start X) * (End Y + Start Y) /2

Another approach is to offset the polyline be a small distance and
compare the areas of the original and the offset.

Here's an extract from my data of material by gille using a third approach

{code}
;; PolylineAlgebricArea (gile)
;; Returns the algebric (signed) area of a polyline (even with arcs)
;; Area is negative if the polyline is clockwise
;;
;; Argument: a polyline (ename)
;; Posted to NG 18 Mar 2009 by _gille

(defun PolylineAlgebricArea (pl / elst lst tot p0 area)
(setq elst (entget pl))
(while (setq elst (member (assoc 10 elst) elst))
(setq lst (cons (cons (cdar elst) (cdr (cadddr elst))) lst)
elst (cdr elst)
)
)
(setq lst (reverse lst)
tot 0.0
p0 (caar lst)
)
(if (/= 0 (cdar lst))
(setq tot (PolyArcArea (cdar lst) p0 (caadr lst)))
)
(if (equal (car (last lst)) p0 1e-9)
(setq lst (reverse (cdr (reverse lst))))
)
(setq lst (cdr lst))
(while (cadr lst)
(setq area (TriangleArea p0 (caar lst) (caadr lst))
tot (+ area tot)
)
(if (/= 0 (cdar lst))
(setq tot (+ tot (PolyArcArea (cdar lst) (caar lst) (caadr lst))))
)
(setq lst (cdr lst))
)
(if (/= 0 (cdar lst))
(setq tot (+ tot (PolyArcArea (cdar lst) (caar lst) p0)))
)
tot
)

;; TriangleArea (gile)
;; Returns the algebric (signed) area of a triangle (3 points)
;; Area is negative if the points are clockwise
;;
;; Arguments: three 2d points

(defun TriangleArea (p1 p2 p3)
(/ (- (* (- (car p2) (car p1))
(- (cadr p3) (cadr p1))
)
(* (- (car p3) (car p1))
(- (cadr p2) (cadr p1))
)
)
2.0
)
)

;; PolyArcArea (gile)
;; Returns the algebric (signed) area of a polyarc
;; Area is negative if the arc is clockwise
;;
;; Arguments
;; bu : polyarc bulge
;; p1 : start point
;; p2 : end point

(defun PolyArcArea (bu p1 p2 / ang rad cen area)
(setq ang (* 4 (atan bu))
rad (/ (distance p1 p2)
(* 2 (sin (/ ang 2)))
)
area (/ (* rad rad (- ang (sin ang))) 2.0)
)
area
)


{code}

leipogs23 wrote:
> How can i test a polyline if its clockwise or counterclockwise?
> I have a routine that gets the area of polylines then if clockwise,the area is positive and negative if not. But I dont want to get the area just to test if its clockwise or not.
> Any idea?
>
*Expert Elite*
Kent1Cooper
Posts: 5,871
Registered: ‎09-13-2004
Message 3 of 14 (2,139 Views)

Re: polyline clockwise or counter clockwise

03-24-2010 05:26 AM in reply to: leipogs23
http://discussion.autodesk.com/forums/message.jspa?messageID=6107105&tstart=0

among several other threads on the same topic.

--
Kent Cooper


leipogs23 wrote...
How can i test a polyline if its clockwise or counterclockwise?
....
Kent Cooper
Valued Mentor
stevor
Posts: 891
Registered: ‎12-26-2005
Message 4 of 14 (2,139 Views)

Re: polyline clockwise or counter clockwise

03-24-2010 07:54 AM in reply to: leipogs23
1. Kent, was the concave problem, your right facing Pacman, ever resolved?

2. We used a convex hull prep of the linear pline to bypass inlets in building space boundaries.

3. Arcs could be subdvided into linear segments for additional vertices to accomodate a hull approximation.
S
*Bill Gilliss
Message 5 of 14 (2,140 Views)

Re: polyline clockwise or counter clockwise

03-24-2010 08:11 AM in reply to: leipogs23
On 3/24/2010 10:54 AM, stevor wrote:
> 1. Kent, was the concave problem, your right facing Pacman, ever resolved?
>
> 2. We used a convex hull prep of the linear pline to bypass inlets in building space boundaries.
>
> 3. Arcs could be subdvided into linear segments for additional vertices to accomodate a hull approximation.
>

Here is Evgeniy Elpanov's solution, adapted very slightly to return
"clockwise" or "counter-clockwise" instead of just T or nil.

It works correctly on the right-facing PacMan figure, and flips the
result for a mirrored polyline.

gile said it was brilliant -- good enough for me.

-Bill
*Expert Elite*
Kent1Cooper
Posts: 5,871
Registered: ‎09-13-2004
Message 6 of 14 (2,140 Views)

Re: polyline clockwise or counter clockwise

03-24-2010 08:41 AM in reply to: leipogs23
Yes -- the routine attached to the same post as the Pacman image *does* return the correct drawing direction for such shapes. [The description of the problem was only there in explanation of why the routine turned out to be longer than I had thought it might -- it was not a situation that the routine doesn't interpret correctly.]

--
Kent Cooper


stevor wrote...
1. Kent, was the concave problem, your right facing Pacman, ever resolved?
....
Kent Cooper
*Bill Gilliss
Message 7 of 14 (2,140 Views)

Re: polyline clockwise or counter clockwise

03-24-2010 09:24 AM in reply to: leipogs23
On 3/24/2010 11:11 AM, Bill Gilliss wrote:
>
> Here is Evgeniy Elpanov's solution, adapted very slightly to return
> "clockwise" or "counter-clockwise" instead of just T or nil.
>
>
>

A caveat:

One figure that the Elpanov solution will not work reliably on is shown
in red in the attached image. The (vlax-curve-getClosestPointTo)
function returns only two *distinct* points, at the top and bottom of
the figure, instead of four as expected, and thus the main LWCL function
always returns "clockwise" regardless of the direction the polyline was
drawn.
*Dale Fugier
Message 8 of 14 (2,140 Views)

Re: polyline clockwise or counter clockwise

03-24-2010 09:35 AM in reply to: leipogs23
If you use DOSLib, you can just call dos_plinewinding.

- Dale

"leipogs23" wrote in message news:6359711@discussion.autodesk.com...
> How can i test a polyline if its clockwise or counterclockwise?
> I have a routine that gets the area of polylines then if clockwise,the
> area is positive and negative if not. But I dont want to get the area just
> to test if its clockwise or not.
> Any idea?
*Bill Gilliss
Message 9 of 14 (2,140 Views)

Re: polyline clockwise or counter clockwise

03-24-2010 12:02 PM in reply to: leipogs23
On 3/24/2010 12:35 PM, Dale Fugier wrote:
> If you use DOSLib, you can just call dos_plinewinding.
>
> - Dale

Well, in ACA 2010, (dos_plinewinding) returns the opposite of what the
documentation says, and I can't erase the polyline afterwards. Very
strange. Can anyone confirm?

I've emailed McNeel.
New Member
trickyfish75
Posts: 1
Registered: ‎05-21-2010
Message 10 of 14 (2,140 Views)

Re: polyline clockwise or counter clockwise

05-21-2010 02:13 PM in reply to: leipogs23
Could you please explain how you get the area calculation to return negative values or post your routine for returning negative areas? All of my polyline loops are given positive areas, regardless of the direction they were drawn.
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.