Author Topic: Coordinates compertion  (Read 10149 times)

0 Members and 1 Guest are viewing this topic.

Shay Gaghe

  • Newt
  • Posts: 85
Coordinates compertion
« on: July 22, 2015, 11:12:48 PM »
hi

i would ask if 2 points are equal like this

Code: [Select]
(defun isCordEqual (pa pb)
    (vl-every '= pa pb)
    )

how can i ask if pa is smaller or equal to pb?

Thanks
Shay


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Coordinates compertion
« Reply #1 on: July 22, 2015, 11:16:30 PM »
What do you mean by 'smaller than' for 3d points ???
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Coordinates compertion
« Reply #2 on: July 22, 2015, 11:49:27 PM »
While we are waiting:

by definition :

if each element of 'a is less than or equal to the elements of 'b
then
each element of 'b is greater than each element of 'a.

AND

if each element of 'a is more than or equal to the elements of 'b
then
each element of 'b is less than each element of 'a.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Coordinates compertion
« Reply #3 on: July 23, 2015, 02:22:43 AM »
Simplistically ... just from the wording in the OP, use '<= (i.e. less than or equal to) instead of '=.

You'd need to figure out which of the 3 dimensions are the more influential during a sort, that's up to you (i.e. what would you want - levels different, then perhaps Z values). Then that defines the greater/less than check, if it's equal then the next one in line does the same. E.g. if you first check X, then Y then Z ... then a simple change to <= in your function would suffice. But what if you want higher / lower levels for Z to be the main defining one?

To show why you need to choose the first to check: Another less useful idea could be to create a comparison function. Note a comparison function for sorting basically returns 0 (if equal), -1 (if less than) and 1 (if greater than). Then add those numbers together by comparing each dimension in the 2 points, checking the result for > 0 or < 0 or = 0. Something like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun comp-val (n m)
  2.   (cond ((< n m) -1)
  3.    ((> n m) 1)
  4.    (0)))
  5.  
  6. (defun comp-lst (n m)
  7.   (apply '+ (mapcar 'comp-val n m)))
  8.  
  9. (comp-lst '(1 2 3) '(2 1 3)) ; Returns 0 as none of XYZ is assumed more influential

If that returns 0, they're "equal", even when they're not. Thus even if that returns a negative (i.e. less than) or a positive (greater than), what does it actually mean?

BTW, with coordinates you have double floating point values. It's highly unlikely that any 2 double floats are equal. So the = function isn't a very good idea for equality testing in this case. I'd advise going with the equal function instead with a fuzz factor to accommodate floating errors. That fuzz factor depends on the scale of the values - since a double float is close to a 13 to 16 digit decimal accuracy. That means if the scale of the number is in the 10000 range, the accuracy is within 13 to 16 less the already taken up 5 digits - i.e. 8 to 11 decimal places after the point. Therefore the fuzz factor should be adjusted depending on the values being checked. Usually you just use the 8 decimal places to catch most problems by doing this:
Code - Auto/Visual Lisp: [Select]
  1. (equal n m 1e-8)
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Coordinates compertion
« Reply #4 on: July 23, 2015, 05:00:04 AM »
Smaller as in closer to the origin?
Code: [Select]
(< (distance '(0 0 0) a) (distance '(0 0 0) b))

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Coordinates compertion
« Reply #5 on: July 24, 2015, 11:33:31 AM »
Maybe :

Code - Auto/Visual Lisp: [Select]
  1.   (setq l1 '(1.001 0.999 1.0)
  2.         l2 '(1     1     1)
  3.         fz 1e-8)
  4.  
  5.   (setq s "(")
  6.   (foreach a '(0 1 2)
  7.     (setq s (cond ((equal (nth a l1) (nth a l2) fz)
  8.                    (strcat s " = "))
  9.                   ((> (nth a l1) (nth a l2))
  10.                    (strcat s " > "))
  11.                   ((< (nth a l1) (nth a l2))
  12.                    (strcat s " < ")))))
  13.   (setq s (strcat s ")"))
  14.   (alert s)

