Author Topic: Lower left and the upper right from 2 point list  (Read 2133 times)

0 Members and 1 Guest are viewing this topic.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Lower left and the upper right from 2 point list
« on: August 18, 2018, 04:40:08 AM »
Surely it is a small problem that has already been solved… I need to calculate the lower left and the upper right of a rectangle represented by two points:

Code: [Select]
p4-------------p2
|              |
|              |
p1-------------p3

(foo p1 p2) ==> (p1 p2)
(foo p2 p1) ==> (p1 p2)
(foo p3 p4) ==> (p1 p2)
(foo p4 p3) ==> (p1 p2)

returns a list of 2 points the lower left and the upper right

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Lower left and the upper right from 2 point list
« Reply #1 on: August 18, 2018, 05:37:34 AM »
Hi,

Code - Auto/Visual Lisp: [Select]
  1. (defun foo (p1 p2)
  2.   (list (mapcar 'min p1 p2) (mapcar 'max p1 p2))
  3. )
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Lower left and the upper right from 2 point list
« Reply #2 on: August 18, 2018, 05:45:55 AM »
More generic, with a point list:

Code - Auto/Visual Lisp: [Select]
  1. (defun bar (ptLst)
  2.   (list (apply 'mapcar (cons 'min ptLst)) (apply 'mapcar (cons 'max ptLst)))
  3. )
Speaking English as a French Frog


Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Lower left and the upper right from 2 point list
« Reply #4 on: August 18, 2018, 08:03:47 AM »
Nice one, gile!

Previously I had to assemble this and use it for a similar purpose as Marc's:

Code - Auto/Visual Lisp: [Select]
  1. ;| Visualisation Test
  2. (defun C:test ( / r i )
  3.   (defun M-Text (pt str) (entmakex (list (cons 0 "MTEXT")(cons 100 "AcDbEntity")(cons 100 "AcDbMText")(cons 10 pt)(cons 1 str)))) ; LM
  4.   (and (setq r (GetRec)) (setq i 0) (mapcar '(lambda (x) (M-Text x (itoa (setq i (1+ i))))) r))
  5.   (princ)
  6. )
  7. |;
  8. ; Prompts for 2 points, that define the diagonal and returns a sorted list of 4 points that describe the corners
  9. ; Returns a list of: '(ll ul ur lr) points, visualised like this:
  10. ; 2 3
  11. ; 1 4
  12. (defun GetRec ( / p1 p2 xL yL p3 p4 r )
  13.   (and
  14.     (setq p1 (getpoint "\nFirst corner: "))
  15.     (setq p2 (getcorner "\nSecond corner: " p1))
  16.     (mapcar (function set) '(p1 p2) (mapcar '(lambda (x) (trans x 1 0)) (list p1 p2)))
  17.     (setq xL (mapcar 'car (list p1 p2)))
  18.     (setq yL (mapcar 'cadr (list p1 p2)))
  19.     (mapcar 'set '(p3 p4) (mapcar 'list xL (reverse yL)))
  20.     (setq r
  21.       (apply (function (lambda (a b c d) (list a b d c)))
  22.         (
  23.           (lambda ( L ) ; Lee Mac
  24.             (vl-sort L
  25.               (function
  26.                 (lambda ( a b )
  27.                   (while (and a b (= (car a) (car b))) (setq a (cdr a) b (cdr b)))
  28.                   (< (car a) (car b))
  29.                 )
  30.               )
  31.             )
  32.           )
  33.           (list p1 p2 p3 p4)
  34.         )
  35.       )
  36.     ); setq r
  37.   ); and
  38.   r
  39. ); defun GetRec

But it was about sorting the points from the rectangle in the same way, no matter the input/direction of the both points.
However in my main I just used p1 and p2, just like Marc, so now I can condense it! :)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg


BIGAL

  • Swamp Rat
  • Posts: 1434
  • 40 + years of using Autocad
Re: Lower left and the upper right from 2 point list
« Reply #6 on: August 18, 2018, 07:45:27 PM »
The bounding box function can be helpful also in case of rectang gives the answer required.
Code: [Select]
;; by Gilles Chanteau
;; gc:UcsBoundingBox
;; Returns the UCS coordinates of the object bounding box about current UCS
A man who never made a mistake never made anything

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Lower left and the upper right from 2 point list
« Reply #7 on: August 19, 2018, 01:14:11 PM »
Another variant for a list of points:
Code: [Select]
(defun foo ( lst )
    (mapcar '(lambda ( x ) (apply 'mapcar (cons x lst))) '(min max))
)