brams Posted May 25, 2012 Posted May 25, 2012 This is what i found with google, and i added random colours for points - and it doesn't work... help! What's wrong? (defun randnum (/ modulus multiplier increment random) (if (not seed) (setq seed (getvar "DATE"))) (setq modulus 65536 multiplier 25173 increment 13849 seed (rem (+ (* multiplier seed) increment) modulus) random (* 1 (/ seed modulus)))) (defun rangd (/ ); = Random ANGle, Degrees ;; takes last 6 digits of DATE System Variable, moves last one ;; [which changes fastest] to front for greater randomization ;; at close-together calls, makes 6-digit decimal between 0 & 1 ;; of that, multiplies x 360 degrees (setq digits (substr (rtos (rem (getvar 'date) 1) 2 16) 13)) (* (atof (strcat "0." (substr digits 6) (substr digits 1 5))) 1) ); end defun (defun C:RPS (/ cmd num rad orig); = Random Points on a Sphere (command "_.ucs" "_del" "RPStemp") (command "_.ucs" "_world") (setq cmd (getvar 'cmdecho)) (setvar 'cmdecho 0) (command "_.undo" "_begin" "_.ucs" "_save" "RPStemp" ); end command (setq num (getint "Number of points: ") rad (getdist "Radius of sphere: ") orig (getpoint "Center of sphere: ") ); end setq (command "_.ucs" "_origin" orig) (repeat num (command "_.ucs" "_X" (* 360 (rangd)) "_.ucs" "_Y" (* 360 (rangd)) "_.ucs" "_Z" (* 360 (rangd)) ); end command (setq col (abs(fix (* 256 (rangd))))) (command "-color" col) (entmake (list '(0 . "POINT") (cons 10 (trans (polar (trans orig 0 1) 0 rad) 1 0)) ); end list ); end entmake ); end repeat (command "_.ucs" "_restore" "RPStemp" "_.ucs" "_del" "RPStemp" "._undo" "_end" ); end command (setvar 'cmdecho cmd) (princ) ); end defun Quote
MSasu Posted May 25, 2012 Posted May 25, 2012 It works on my stations - the points are equally distanced from selected center, so they lay on a sphere; also the colors seems to follow a quite random pattern. What I suspect is that the color issue is related to the speed of your workstation; to rely on computer's clock to get random numbers on a powerful station isn't very reliable since then you will not got truly random numbers - please check the example below: (repeat 10 (princ (strcat "\n" (rtos (rangd) 2 5)))) You may check on Forum for a better random generator (it was a similar thread not long ago) or can add a delay between point generations. Quote
brams Posted May 25, 2012 Author Posted May 25, 2012 this is what i get : Invalid color number. ; error: Function cancelled strange... Please try with ...10000 points Quote
pBe Posted May 25, 2012 Posted May 25, 2012 perhaps change the pdmode to 3 so you can see the points brams Quote
brams Posted May 25, 2012 Author Posted May 25, 2012 Is working a little... for few times i succeed to put 5000 points...then error Quote
MSasu Posted May 25, 2012 Posted May 25, 2012 Maybe add a protection to don't get unsupported color indexes? [color=red](setq col 0)[/color] [color=red](while[/color] [color=red](or (= col 0) (> col 256))[/color] (setq col (abs (fix (* 256 (rangd)))))[color=red])[/color] Quote
pBe Posted May 25, 2012 Posted May 25, 2012 oh... silly me (setq color [b][color=blue](_col rangd))[/color][/b] (defun _col (ran) (setq c (abs(fix (* 256 (ran))))) (if (< 0 c 256) c (_col ran))) Quote
brams Posted May 25, 2012 Author Posted May 25, 2012 Now is working, Pbe. Why not before, with (rangd) ? Quote
pBe Posted May 25, 2012 Posted May 25, 2012 as noted by MSasu if varaible color value is not within the valid range of 1-255 (ACI) it will not recognize that value and the command color will generate an error Quote
brams Posted May 25, 2012 Author Posted May 25, 2012 (rangd) has value between 0 and 1,then i multiply with 256, i use abs. and fix - how can be out of range 1- 256? Quote
pBe Posted May 25, 2012 Posted May 25, 2012 (edited) for example when its "0" , try assigning color 0 (command "_color" 0) BTW: where dd you get theat cool routine? EDIT: i did not really test rangd value if it ever exceeds 255 after evaluation .. but the snippet we posted will prevent that anyway. Edited May 25, 2012 by pBe Quote
MSasu Posted May 25, 2012 Posted May 25, 2012 (rangd) has value between 0 and 1,then i multiply with 256, i use abs. and fix - how can be out of range 1- 256? Since may generate a 0 value. Quote
pBe Posted May 25, 2012 Posted May 25, 2012 How can i make a cube instead of sphere? Now you're pushing your luck brams I would suggest you contact the original author to do just that. he probably have one by now. Cheers Quote
brams Posted May 25, 2012 Author Posted May 25, 2012 http://208.74.205.69/t5/Visual-LISP-AutoLISP-and-General/Random-points-on-a-sphere-NOT/td-p/2891702 Quote
MSasu Posted May 25, 2012 Posted May 25, 2012 You may also want to take a look to a better random numbers generator - check post #6 from this thread. Quote
MSasu Posted May 25, 2012 Posted May 25, 2012 How can i make a cube instead of sphere? For sure that is possible to distribute the points on the faces of a cube – you should draw one face at a time, generate randomly X and Y coordinates pairs around the “center” of the face and validate their location to don’t exceed the limits of the face (should account for half of cube's edge and the cosines of the angle between “center” and current point). (<= (distance pointCenter pointRandom) (/ (* 0.5 edgeSquare) (cos (angle pointCenter pointRandom)))) Quote
pBe Posted May 25, 2012 Posted May 25, 2012 Did you test your theory yet MSasu? It would be intersting to see how it works. sine/cosines makes my head spin so bad i wont even try. Quote
MSasu Posted May 25, 2012 Posted May 25, 2012 If I will have time this evening, maybe I will try to write one. The validation should be refined to account for points located on the same vertical with the “center”: (setq theAngle (angle pointCenter pointRandom)) (<= (distance pointCenter pointRandom) (/ (* 0.5 edgeSquare) (if (member theAngle (list (* 0.5 pi) (* 1.5 pi))) 1.0 (abs (cos theAngle))))) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.