-David
R12 Dos - A2K

Shay Gaghe

  • Newt
  • Posts: 85
Re: Coordinates compertion
« Reply #6 on: July 27, 2015, 05:35:05 PM »
lets say i have this association list where every key refers a list. i want to know which list (a,b or c) has the highest/lowest point and the actual point.how can i do it?

Code: [Select]
(setq lst (list
    (cons "a"  (list '( (0 2 4) (0 7 0) (4 2 0))))
    (cons "b"  (list '( (8 2 0) (0 5 0) (7 7 0))))
    (cons "c"  (list '( (3 2 0) (0 2 0) (9 3 0))))
    )
      )

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Coordinates compertion
« Reply #7 on: July 27, 2015, 06:22:01 PM »
i want to know which list (a,b or c) has the highest/lowest point

Highest meaning largest y-coordinate value?
Largest z-coordinate value?
Furthest from the origin?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Coordinates compertion
« Reply #8 on: July 27, 2015, 06:35:49 PM »
What do you mean by 'smaller than' for 3d points ???

It is a little tedious talking to myself.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Shay Gaghe

  • Newt
  • Posts: 85
Re: Coordinates compertion
« Reply #9 on: July 27, 2015, 11:10:42 PM »
What do you mean by 'smaller than' for 3d points ???

It is a little tedious talking to myself.

 :police: sorry. weekends are "full scan mode" for me  :police:

Shay Gaghe

  • Newt
  • Posts: 85
Re: Coordinates compertion
« Reply #10 on: July 27, 2015, 11:20:55 PM »
i want to know which list (a,b or c) has the highest/lowest point

Highest meaning largest y-coordinate value?
Largest z-coordinate value?
Furthest from the origin?
by highest i meant the coordinate with the largest y value

in ("a" ((0 2 0) (0 7 0) (0 2 0))) the highest level is (0 7 0)
in ("b" ((0 2 0) (0 5 0) (0 2 0))) the highest level is (0 5 0)
in ("c" ((3 2 0) (0 2 0) (9 3 0))) the highest level is (9 3 0)

the highest level of all is (0 7 0) and the key is "a"

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Coordinates compertion
« Reply #11 on: July 28, 2015, 05:38:57 AM »
So, do you now want the cons-key, and the point ??

... or just the point ?

added:
and there may be any quantum of consed lists
and there may be any quantum of point lists in each consed list.
??


« Last Edit: July 28, 2015, 05:42:14 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Shay Gaghe

  • Newt
  • Posts: 85
Re: Coordinates compertion
« Reply #12 on: July 28, 2015, 05:41:20 AM »
Both

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Coordinates compertion
« Reply #13 on: July 28, 2015, 05:44:08 AM »
Here's a simple way to approach it:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( lst / key rtn )
  2.     (setq key (caar lst)
  3.           rtn (caadar lst)
  4.     )
  5.     (foreach grp lst
  6.         (foreach pnt (cadr grp)
  7.             (if (< (cadr rtn) (cadr pnt))
  8.                 (setq rtn pnt
  9.                       key (car grp)
  10.                 )
  11.             )
  12.         )
  13.     )
  14.     (list key rtn)
  15. )

Code - Auto/Visual Lisp: [Select]
  1. (setq lst
  2.    '(
  3.         ("a" ((0 2 4) (0 7 0) (0 2 0)))
  4.         ("b" ((0 2 0) (0 5 0) (0 2 0)))
  5.         ("c" ((3 2 0) (0 2 0) (9 3 0)))
  6.     )
  7. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (foo lst)
  2. ("a" (0 7 0))

Shay Gaghe

  • Newt
  • Posts: 85
Re: Coordinates compertion
« Reply #14 on: July 28, 2015, 06:56:29 AM »
Here's a simple way to approach it:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( lst / key rtn )
  2.     (setq key (caar lst)
  3.           rtn (caadar lst)
  4.     )
  5.     (foreach grp lst
  6.         (foreach pnt (cadr grp)
  7.             (if (< (cadr rtn) (cadr pnt))
  8.                 (setq rtn pnt
  9.                       key (car grp)
  10.                 )
  11.             )
  12.         )
  13.     )
  14.     (list key rtn)
  15. )

Code - Auto/Visual Lisp: [Select]
  1. (setq lst
  2.    '(
  3.         ("a" ((0 2 4) (0 7 0) (0 2 0)))
  4.         ("b" ((0 2 0) (0 5 0) (0 2 0)))
  5.         ("c" ((3 2 0) (0 2 0) (9 3 0)))
  6.     )
  7. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (foo lst)
  2. ("a" (0 7 0))

i need to learn what you did. :-o

thanks for the meantime....

Shay Gaghe

  • Newt
  • Posts: 85
Re: Coordinates compertion
« Reply #15 on: July 28, 2015, 08:44:33 AM »
Here's a simple way to approach it:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( lst / key rtn )
  2.     (setq key (caar lst)
  3.           rtn (caadar lst)
  4.     )
  5.     (foreach grp lst
  6.         (foreach pnt (cadr grp)
  7.             (if (< (cadr rtn) (cadr pnt))
  8.                 (setq rtn pnt
  9.                       key (car grp)
  10.                 )
  11.             )
  12.         )
  13.     )
  14.     (list key rtn)
  15. )

Code - Auto/Visual Lisp: [Select]
  1. (setq lst
  2.    '(
  3.         ("a" ((0 2 4) (0 7 0) (0 2 0)))
  4.         ("b" ((0 2 0) (0 5 0) (0 2 0)))
  5.         ("c" ((3 2 0) (0 2 0) (9 3 0)))
  6.     )
  7. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (foo lst)
  2. ("a" (0 7 0))

Code: [Select]
(defun foo ( lst / key rtn )
    (setq key (caar lst)
          rtn (caadar lst)
    )
    (foreach grp lst
        (foreach pnt (cadr grp)
            (if (< (cadr rtn) (cadr pnt))
                (setq rtn pnt
                      key (car grp)
                )
            )
        )
    )
    (list key rtn)
)

;_rtn is the first point to be compared to
;_compare each delivered point's Y to rtn's Y
;_if the delivered point is bigger
;_replace  rtn with that point
;_replace the key with the delivered key
        ;_
;_

i understand it, but i dont know how i would explain it to someone else....what logical steps i need to plan in order  to the solution?

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Coordinates compertion
« Reply #16 on: July 28, 2015, 08:48:53 AM »
i need to learn what you did. :-o

thanks for the meantime....

You're welcome.

The task could be accomplished in many ways - here is another:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo2 ( lst )
  2.     (car (vl-sort (apply 'append (mapcar '(lambda ( x ) (mapcar '(lambda ( y ) (list (car x) y)) (cadr x))) lst)) '(lambda ( a b ) (> (cadadr a) (cadadr b)))))
  3. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (foo2 lst)
  2. ("a" (0 7 0))

Shay Gaghe

  • Newt
  • Posts: 85
Re: Coordinates compertion
« Reply #17 on: July 28, 2015, 08:50:47 AM »
Quote
i understand it, but i dont know how i would explain it to someone else....what logical steps i need to plan in order  to the solution?

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Coordinates compertion
« Reply #18 on: July 28, 2015, 08:54:01 AM »
;_rtn is the first point to be compared to
;_compare each delivered point's Y to rtn's Y
   ;_if the delivered point is bigger
      ;_replace  rtn with that point
      ;_replace the key with the delivered key
        ;_
;_

Your understanding looks to be correct.

Shay Gaghe

  • Newt
  • Posts: 85
Re: Coordinates compertion
« Reply #19 on: July 28, 2015, 08:55:33 AM »
;_rtn is the first point to be compared to
;_compare each delivered point's Y to rtn's Y
   ;_if the delivered point is bigger
      ;_replace  rtn with that point
      ;_replace the key with the delivered key
        ;_
;_

Your understanding looks to be correct.

how do you map this logic to human language?  :-)

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Coordinates compertion
« Reply #20 on: July 28, 2015, 09:09:11 AM »
how do you map this logic to human language?  :-)

I don't see the point of your question - you have already described each step of the algorithm in 'human language', which shows that you already understand the algorithm; why then would you need this to be written in such a way to explain to someone else? Is this for a homework assignment?

Shay Gaghe

  • Newt
  • Posts: 85
Re: Coordinates compertion
« Reply #21 on: July 28, 2015, 09:22:34 AM »
how do you map this logic to human language?  :-)

I don't see the point of your question - you have already described each step of the algorithm in 'human language', which shows that you already understand the algorithm; why then would you need this to be written in such a way to explain to someone else? Is this for a homework assignment?

i spent half day thinking about this while you just solve it like you are sms "good morning" to your girlfriend  :-D :-D :-D

i just preparing myself to help my daughter with her homework :-D

I was always thinking there was  a way to tell the machine what to do as it was your employee, but I cant really see it possible, (otherwise there wasnt a need for programing language i guess).
I do understand the algorithm, and you understand that I understand it because you are a programmer, but if youwarnt  a programmer you wouldn’t understand.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Coordinates compertion
« Reply #22 on: July 28, 2015, 09:27:59 AM »
how do you map this logic to human language?  :)
You mean something like this:
  • Keep track of the first point and its key in some local variables.
  • Look at each key+point pair in turn, comparing its Y value to the one in that local variable.
  • If the current point's Y is larger than the Y in the local variable, replace the contents of the local variables with the current point and its key.
  • Once all points have finished being compared, the local variables should now contain the values of the largest point, so return them as the last statement in the function.
I was always thinking there was  a way to tell the machine what to do as it was your employee, but I cant really see it possible, (otherwise there wasnt a need for programing language i guess).
That's still just a pipe dream ... one that was thought up in the 50s - 60s: What's referred to as a 4th generation programming language, i.e. a natural human language. 3rd being the normal programming languages we use mostly (e.g. Lisp / C / Python / etc.), 2nd being assembly code (i.e. CPU instructions as abreviated words), 1st being the 1s and 0s representing the on/off signals the CPU actually receives and sends.


Unfortunately human languages are very complex with lots of ambiguity, not the best thing for a computer to "understand". For these to actually work, you'd need a computer with "true" artificial intelligence, i.e. it would need to "understand" your meaning instead of just following a set of steps.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Shay Gaghe

  • Newt
  • Posts: 85
Re: Coordinates compertion
« Reply #23 on: July 28, 2015, 09:35:15 AM »
how do you map this logic to human language?  :)
You mean something like this:
  • Keep track of the first point and its key in some local variables.
  • Look at each key+point pair in turn, comparing its Y value to the one in that local variable.
  • If the current point's Y is larger than the Y in the local variable, replace the contents of the local variables with the current point and its key.
  • Once all points have finished being compared, the local variables should now contain the values of the largest point, so return them as the last statement in the function.
I was always thinking there was  a way to tell the machine what to do as it was your employee, but I cant really see it possible, (otherwise there wasnt a need for programing language i guess).
That's still just a pipe dream ... one that was thought up in the 50s - 60s: What's referred to as a 4th generation programming language, i.e. a natural human language. 3rd being the normal programming languages we use mostly (e.g. Lisp / C / Python / etc.), 2nd being assembly code (i.e. CPU instructions as abreviated words), 1st being the 1s and 0s representing the on/off signals the CPU actually receives and sends.


Unfortunately human languages are very complex with lots of ambiguity, not the best thing for a computer to "understand". For these to actually work, you'd need a computer with "true" artificial intelligence, i.e. it would need to "understand" your meaning instead of just following a set of steps.

yaa.. that why a robot can never replace a humen... lets cheers for that  :angel:

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Coordinates compertion
« Reply #24 on: July 28, 2015, 10:04:38 AM »
Another sort solution:
Code - Auto/Visual Lisp: [Select]
  1. ; (testSort2 lst)
  2. (defun testSort2 (lst / tmp)
  3.   (setq tmp
  4.     (vl-sort
  5.       (mapcar
  6.         '(lambda (sub)
  7.           (list
  8.             (car sub)
  9.             (vl-sort
  10.               (cadr sub)
  11.               '(lambda (a b) (> (cadr a) (cadr b)))
  12.             )
  13.           )
  14.         )
  15.         lst
  16.       )
  17.       '(lambda (a b) (> (cadr (caadr a)) (cadr (caadr b))))
  18.     )
  19.   )
  20.   (list (caar tmp) (caadar tmp))
  21. )

