;| RandomPoints.lsp generates random 3D points within a unit cube centered on WCS origin Options: Random points in a sphere, rectangular volume, or ellipsoid. Notes: Fewer points yield more irregular shapes. Thanks to Takehiko Nagakura (e-mail: takehiko@mit.edu) Massachusetts Institute of Technology for (rand16) function, adapted from Kernighan and Ritchie algorithm. (c) 2008 by Bill Gilliss bill [dot] gilliss [at] aya.yale.edu version 1.00: 09/09/2008 - published on autodesk.autocad.customization 1.01: 09/13/2008 - fixed initgets and scoping 1.02: 09/17/2008 - really *did* fix the initgets this time - Thanx, Jason Comments and suggestions always welcome. |; (defun c:randompoints ( ) ; (/ num shape xfac yfac zfac n x y z *Seedrand*) (command "._undo" "_begin") (setq RandPts (ssadd)) ;; set to hold points for later manipulation (setq xfac 1.0 yfac 1.0 zfac 1.0) (initget 7) (setq num (getreal "How many points: ")) (initget "Sphere Ellipsoid Rectangle Cube") (setq shape (getkword "Shape [Sphere/Ellipsoid/Rectangle/Cube] : ")) (if (not shape) (setq shape "Cube")) (if (or (= shape "Ellipsoid") (= shape "Rectangle")) (progn (initget 6) (setq xfac (getdist "\nLength along X-axis <1.0>: ")) (if (not xfac) (setq xfac 1.0)) (initget 6) (setq yfac (getdist "\nLength along Y-axis <1.0>: ")) (if (not yfac) (setq yfac 1.0)) (initget 6) (setq zfac (getdist "\nLength along Z-axis <1.0>: ")) (if (not zfac) (setq zfac 1.0)) ) ) (setq n 0) (while (< n num) (setq x (- (rand16) 0.5) y (- (rand16) 0.5) z (- (rand16) 0.5)) ;;center on 0,0 (cond ((or (= shape "Sphere") (= shape "Ellipsoid")) (if (< (+ (* x x) (* y y) (* z z)) (/ pi 16)) ;;if inside unit sphere, keep point (progn (setq x (* x xfac) y (* y yfac) z (* z zfac)) (entmake (list (cons 0 "POINT") (list 10 x y z))) (ssadd (entlast) RandPts) ) (setq n (1- n));;if outside unit sphere, try another point ) ) ((= shape "Rectangle") (progn (setq x (* x xfac) y (* y yfac) z (* z zfac)) (entmake (list (cons 0 "POINT") (list 10 x y z))) (ssadd (entlast) RandPts) ) ) ( T ;; cube (entmake (list (cons 0 "POINT") (list 10 x y z))) (ssadd (entlast) RandPts) ) );cond (setq n (1+ n)) );while (prompt "Points are in selection set RandPts.") (command "._undo" "_end") (princ) );defun (defun rand16 ( / s rand) (if (null *SeedRand*) (setq s (getvar "date") *SeedRand* (fix (* 86400 (- s (fix s))))) ) ; if (setq *SeedRand* (+ (* *SeedRand* 1103515245) 12345)) (setq rand (logand (/ *SeedRand* 65536) 32767)) (setq rand (/ rand 32767.0)) ;;added to rand16 to return a real between 0 and 1 ) (princ "RandomPoints loaded.") (princ)