Hi I am thinking aloud on this one and would like to see if I am on the right track
(setq distlist (list' 0)); set first distance on list as 0
(while
(setq pt1 (nth pointslist)); get point n from list of points
(setq pt2 (+1 nth pointslist));get next point from the list
(setq dist (+dist (distance pt1 pt2))); work out the distance between the points and add it to the previous
(setq distlist (cons dist)); add that distance to the list
) move nth to the next one
(reverse distlist) make the list run the right direction
Solved! Go to Solution.
Solved by Kent1Cooper. Go to Solution.
Solved by pbejse. Go to Solution.
Solved by pbejse. Go to Solution.
Not sure what you want to do, Is this just an lisp exercise? What exactly is the end result? the Total length?
Should end up with a list like ( 0 10 30 60 100 ), first join is 0 as it is the start point. "joining" point 1 and point 2 from my list I get 10 as shown , "joining" point 2 and point 3 from my list i get 20 add this to 10 and I get 30 as shown this carries on till I get to the end of the points list. I want to do a further calculation based on that running distance list so I need each one not just the total. Note these are notional values as my points list comes from any selected polyline.
HYG [as per your posted code]
(setq distlist (list '0) ; set first distance on list as 0 n 0 ; counter for nth function dist 0 ; dist as a non nil value ) (while (and (setq pt1 (nth n pointslist)) ; get point n from list of points (setq pt2 (nth (1+ n) pointslist)) ; get next point from the list ) (setq dist (+ dist (distance pt1 pt2))) ; work out the distance between the points ; and add it to the previous (setq distlist (cons dist distlist)) ; add that distance to the list (setq n (1+ n)) ; move nth to the next one ) ; end while (reverse distlist) ;make the list run the right direction
HTH
@richie_hodgson wrote:
Cool thanks thats just wonderfull.
You are welcome, there is a couple of ways to accomplish that desired result, heres one approach.
(setq distlist (cons (setq dist 0) (mapcar '(lambda (p1 p2) (setq dist (+ dist (distance p1 p2))) ) pointslist (cdr pointslist) ) ) )
HTH
@richie_hodgson wrote:
Hi thanks, the second option looks interesting if it achieves the same thing. One thing I forgot is that the distance should be absolute, never negative so I am unsure where to put the ABS statement into the second code option
I maybe wrong, but i dont see why the distance would result to a negative value, so there's no need for abs function, UNLESS, the value of dist is a negative value from the get-go. The code i posted should start wih 0. Can you give me an example of a negative value result.
@richie_hodgson wrote:
Ok if for example pt2 was lower and to the left of pt1, would distance pt1 pt2 result in a negative
No. not really, Look here:
(setq pt1 '(394.31 70.9552 0.0));<-- lower point
(setq pt2 '(600.145 315.343 0.0)):<-- higher point
(distance pt1 pt2) -----> 319.52
(distance pt2 pt1) -----> 319.52
To expand on this just a little so you can understand a little better.....
A distance is just that a DISTANCE. If it were to include a negative, that would indicate DIRECTION from a specific location.
Don Ireland
Engineering Design Technician
@richie_hodgson wrote:
Should end up with a list like ( 0 10 30 60 100 ), first join is 0 as it is the start point. "joining" point 1 and point 2 from my list I get 10 as shown , "joining" point 2 and point 3 from my list i get 20 add this to 10 and I get 30 as shown this carries on till I get to the end of the points list. I want to do a further calculation based on that running distance list so I need each one not just the total. Note these are notional values as my points list comes from any selected polyline.
You can get that as a series of distances-so-far directly from the Polyline itself, rather than finding a batch of segment lengths as separate numbers and adding them up as you go along. For a Lightweight Polyline:
(setq
plent (car (entsel "\nSelect Polyline: "))
verts (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget plent))); your points list
); setq
(foreach vert verts
(setq distlist (cons (vlax-curve-getDistAtPoint plent vert) distlist)); put distance along the Polyline at each vertex into list
); foreach
(reverse distlist)
If you need them as integers as in your example [the above returns real numbers], that can be done in a couple of ways. If you need to do it with "heavy" 2D or 3D Polylines, that can also be done with a different approach to the points list, but if you already have that list, you can plug it and the entity name in and just use the (foreach) part of the above. And if you need the overall length at the end for a closed Polyline [the above would not include that], that can easily be added.
Can't find what you're looking for? Ask the community or share your knowledge.