TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: David Bethel on October 20, 2015, 05:11:50 AM

Title: 2D Point car/cdr
Post by: David Bethel on October 20, 2015, 05:11:50 AM


Can anyone think of a combination of car/cdr that could extract the X & Y values of a point group

Code: [Select]
(setq p '(10 2 4 6))

-> (2 4)



Instead of the kludge :

Code: [Select]
(setq p2 (list (nth 1 p) (nth 2 p))

Thanks  -David
Title: Re: 2D Point car/cdr
Post by: Lee Mac on October 20, 2015, 05:27:24 AM
Code: [Select]
(reverse (cdr (reverse (cdr p))))
Not sure its any less of a kludge.

Others:
Code: [Select]
(list (cadr p) (caddr p))
Code: [Select]
(mapcar 'nth '(1 2) (list p p))
Code: [Select]
(mapcar 'progn '(0 0) (cdr p))
Title: Re: 2D Point car/cdr
Post by: David Bethel on October 20, 2015, 05:32:27 AM
LOL

I looked at the double reverse - way ugly  8-)
Title: Re: 2D Point car/cdr
Post by: ribarm on October 20, 2015, 05:52:27 AM
I don't understand :

Code: [Select]
(mapcar 'progn '(0 0) (cdr p))

I thought it should be :

Code: [Select]
(mapcar '+ '(0 0) (cdr p))
Title: Re: 2D Point car/cdr
Post by: Lee Mac on October 20, 2015, 06:04:15 AM
LOL

I looked at the double reverse - way ugly  8-)

 :-D
Title: Re: 2D Point car/cdr
Post by: Lee Mac on October 20, 2015, 06:53:04 AM
I don't understand :

Code: [Select]
(mapcar 'progn '(0 0) (cdr p))

I thought it should be :

Code: [Select]
(mapcar '+ '(0 0) (cdr p))

Why?

I think it should be:
Code: [Select]
(mapcar 'progn '(Lee Mac) (cdr p))

 :lol:
Title: Re: 2D Point car/cdr
Post by: ymg on October 21, 2015, 01:34:23 PM
David,

FWIW,

Quote
(benchmark '((butlast (cdr p)) (list (nth 1 p) (nth 2 p))  (list (cadr p) (caddr p))))
Benchmarking ...................Elapsed milliseconds / relative speed for 65536 iteration(s):

    (BUTLAST (CDR P))..............1591 / 1.19 <fastest>
    (LIST (CADR P) (CADDR P))......1794 / 1.05
    (LIST (NTH 1 P) (NTH 2 P)).....1888 / 1.00 <slowest>

ymg

Title: Re: 2D Point car/cdr
Post by: MickD on October 21, 2015, 04:35:01 PM
I'm a lisp neophyte but I prefer clarity over conciseness/speed for maintenance and comprehension reasons and I would do it something like this if I knew the point group list was always the same.

(note: this is in scheme but you get the picture)

(define (x-point pgroup)
  (cadr p))

(define (y-point pgroup)
  (caddr p))

output:
> (x-coord '(10 2 4 6))
2
> (y-coord '(10 2 4 6))
4

If more sample code was written like this (i.e. with simple helper functions) I think the logic behind the function would be easier to understand.
Don't mind me though, just having a play and learning from you guys :)