'if', 'and', and rem...?

'if', 'and', and rem...?

Anonymous
Not applicable
532 Views
6 Replies
Message 1 of 7

'if', 'and', and rem...?

Anonymous
Not applicable

Hi Please could someone tell me why this is not working. I am trying the calculate the number of points of intersection that could be created by using the vertices of a polyline. The first point of intersection would be possible once there are four vertices to the polyline. Thereafter, whenever the number of vertices is an even number, another point of intersection becomes available. 

 

In the code below, the variable 'p' is used for the number of points of intersection and the variable 'n' is used for the number of vertices. The trouble is, 'p' is returning too many points of intersection? For example, when I analyse a polyline with 6 vertices, 'p' is 8? I just can't figure out what is wrong with it?

 

(defun c:r15 ()

(setq e (entget (car (entsel "Select polyline to analyse: "))))
(setq p1 (getpoint "Click where you want the table: "))
(setq p2 p1)
(setq tz (getreal "Enter text height: "))
(setq ct 0)
(setq lnth (length e))
(setq tbly 0)
(setq n 0)


(repeat lnth
(setq ct (+ 1 ct))
(setq tx (nth ct e))
(setq ts (car tx))

(if (= ts 10)
(progn
(setq n (+ 1 n))
(set (read (strcat "crd" (itoa n))) (cdr tx))
)
)

(if (= n 4) (setq p 1))

(if (and
(> n 4)
(= (rem n 2) 0))
(setq p (+ p 1))
)

)

0 Likes
Accepted solutions (1)
533 Views
6 Replies
Replies (6)
Message 2 of 7

Kent1Cooper
Consultant
Consultant

You don't say in what way it "is not working."  Does it give some result, but not what you expect?  Does it not do anything?  Obviously this isn't the entire code, since you don't include doing anything with the "crd"+n variables or filling in the "table," so could the problem be in some part that's not included here?

 

Nothing jumps out at me, but there are considerably more concise ways to do most of this, for example:

 

(setq
  e (entget (car (entsel "Select polyline to analyse: "))))
  verts
    (mapcar 'cdr ; take first item [the 10] off beginning of, and leave only point list from, each item in:
      (vl-remove-if-not '(lambda (x) (= (car x) 10)) e)
        ; list of vertex entries only from entity data [remove all that don't start with 10]
    ); mapcar & verts
  p (/ (- (length verts) 2) 2); all at once!
    ; [can also use (cdr (assoc 90 e)) in place of (length verts)]
  n 0
); setq
(foreach vert verts
  ; ... build variables with "cdr"+n names containing vertex locations, similar to your

  ;   construction but no (cdr) required for points -- that was done by (mapcar) above]
); foreach

 

That setting of 'p' in one shot relies on the fact that dividing an integer by an integer always results in an integer, rounded down if there's any remainder.

Kent Cooper, AIA
0 Likes
Message 3 of 7

Anonymous
Not applicable
Hi Kent

You have seen this code before, I cannot use the map function, as I then
cannot save each point as a variable. I need the points as variables,
because I need to calculate the points of intersection created by all the
vertices of the polyline.

I have run this code without the rest of the code and it still gives the
exact same result. The rest of the code does not affect the code given (go
back to the code given) at any stage...

The problem with the code, is that p should count to 2, but is 8 (when I
have a polyline with 6 vertices)...

Cheers

Derryck
0 Likes
Message 4 of 7

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:
... I cannot use the map function, as I then cannot save each point as a variable. I need the points as variables....
....
The problem with the code, is that p should count to 2, but is 8 (when I have a polyline with 6 vertices)...
....

Sure you can.  I didn't fill it in before [the two-line blue comment], but:

 

(foreach vert verts

  (set (read (strcat "crd" (itoa (setq n (1+ n))))) vert)

); foreach

 

That would put the first vertex as a point list into the "crd1" variable, the next into "crd2" and so on.

 

I don't notice anything to explain p coming out as 8 from 6 vertices, unless perhaps you're compounding my all-at-once version with keeping later related code from your original, but my one-shot setting results in p = 2 from 6 vertices.  [But I did have an extra right parenthesis on the line setting the e variable -- it should have only 3 of them at the end].

Kent Cooper, AIA
0 Likes
Message 5 of 7

Anonymous
Not applicable
Hi Kent

I really do not understand how you get p to be 2 with n 6? I have run this code many times and always p is 8 with n 6?

My line setting the e variable has four parentheses at the end, which it should, because it closes the setq command?
0 Likes
Message 6 of 7

Anonymous
Not applicable
Actually never mind, I have figured out another way to obtain the number of points of intersection.. and this time it works 🙂 So cool.

Thanks anyway.

Cheers

Derryck
0 Likes
Message 7 of 7

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:
....
I really do not understand how you get p to be 2 with n 6? I have run this code many times and always p is 8 with n 6?

My line setting the e variable has four parentheses at the end, which it should, because it closes the setq command?

I have several times run that (/) function that calculates p, substituting 6 in place of (length verts), and it always returns 2.  Have you left in any of your other p-setting elements?  Those should all be removed [that's what "all at once!" meant -- no adjusting p step by step as n increases].

 

My line setting the e variable had four right parentheses at the end when that was the only variable set within that (setq) function, but when I folded more variables into the same (setq) function, I should have taken the last one off that line -- it's covered by the one down below the setting of n.

Kent Cooper, AIA
0 Likes