Author Topic: Furthest Points in a list  (Read 1596 times)

0 Members and 1 Guest are viewing this topic.

LSElite

  • Newt
  • Posts: 65
Furthest Points in a list
« on: November 21, 2015, 06:03:11 PM »
Hey All

I have a list of points approximately in a line, and I want to find the furthest 2 points.
I can achieve this using 2 foreach loops however I believe there is a more efficient way using mapcar and sort.
I am still wrapping my mind around the diversity of the list processing functions and a few solutions would be interesting to see.:-D

Cheers
“A life spent making mistakes is not only more honorable, but more useful than a life spent doing nothing.”
― George Bernard Shaw

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Furthest Points in a list
« Reply #1 on: November 21, 2015, 08:21:10 PM »

Depends what you mean by furthest ?

Most X ?
Most Y ?
Most Z ?

or just the last 2 in the list ?
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.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Furthest Points in a list
« Reply #2 on: November 21, 2015, 08:51:59 PM »
Bounding box??

But, as Kerry said, if you just need the last two given some condition (i.e. greatest x,y,z), you could then check the length of the vector of each point from the last(greatest) point and choose the shortest.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

LSElite

  • Newt
  • Posts: 65
Re: Furthest Points in a list
« Reply #3 on: November 21, 2015, 09:01:59 PM »
I need the 2 points furthest from each other.
Below is the solution I found with foreach.
I was just wondering if there was a faster way of doing this with mapcar or any other list functions.

Code: [Select]
(defun FindEndPoints (PtList / dist p1 p2 Output)
(if (> (length PtList) 1)
(progn
(setq dist (list 0))
(foreach p1 ptlist
(foreach p2 ptlist
(if (> (distance p1 p2) (nth 0 dist))
(setq dist (list (distance p1 p2) p1 p2))
)
)
)
(setq Output (list (nth 1 dist) (nth 2 dist)))
)
)
princ Output
)
“A life spent making mistakes is not only more honorable, but more useful than a life spent doing nothing.”
― George Bernard Shaw

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Furthest Points in a list
« Reply #4 on: November 22, 2015, 06:17:38 AM »
One thing that can improve the speed: avoid calculating every distance twice.
Code - Auto/Visual Lisp: [Select]
  1. ; (FindFurthestPoints '((0 0) (1 1) (2 2) (3 3)))
  2. (defun FindFurthestPoints (ptList / dist num p1)
  3.   (if (< 0 (setq num (1- (length ptList))))
  4.     (repeat num
  5.       (setq p1 (car ptList))
  6.       (foreach p2 (setq ptList (cdr ptList)) ; P2 is local to the foreach loop.
  7.         (if (< (car dist) (distance p1 p2))  ; There is no problem if dist=nil. Nil is smaller than any number.
  8.           (setq dist (list (distance p1 p2) p1 p2))
  9.         )
  10.       )
  11.     )
  12.   )
  13.   (cdr dist)
  14. )