### Author Topic: Challenge - Find interior angles of a closed polyline  (Read 12125 times)

0 Members and 1 Guest are viewing this topic.

#### T.Willey ##### Re: Challenge - Find interior angles of a closed polyline
« Reply #15 on: December 10, 2010, 03:18:05 PM »
Nice solution Luis.  Now I have one more code I need to study when I have time.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

#### Guitar_Jones

• Guest ##### Re: Challenge - Find interior angles of a closed polyline
« Reply #16 on: December 14, 2010, 05:57:39 PM »
Just had time to test these today.
The only one that seems to work is Le3's (by looking at pics)
With the example shown, output should only include: ("125" "109" "125" "80" "100")

Testing open and closed polylines...

Vovka's Output (w/o including isboundaryclockwise)
("90" "125" "109" "125" "80" "190") for both

Lee's Output
("90" "125" "109" "125" "80" "10") for both

Elpanov's output
1st code ("90" "125" "109" "125" "80" "10") for both
2nd code
("235" "251" "235" "280" "180" "260") for open polyline
"; error: bad argument type: 2D/3D point: nil" for closed polyline

T.Willey's output
"; error: no function definition: nil" for both
**EDIT**
Was missing the RTD function...output was
("10" "80" "125" "109" "125" "90") for closed
nil (as intended) for open

Using some of the supplied code by Lee and Vovka, this seems to work for both open and closed

Code: [Select]
`(defun GetLeftAngle ( p2 / p1 p3 pos)  (setq pos (vl-position p2 l) p1 (nth (rem (+  n(1- pos)) n) l) p3 (nth (rem (+ n (1+ pos))  n) l))  (rem (+ pi pi (- (angle p2 p1) (angle p2 p3))) (+ pi pi)))(defun c:test ( / l m n)  (setq n (length (if (member nil (mapcar '(lambda (x y) (eq x y))          (car (setq l (mapcar 'cdr (vl-remove-if-not '(lambda ( x ) (= (car x) 10)) (entget (car (entsel)))))))          (last l))) l (setq l (cdr l))))        l (mapcar 'GetLeftAngle l)) (mapcar 'angtos (if (< (apply '+ l) (apply '+ (setq m (mapcar '(lambda ( x ) (- (* 2. pi) x)) l)))) l m)))`
*EDIT*
I'm including the example drawing.
« Last Edit: December 14, 2010, 11:04:26 PM by Guitar_Jones » ##### Re: Challenge - Find interior angles of a closed polyline
« Reply #17 on: December 15, 2010, 03:27:26 AM »
@ Guitar_Jones:
That is an interesting test but... the polylines you use are "exceptional":

Your open polyline is pseudo-closed. The coordinates of the first and last vertex are identical. A "really" open polyline doesn't have an interior and therefore no interior angles.

Your closed polyline (created from your open polyline) has two vertices with the same coordinates. Your code handles the case where the first and the last vertex are identical, but of course there can be more or other matching sequential vertices.
« Last Edit: December 15, 2010, 07:07:31 AM by roy_043 »

#### Guitar_Jones

• Guest ##### Re: Challenge - Find interior angles of a closed polyline
« Reply #18 on: December 15, 2010, 09:37:46 AM »
Quote
Your open polyline is pseudo-closed. The coordinates of the first and last vertex are identical. A "really" open polyline doesn't have an interior and therefore no interior angles.
I can understand the code not working on the open polylines.

Quote
Your closed polyline (created from your open polyline) has two vertices with the same coordinates. Your code handles the case where the first and the last vertex are identical, but of course there can be more or other matching sequential vertices.
IMO, a closed polyline containing matching start/end vertices is not unusual as i found this to be the case in many drawings tested coming from multiple sources.

#### stevesfr

• Newt
• Posts: 54 ##### Re: Challenge - Find interior angles of a closed polyline
« Reply #19 on: December 15, 2010, 11:28:40 AM »
Just had time to test these today.
The only one that seems to work is Le3's (by looking at pics)
With the example shown, output should only include: ("125" "109" "125" "80" "100")

