Reverse list of 3-D coordinates

Reverse list of 3-D coordinates

Browning_Zed
Advocate Advocate
923 Views
13 Replies
Message 1 of 14

Reverse list of 3-D coordinates

Browning_Zed
Advocate
Advocate

Hi,

If there is a list of 3-d coordinates formatted like this:
'(x y z x y z x y z)
then how can a reverse without changing the order of the coordinates: x y z,
as an example, reverse list:
'(1.0 1.0 0.0 4.0 1.0 0.0 4.0 2.0 0.0)
will return a list:
'(0.0 2.0 4.0 0.0 1.0 4.0 0.0 1.0 1.0)
but I need to get a list like this:
'(4.0 2.0 0.0 4.0 1.0 0.0 1.0 1.0 0.0)
?

0 Likes
Accepted solutions (3)
924 Views
13 Replies
Replies (13)
Message 2 of 14

Sea-Haven
Mentor
Mentor

Convert the lst to ((xyz)(xyz) then reverse.

 

 

; convert now to xyz
(defun co-ords2xy (co-ords xyz / )
(setq co-ordsxy '())
(if (= xyz 2)
(progn
(setq I 0)
(repeat (/ (length co-ords) 2)
(setq xy (list (nth i co-ords)(nth (+ I 1) co-ords) ))
(setq co-ordsxy (cons xy co-ordsxy))
(setq I (+ I 2))
)
)
)
(if (= xyz 3)
(progn
(setq I 0)
(repeat (/ (length co-ords) 3)
(setq xyz (list (nth i co-ords)(nth (+ I 1) co-ords)(nth (+ I 2) co-ords) ))
(setq co-ordsxy (cons xyz co-ordsxy))
(setq I (+ I 3))
)
)
)
)

 

 There are better mapcar functions for this conversion. 

0 Likes
Message 3 of 14

Browning_Zed
Advocate
Advocate

Executing the function (co-ords2xy '(1.0 1.0 0.0 4.0 1.0 0.0 4.0 2.0 0.0) 3) returns 9.

What's the point?

0 Likes
Message 4 of 14

john.uhden
Mentor
Mentor
Accepted solution

@Browning_Zed 

From my perspective it would go like this...

  ;; Function to group a list of items into a list of
  ;; multiple lists, each of length N, e.g.
  ;; '(A B C D E F G H I) -> '((A B C)(D E F)(G H I))
  (defun @group (lst n / item new)
    (foreach element (reverse lst)
      (setq item (cons element item))
      (if (= (length item) n)
        (setq new (cons item new) item nil)
      )
    )
    new
  )
;; Presuming flat is a flat list of 3D coordinates:
(defun @reverse_coords (flat)
  (apply 'append (reverse (@group flat 3)))
)
;; If it's 2D coordinates, change the 3 to a 2.

John F. Uhden

Message 5 of 14

Browning_Zed
Advocate
Advocate

Thank. Grouping lists is not necessary. I need to convert the list like this:

Source list:
'(x y z x y z x y z x y z)
Result after conversion:
'(x y z x y z x y z x y z)

0 Likes
Message 6 of 14

martti.halminen
Collaborator
Collaborator
Accepted solution
(defun reverse-flat-points (data)
  ;; (1 2 3 4 5 6 7 8 9) -> (7 8 9 4 5 6 1 2 3)
  (apply (function append)
         (reverse (group-by-3 data))))

(defun group-by-3 (data)
  ;; (a b c d e f) -> ((a b c)(d e f))
  (if (null data)
      nil
      (cons (list (car data)(cadr data)(caddr data))
            (group-by-3 (cdddr data)))))
Message 7 of 14

Browning_Zed
Advocate
Advocate

Many thanks. This is what I needed.

0 Likes
Message 8 of 14

john.uhden
Mentor
Mentor

@Browning_Zed 

Did you try it?  Notice the (apply 'append ...) in the @reverse function.

OR, @martti.halminen's code looks like a winner too.

John F. Uhden

0 Likes
Message 9 of 14

hak_vz
Advisor
Advisor

@Browning_Zed  Here I will post functions take and pointlistxx that I have provided to your request many times before. Here is last one here. And we have had variation (apply 'append ....) many times before.

 

(defun foo (lst / rake pointlist3d)
(defun take (amount lst / ret)(repeat amount (setq ret (cons (car lst) (take (1- amount) (cdr lst))))))	
(defun pointlist3d (lst / ret) (while lst (setq	ret (cons (take 3 lst) ret) lst (cdddr lst))) (reverse ret))
(apply 'append (reverse(pointlist3d lst)))
)

Command: (foo '(1.0 1.0 0.0 4.0 1.0 0.0 4.0 2.0 0.0))
(4.0 2.0 0.0 4.0 1.0 0.0 1.0 1.0 0.0)

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 10 of 14

john.uhden
Mentor
Mentor

@hak_vz wrote, "many times before."

That is exasperating but what makes you the forum historian. 🤓

John F. Uhden

Message 11 of 14

Browning_Zed
Advocate
Advocate

Yes, I'm sorry, didn't immediately notice the @Anonymous_coords function, then I noticed it, but I could no longer delete my post.) Then I marked your post as a solution to the problem. Thanks to you and others who answered for the help.

0 Likes
Message 12 of 14

Browning_Zed
Advocate
Advocate

Could you give a description of the "take" function? Yes, you always refer to this function, but if I don't understand how it works and what it does, then how can I use it?

0 Likes
Message 13 of 14

hak_vz
Advisor
Advisor
Accepted solution

@Browning_Zed wrote:

Could you give a description of the "take" function? Yes, you always refer to this function, but if I don't understand how it works and what it does, then how can I use it

 

(defun take (amount lst / ret)(repeat amount (setq ret (cons (car lst) (take (1- amount) (cdr lst))))))	

 

It takes some number of list elements (amount)from the start of list

 

Command: (take 2 '(1 2 3 4))
(1 2)
Command: (take 3 '(1 2 3 4))
(1 2 3)

 

You can utilize this function to create list of 2 element lists (2d poly i.e lwpoyline points) or list pf 3 elements (3d poly) .

 

(defun pointlist2d (lst / ret) (while lst (setq	ret (cons (take 2 lst) ret) lst (cddr lst))) (reverse ret))

 

Loop as long list exist, take 2 entities  from he list and remove first two elements from original list (cddr lst).

 

(defun pointlist3d (lst / ret) (while lst (setq	ret (cons (take 3 lst) ret) lst (cdddr lst))) (reverse ret))

 

Loop as long list exist, take 3 entities  from he list and remove first three elements from original list (cddr lst).

To join list of list you can use apply function and append over the list.

 

Command: (setq lst (pointlist2d '(1 2 3 4 5 6)))
((1 2) (3 4) (5 6))
Command: (setq lst (apply 'append lst))
(1 2 3 4 5 6)

 

To switch order of list elements use function reverse.

 

Command: (reverse lst)
(6 5 4 3 2 1)

 

P.S. If you need explanation about how some function work, just ask. 

Same as you my first 30-50 posts to this forum were just requests. After that I started solving some simple tasks, than more complex one..... Once you learn how to use lisp then "only sky is the limit".

Today at work I was repairing a survey map that was created all wrong. For every step of repair I have created my simple function or used something from my lisp base and after some 2h result was top notch. Usually it would take two to three days when contractors surveyor would repair all the flaws and many times result was not good.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 14 of 14

Browning_Zed
Advocate
Advocate

Thank you so much for sharing such detailed information about how the functions work and your personal programming experience. By a strange coincidence, I am also a surveyor and I am learning AutoLISP in order to automate my routine actions when creating topographic maps.
Once again, many thanks.

0 Likes