# Polyline is going clockwise or counterclockwise

## Recommended Posts

Hello. I need to know if a closed polyline is going clockwise or counterclockwise. Thanks

##### Share on other sites

How far have you got with this?

I guess if you use entget for the user to select the polyline and then entlist you could use what was discussed below (Dan2047 or Mhupp) to give you a list of all the assoc's umber 10. all the points from the polyline

Once you have them I think it is just maths to work out what direction each segment is going in.. though I haven't thought how to tell if a closed polyline is clockwise or anticlockwise yet.

Maybe have a go at getting the list of polyline points and see if someone can take it from there?

##### Share on other sites

This has been asked many times over ... HERE is a function you can use. Do you know how to get points from a polyline?

##### Share on other sites

In this forum ...

you can find CHVERT.LSP to change first point and/or reverse polyline.

##### Share on other sites

@ronjonp  Lee Mac's routine determines CW or CCW from three points.  Use it assumes the polyline is a convex shape. If the polyline  has a concave segment this may yield the wrong direction.

##### Share on other sites

That's right, but this is perhaps where the OP needs to help out with some thinking maybe?

Haven't checked but it probably needs a combination of each segment direction and length weighted somehow and added together to get an overall direction. Imagine rectangle with a concave segment cut into it - for arguments the concave segment is made from short lines. If you add up all the directions then the short lines of the concave section will be more than the directions of the other 5. +ve multiplier for line length clockwise and -ve multiplier line length for anticlockwise, then add up all the line lengths? -ve and it is mostly anticlockwise, +ve and clockwise perhaps

##### Share on other sites

One by Kent Cooper is for closed plines get area then use VLAoffset, test (< area1 area2) a simple is it clockwise. If area goes bigger its CCW.

For direction on a open PLINE I use pick an end so know direction to imply.

##### Share on other sites

Take a look at the function clockwise-p in this post.

##### Share on other sites

Thank you all. In the end I have done this, which works in most cases. If they are very rare polylines, it may fail. To find the solution I use an auxiliary line that I delete at the end of the sequence.
Thanks!!!!!

```(defun c:sentido ()
Space (if (= (getvar "CVPORT") 1)
(vla-get-PaperSpace AcDoc)
(vla-get-ModelSpace AcDoc)
)
)
(setq lista_poli (entget nombre_poli))
(foreach n lista_poli
)

;centroide
(setq obj (vlax-ename->vla-object nombre_poli))
(setq reg (vlax-invoke Space 'addRegion (list obj)))
(setq centroide (vlax-get (car reg) 'Centroid))
(entdel (entlast))

;creamos linea auxiliar
(entmake
(list
'(0 . "LINE")
'(100 . "AcDbEntity")
'(100 . "AcDbLine")
(cons 10 centroide)
(cons 11 pt_izda)
)
);fin entmake
(setq aux (vlax-ename->vla-object (entlast)))
(setq interseccion (vla-intersectWith aux obj AcExtendNone))
(if (not(vl-catch-all-error-p (vl-catch-all-apply 'vlax-safearray->list (list (vlax-variant-value interseccion)))))
(princ "\nSentido Horario") (princ "\nSentido AntiHorario")
)
(entdel (entlast))
);fin while
)```

Edited by robierzo

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.