Author Topic: (Challenge) bounding box of the points list.  (Read 4849 times)

0 Members and 1 Guest are viewing this topic.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
(Challenge) bounding box of the points list.
« on: February 20, 2007, 09:31:38 AM »
You have a list of points:

Code: [Select]
(while (setq p (getpoint "\n  Specify point:  ")) (setq pt-list (cons p pt-list)))Example:
Code: [Select]
(setq pt-list '((0. 5. 0.) (1. 10. 0.) (-2. 0. 10.) (-18. 3. -4.)))
;; Explanation - all point 3D

It is necessary to receive the dimensional container of this list
As vla-GetBoundingBox, only list point...

Write two programs:

The first program: - calculates 3D list point
Code: [Select]
(GetBoundingBox-3d pt-list) = >> '((-18.0 0.0 -4.0) (1.0 10.0 10.0))
;; Explanation - return '((min-X min-Y min-Z) (max-X max-Y max-Z))

The second program: - calculates 2D list point
Code: [Select]
(GetBoundingBox-2d pt-list) = >> '((-18.0 0.0) (1.0 10.0))
;; Explanation - return '((min-X min-Y) (max-X max-Y))

PS. Last time, I have written not clearly...  :oops:
Ask also I shall explain once again!   :-)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: (Challenge) bounding box of the points list.
« Reply #1 on: February 20, 2007, 10:35:26 AM »
Hi,

Here's my contribution.

Code: [Select]
(defun GetBoundingBox-3d (pt_lst)
  (list (apply 'mapcar (cons 'min pt_lst))
(apply 'mapcar (cons 'max pt_lst))
  )
)

Code: [Select]
(defun GetBoundingBox-2d (pt_lst)
  ((lambda (l)
     (list
       (apply 'mapcar (cons 'min l))
       (apply 'mapcar (cons 'max l))
     )
   )
    (mapcar '(lambda (p) (list (car p) (cadr p))) pt_lst)
  )
)
Speaking English as a French Frog

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) bounding box of the points list.
« Reply #2 on: February 20, 2007, 10:41:56 AM »
Hello gile!
My function GetBoundingBox-3d is identical to yours!
But my function GetBoundingBox-2d is easier than yours...

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: (Challenge) bounding box of the points list.
« Reply #3 on: February 20, 2007, 10:50:36 AM »
Another way for GetBoundingBox-2d

Code: [Select]
(defun GetBoundingBox-2d (pt_lst)
 (mapcar '(lambda (p) (reverse (cdr (reverse p))))
     (list
       (apply 'mapcar (cons 'min l))
       (apply 'mapcar (cons 'max l))
     )
   )
)
Speaking English as a French Frog

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) bounding box of the points list.
« Reply #4 on: February 20, 2007, 10:56:26 AM »
Already better! :)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) bounding box of the points list.
« Reply #5 on: February 20, 2007, 11:42:23 AM »
Already better! :)

I wished to tell, that it is possible to reduce quantity of calculations...

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) bounding box of the points list.
« Reply #6 on: February 21, 2007, 03:21:19 PM »
Another way for GetBoundingBox-2d

Code: [Select]
(defun GetBoundingBox-2d (pt_lst)
 (mapcar '(lambda (p) (reverse (cdr (reverse p))))
     (list
       (apply 'mapcar (cons 'min l))
       (apply 'mapcar (cons 'max l))
     )
   )
)

my variant:
Code: [Select]
(defun GetBoundingBox-2d (pt_lst)
 ((lambda (l)
   (list
    (apply 'mapcar (cons 'min l))
    (apply 'mapcar (cons 'max l))
   ) ;_  list
  ) ;_  lambda
  (cons (list (caar pt_lst) (cadar pt_lst))
        (cdr pt_lst)
  ) ;_  cons
 )
) ;_  defun

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: (Challenge) bounding box of the points list.
« Reply #7 on: February 21, 2007, 03:47:34 PM »
Nice one, Evgeniy. :-)

I have to learn more about ((lambda ...) using  :roll:
Speaking English as a French Frog

efernal

  • Bull Frog
  • Posts: 206
Re: (Challenge) bounding box of the points list.
« Reply #8 on: February 22, 2007, 12:49:14 PM »
Code: [Select]
;; So, we can draw a box with these points...
(DEFUN _3dbbox (points_list / pa pb pc pd pe pf pg)
  (IF (AND (= (LENGTH points_list) 2)
           (= (TYPE (CAR points_list)) 'LIST)
           (= (TYPE (CADR points_list)) 'LIST)
           (= (LENGTH (CAR points_list)) 3)
           (= (LENGTH (CADR points_list)) 3)
           (/= (LAST (CAR points_list)) (LAST (CADR points_list)))
      )
    (PROGN (SETQ pa (CAR points_list)
                 pg (CADR points_list)
                 ;;
                 pb (LIST (CAR pg) (CADR pa) (CADDR pa))
                 pc (LIST (CAR pg) (CADR pg) (CADDR pa))
                 pd (LIST (CAR pa) (CADR pg) (CADDR pa))
                 ;;
                 pe (LIST (CAR pa) (CADR pa) (CADDR pg))
                 pf (LIST (CAR pb) (CADR pb) (CADDR pg))
                 ph (LIST (CAR pd) (CADR pd) (CADDR pg))
           )
           (COMMAND "_.PFACE")
           (FOREACH x (LIST pa pb pc pd pe pf pg ph) (COMMAND "_NON" x))
           (COMMAND ""                  ;
                    1 2 3 4 ""          ;
                    5 6 7 8 ""          ;
                    1 2 6 5 ""          ;
                    2 3 7 6 ""          ;
                    3 4 8 7 ""          ;
                    4 1 5 8 ""          ;
                    "")
    )
    nil
  )
  (PRINC)
)
;; Usage = (_3dbbox (list pa pb)) where pa and pb are 3d points, ie: (0 0 0) and (100 100 100)
e.fernal

Joe Burke

  • Guest
Re: (Challenge) bounding box of the points list.
« Reply #9 on: February 23, 2007, 09:10:52 AM »
Here's an old one by Tony Tanzillo. Obviously similar to code already posted, but I'm not sure about how it differs.

It works well for me.

;; Returns the lower left and upper right corners of a point list.
(defun extents (plist)
   (list
      (apply 'mapcar (cons 'min plist))
      (apply 'mapcar (cons 'max plist))
   )
)