Recursive solution:
Code - Auto/Visual Lisp: [Select]
  1. ; (TestRecursive lst nil nil)
  2. (defun TestRecursive (lst key pt)
  3.   (cond
  4.     ((not lst)
  5.       (list key pt)
  6.     )
  7.     ;; No more points in sub:
  8.     ((not (cadar lst))
  9.       (TestRecursive (cdr lst) key pt)
  10.     )
  11.     ;; Check if current point has higher Y:
  12.     (
  13.       (>
  14.         (cadr (caadar lst))
  15.         (cadr pt)
  16.       )
  17.       (TestRecursive
  18.         (cons
  19.           (list (caar lst) (cdadar lst)) ; Remove point from current sub.
  20.           (cdr lst)
  21.         )
  22.         (caar lst)
  23.         (caadar lst)
  24.       )
  25.     )
  26.     (T
  27.       (TestRecursive
  28.         (cons
  29.           (list (caar lst) (cdadar lst))
  30.           (cdr lst)
  31.         )
  32.         key
  33.         pt
  34.       )
  35.     )
  36.   )
  37. )

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Coordinates compertion
« Reply #25 on: July 28, 2015, 11:51:48 AM »
Actually I would tend to go about it in a functional way - i.e. I'd want a reduce function (but AutoLisp doesn't have such built in). Thus I'd make one for myself. Here's a simplistic (imperative) variant which works in pretty much the same way as Lee's original code:
Code - Auto/Visual Lisp: [Select]
  1. (defun reduce (operation lst / accumulator)
  2.   (setq accumulator (car lst) operation (eval operation))
  3.   (foreach item (cdr lst)
  4.     (setq accumulator (operation accumulator item)))
  5.   accumulator)