Testing open and closed polylines...

Vovka's Output (w/o including isboundaryclockwise)
("90" "125" "109" "125" "80" "190") for both

Lee's Output
("90" "125" "109" "125" "80" "10") for both

Elpanov's output
1st code ("90" "125" "109" "125" "80" "10") for both
2nd code
("235" "251" "235" "280" "180" "260") for open polyline
"; error: bad argument type: 2D/3D point: nil" for closed polyline

T.Willey's output
"; error: no function definition: nil" for both
**EDIT**
Was missing the RTD function...output was
("10" "80" "125" "109" "125" "90") for closed
nil (as intended) for open

Using some of the supplied code by Lee and Vovka, this seems to work for both open and closed

Code: [Select]
`(defun GetLeftAngle ( p2 / p1 p3 pos)  (setq pos (vl-position p2 l) p1 (nth (rem (+  n(1- pos)) n) l) p3 (nth (rem (+ n (1+ pos))  n) l))  (rem (+ pi pi (- (angle p2 p1) (angle p2 p3))) (+ pi pi)))(defun c:test ( / l m n)  (setq n (length (if (member nil (mapcar '(lambda (x y) (eq x y))          (car (setq l (mapcar 'cdr (vl-remove-if-not '(lambda ( x ) (= (car x) 10)) (entget (car (entsel)))))))          (last l))) l (setq l (cdr l))))        l (mapcar 'GetLeftAngle l)) (mapcar 'angtos (if (< (apply '+ l) (apply '+ (setq m (mapcar '(lambda ( x ) (- (* 2. pi) x)) l)))) l m)))`
*EDIT*
I'm including the example drawing.

So where id the left over degree go ? 539 vs 540  and this is a closed pline ?  ya gotta do better.
Can't remember what I'm supposed to forget.

#### Guitar_Jones

• Guest ##### Re: Challenge - Find interior angles of a closed polyline
« Reply #20 on: December 15, 2010, 11:39:02 AM »
Quote
So where id the left over degree go ? 539 vs 540  and this is a closed pline ?  ya gotta do better.

The precision of the output angles is based on the auprec variable as the argument is omitted in the code...
« Last Edit: December 15, 2010, 11:47:29 AM by Guitar_Jones » ##### Re: Challenge - Find interior angles of a closed polyline
« Reply #21 on: December 15, 2010, 12:00:44 PM »
Maybe just this to avoid duplicate vertices (fuzz could be adjusted):

Code: [Select]
`(defun LM:GetLeftAngle ( p1 p2 p3 )  (rem (+ pi pi (- (angle p2 p1) (angle p2 p3))) (+ pi pi)))(defun LM:UniqueWithFuzz ( l fz )  (if l (cons (car l) (LM:UniqueWithFuzz (vl-remove-if '(lambda ( x ) (equal x (car l) fz)) (cdr l)) fz))))(defun c:test ( / ss l m )  (if (setq ss (ssget "_+.:S:E" '((0 . "LWPOLYLINE") (70 . 1))))    (progn      (setq l        (LM:UniqueWithFuzz          (mapcar 'cdr (vl-remove-if-not (function (lambda ( x ) (= (car x) 10))) (entget (ssname ss 0))))          1e-8        )      )            (mapcar 'angtos        (if (< (apply '+ (setq l (mapcar 'LM:GetLeftAngle (cons (last l) l) l (append (cdr l) (list (car l))))))               (apply '+ (setq m (mapcar '(lambda ( x ) (- (* 2. pi) x)) l))))          l m        )      )    )  ))`

#### Guitar_Jones

• Guest ##### Re: Challenge - Find interior angles of a closed polyline
« Reply #22 on: December 15, 2010, 12:26:49 PM »
Beautiful. Works perfectly. Thanks Lee!