Then I could use that by applying a custom operation onto it - the operation only ever needs to compare two inputs and return whichever it deems the "correct" or some modified value (e.g. when summing a list). In this example case you're looking for the highest Y value inside a list of points. So:
Code - Auto/Visual Lisp: [Select]
  1. (defun PointMaxY (pt1 pt2)
  2.   (if (> (cadr pt2) (cadr pt1)) pt2 pt1))
  3.  
  4. ; Test code
  5. _$ (reduce 'PointMaxY '((1 2 3) (4 3 2) (2 0 6)))
  6. (4 3 2)

Of course, that's just half the issue in your OP. And that's where the map+reduce gives the power behind all this. E.g. extracting the "highest" point from each item in the keyed list:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq lst '(
  2.         ("a" ((0 2 4) (0 7 0) (0 2 0)))
  3.         ("b" ((0 2 0) (0 5 0) (0 2 0)))
  4.         ("c" ((3 2 0) (0 2 0) (9 3 0)))))
  5. (("a" ((0 2 4) (0 7 0) (0 2 0))) ("b" ((0 2 0) (0 5 0) (0 2 0))) ("c" ((3 2 0) (0 2 0) (9 3 0))))
  6. _$ (mapcar '(lambda (item) (list (car item) (reduce 'PointMaxY (cadr item)))) lst)
  7. (("a" (0 7 0)) ("b" (0 5 0)) ("c" (9 3 0)))

And then to extract the highest of each, another reduce on top of that:
Code - Auto/Visual Lisp: [Select]
  1. (reduce '(lambda (item1 item2)
  2.            (if (> (cadadr item2) (cadadr item1)) item2 item1))
  3.         (mapcar '(lambda (item)
  4.                    (list (car item) (reduce 'PointMaxY (cadr item))))
  5.                 lst))
  6. ("a" (0 7 0))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Coordinates compertion
« Reply #26 on: July 28, 2015, 01:37:30 PM »
Good generalisation Irné  :-)

For fun, here's another recursive solution, hampered slightly by the data format:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( lst )
  2.     (   (lambda ( bar ) (bar (apply 'append (mapcar '(lambda ( x ) (mapcar '(lambda ( y ) (list (car x) y)) (cadr x))) lst))))
  3.         (lambda ( lst )
  4.             (if (cdr lst)
  5.                 (if (< (car (cdadar lst)) (cadr (cadadr lst)))
  6.                     (bar (cdr lst))
  7.                     (bar (cons (car lst) (cddr lst)))
  8.                 )
  9.                 (car lst)
  10.             )
  11.         )
  12.     )
  13. )

(EDIT: Updated as per Roy's suggestion below)
« Last Edit: July 29, 2015, 05:15:33 AM by Lee Mac »

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Coordinates compertion
« Reply #27 on: July 28, 2015, 03:43:11 PM »
@ Lee: take another look at line 6 of the code in your last post:
Code: [Select]
(cons (cadr lst) (cddr lst)) :-o

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Coordinates compertion
« Reply #28 on: July 28, 2015, 03:47:20 PM »
@ Lee: take another look at line 6 of the code in your last post:
Code: [Select]
(cons (cadr lst) (cddr lst)) :-o

Sorry roy, I don't follow?  :?

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Coordinates compertion
« Reply #29 on: July 29, 2015, 02:56:12 AM »
@ Lee: take another look at line 6 of the code in your last post:
Code: [Select]
(cons (cadr lst) (cddr lst)) :-o

Sorry roy, I don't follow?  :?
Code: [Select]
(setq lst '(0 1 2 3 4))
(cons (cadr lst) (cddr lst)) => (1 2 3 4)
(cdr lst) => (1 2 3 4)

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Coordinates compertion
« Reply #30 on: July 29, 2015, 05:11:33 AM »
@ Lee: take another look at line 6 of the code in your last post:
Code: [Select]
(cons (cadr lst) (cddr lst)) :-o

Sorry roy, I don't follow?  :?
Code: [Select]
(setq lst '(0 1 2 3 4))
(cons (cadr lst) (cddr lst)) => (1 2 3 4)
(cdr lst) => (1 2 3 4)

Ah - of course! Thanks roy.

Shay Gaghe

  • Newt
  • Posts: 85
Re: Coordinates compertion
« Reply #31 on: July 29, 2015, 08:05:26 AM »
Lee and Roy...can u speak English please  :police: :police: :police:

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Coordinates compertion
« Reply #32 on: July 29, 2015, 08:29:55 AM »
Lee and Roy...can u speak English please  :police: :police: :police:
Maybe you don't know the meaning of '=>'?
'=>' means: 'The return value of the previous code is:'

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Coordinates compertion
« Reply #33 on: July 29, 2015, 08:36:04 AM »
@ Shay:
Can you explain how/why you create this list?:
Code: [Select]
(setq lst
  '(
    ("a" ((0 2 4) (0 7 0) (0 2 0)))
    ("b" ((0 2 0) (0 5 0) (0 2 0)))
    ("c" ((3 2 0) (0 2 0) (9 3 0)))
  )
)

bruno_vdh

  • Guest
Re: Coordinates compertion
« Reply #34 on: July 29, 2015, 09:17:37 AM »
Hello,
Irneb reading your excellent speech, makes me want to continue on this path
By adding arguments to functions "Reduce" and "PointMaxY"

Code: [Select]
(defun reduceKey (operation funKey lst)
  (if (cdr lst)
    (reduceKey operation funKey (cons ((eval operation) funKey (car lst) (cadr lst)) (cddr lst)))
    (car lst)
  )
)

(defun PointMaxRank (Rank pt1 pt2)
  (setq Rank (eval Rank))
  (if (> (Rank pt2) (Rank pt1))
    pt2
    pt1
  )
)

Code: [Select]
(("a" ((0 2 4) (0 7 0) (0 2 0))) ("b" ((0 2 0) (0 5 0) (0 2 0))) ("c" ((3 2 0) (0 2 0) (9 3 0))))
_$ (reduceKey 'PointMaxRank 'cadadr (mapcar '(lambda (item) (list (car item) (reduceKey 'PointMaxRank 'cadr (cadr item)))) lst))
("a" (0 7 0))

Regards,

bruno_vdh

  • Guest
Re: Coordinates compertion
« Reply #35 on: July 29, 2015, 10:46:05 AM »
An equivalent of the foreach Version proposed by Lee, with mapcar ...
Code: [Select]
(defun fun (lst / key pt)
  (mapcar '(lambda (l)
             (mapcar '(lambda (xyz) (if (< (cadr pt) (cadr xyz)) (setq pt  xyz  key (car l))))
                     (cadr l)
             )
           )
          lst
  )
  (list key pt)
)

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Coordinates compertion
« Reply #36 on: July 29, 2015, 10:53:16 AM »
Hello,
Irneb reading your excellent speech, makes me want to continue on this path
By adding arguments to functions "Reduce" and "PointMaxY"
I think that Irneb (reduce) function is intended as a generic function. Adding an argument, as in your (reduceKey), makes the function less so.

bruno_vdh

  • Guest
Re: Coordinates compertion
« Reply #37 on: July 29, 2015, 11:04:33 AM »
I think that Irneb (reduce) function is intended as a generic function. Adding an argument, as in your (reduceKey), makes the function less so.
Yes, I totally agree with that
« Last Edit: July 29, 2015, 11:48:50 AM by bruno_vdh »

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Coordinates compertion
« Reply #38 on: July 30, 2015, 04:29:04 AM »
Inspired by irneb's REDUCE:

An implementation of FOLD may also be of use. Fold belongs to the same 'family' of functions as Reduce.
(Fold is) The fundamental list iterator.

Recursive implementation:
Code - Auto/Visual Lisp: [Select]
  1. (defun fold (func val lst)
  2.   (if lst
  3.     (fold func (func (car lst) val) (cdr lst))
  4.     val
  5.   )
  6. )

Some examples taken from http://srfi.schemers.org/srfi-1/srfi-1.html#FoldUnfoldMap:
Code - Auto/Visual Lisp: [Select]
  1. ; Add up the elements of lst:
  2. (setq lst '(0 1 2 3 4))
  3. (fold + 0 lst) => 10
  4.  
  5. ; Reverse lst:
  6. (setq lst '(0 1 2 3 4))
  7. (fold cons nil lst) => (4 3 2 1 0)
  8.  
  9. ; How many symbols in lst:
  10. (setq lst '(0 a 2 b 4 c))
  11. (fold (lambda (x cnt) (if (= (type x) 'sym) (1+ cnt) cnt)) 0 lst) => 3
  12.  
  13. ; Length of the longest string in lst:
  14. (setq lst '("a" "aa" "aaa" "aaaa" "a" "aa" "aaaaaaa" "a"))
  15. (fold (lambda (str val) (max (strlen str) val)) 0 lst) => 7

The current problem:
Code - Auto/Visual Lisp: [Select]
  1. (setq lst
  2.   '(
  3.     ("a" ((0 2 4) (0 7 0) (0 2 0)))
  4.     ("b" ((0 2 0) (0 5 0) (0 2 0)))
  5.     ("c" ((3 2 0) (0 1 0) (9 3 0)))
  6.   )
  7. )
  8. (defun PointMaxY (pt1 pt2) ; Irneb.
  9.   (if (> (cadr pt2) (cadr pt1)) pt2 pt1)
  10. )
  11. (defun MainFunc (sub val / pt)
  12.   (setq pt (fold PointMaxY (caadr sub) (cdadr sub)))
  13.   (if
  14.     (or
  15.       (not val)
  16.       (> (cadr pt) (cadadr val))
  17.     )
  18.     (list (car sub) pt)
  19.     val
  20.   )
  21. )
  22. (fold MainFunc nil lst) => ("a" (0 7 0))

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Coordinates compertion
« Reply #39 on: July 30, 2015, 04:32:12 AM »
roy, I think you'd enjoy this thread: http://www.theswamp.org/index.php?topic=37362  :wink:

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Coordinates compertion
« Reply #40 on: July 30, 2015, 06:18:03 AM »
Inspired by irneb's REDUCE:
Nice!  8)


There's quite a few of these "standard" functions which ADesk omitted when they "copied" the original XLisp from the 70s. As per that link Lee shows ... trying to add them back because a later language (F#) had actually implemented them as it saw them in the 50+ year old Lisps.  ;D
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Coordinates compertion
« Reply #41 on: July 30, 2015, 10:31:53 AM »
roy, I think you'd enjoy this thread: http://www.theswamp.org/index.php?topic=37362  :wink:
Thanks for pointing that topic out. Food for thought indeed...

Shay Gaghe

  • Newt
  • Posts: 85
Re: Coordinates compertion
« Reply #42 on: August 01, 2015, 08:29:33 AM »
@ Shay:
Can you explain how/why you create this list?:
Code: [Select]
(setq lst
  '(
    ("a" ((0 2 4) (0 7 0) (0 2 0)))
    ("b" ((0 2 0) (0 5 0) (0 2 0)))
    ("c" ((3 2 0) (0 2 0) (9 3 0)))
  )
)

Hi roy

Ill try to explain what im doing,

Im from the constriction discipline, my task is to construct a supporting wall array between two planed ground levels, the one that behind the wall and the other is in front of the wall , (there is “exist” line but its off-topic now) Each ground level is represented by a line.

I distribute points along those lines and retrieve those points as a list associated with a key, by comparing their Y values I can know:

  • Which point is the highest/lowest in the wall?
    What line owns the highest/lowest point?

In order to get the answer I must use association list. In fact I need first to sort out which point is highest/lowest in which line, and which line has the highest/lowest point among all three lines.
There are some situation where the point will be rejected if its owner is “exist” , in that case the second lowest/height point will be tested.

Code: [Select]
(setq lst
   '(
        ("front" ((0 3 4) (0 4 0) (0 2 0)))
        ("back" ((0 1 3) (0 5 0) (0 3 0)))
        ("exist" ((3 2 0) (0 4 0) (9 3 0)))
    )
)

Code: [Select]
(defun foo3 (lst)

  (vl-sort
    (mapcar
      (function
(lambda (grp)

  (list (car grp)
(vl-sort (cadr grp)
(function
   (lambda (a b)
     (> (cadr a) (cadr b))

     )
   )
)
)
  )
)
      lst
      )
    (function
      (lambda (a b)
(> (cadr (caadr a)) (cadr (caadr b)))
)
      )
    )
  )

Code: [Select]
(("back" ((0 5 0) (0 3 0) (0 1 3))) ("front" ((0 4 0) (0 3 4) (0 2 0))) ("exist" ((0 4 0) (9 3 0) (3 2 0))))
how to shorten it?

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Coordinates compertion
« Reply #43 on: August 03, 2015, 04:21:26 AM »
@ Shay:
I have a hard time picturing your problem (design of retaining walls?). But that is not very important. The fact is that you are the programmer who is creating this list structure. It is not thrown at you by the CAD program. You should use a structure that works best for you and whatever algorithm you are planning. It is unlikely that there is only a single option. Maybe you don't see the alternative solutions now but when you look back on the finished program after a couple of years you no doubt will.

One thing I would do is pre-sort the point lists before creating the lst list.

Perhaps it can also make sense to work with 3 variables: ptLstFront, ptLstBack and ptLstExist.

Or perhaps the list structure below can work. As you have learnt from the contributions in this topic, it makes sorting the list much easier.
Code: [Select]
(setq lst
  '(
    ("front" (0 3 4))
    ("front" (0 4 0))
    ("front" (0 2 0))
    ("back"  (0 1 3))
    ("back"  (0 5 0))
    ("back"  (0 3 0))
    ("exist" (3 2 0))
    ("exist" (0 4 0))
    ("exist" (9 3 0))
  )
)