turtles-own [x-pos ;; x-pos, y-pos, and z-pos are the cartesian coordinates y-pos ;; don't confuse them with xcor and ycor, which are predefined NetLogo variables for turtles z-pos p ;; p, theta, and phi are the spherical coordinates theta phi temp-alpha ;; used to store temporary random values temp-beta temp-gamma ] to setup-sphere ca ;; generate a sphere of radius SHAPE-SIZE cct num-turtles [set p shape-size ; all points are equal distance from the center set theta random-float 360 ; however, random distribution on the surface of the sphere set phi random-float 180 render-turtle ] end ; a filled 3-d cube to setup-cube-filled ca ;; generate a square with edge of length radius ;; placing a point randomly anywhere inside the square cct num-turtles [cartset ((- shape-size) + 2 * (random-float shape-size)) ((- shape-size) + 2 * (random-float shape-size)) ((- shape-size) + 2 * (random-float shape-size)) render-turtle ] end ; cube with turtles only on its surface to setup-cube-surface ca cct num-turtles [set temp-alpha shape-size * (1 - 2 * (random 2)) ; +-shape-size set temp-beta shape-size - 2 * (random-float shape-size) ; random distribution bounded by +-shape-size set temp-gamma (random 2) ; front/back or left/right? ifelse temp-gamma = 0 ; generating the front and back surfaces [cartset (temp-alpha) (temp-beta) (shape-size - (2 * (random-float shape-size))) ] [cartset (temp-beta) ; generating the side surfaces (temp-alpha) (shape-size - (2 * (random-float shape-size))) ] render-turtle ] end ; 3-d cone to setup-cone ca cct num-turtles [set theta (random-float 360) ; points have a random angle set p (random-float shape-size) cartset (p * (cos theta)) ; x = r*cos(theta) (p * (sin theta)) ; y = r*sin(theta) (shape-size - 2 * p) ; this centers the cone at the origin ; instead of it only being in positive space render-turtle ] end ; vertical cylinder to setup-cylinder-v ca ;the code is almost the same as the setup-cone code ;except the xy-plane radius remains constant cct num-turtles [set theta (random-float 360) cartset (shape-size * (cos theta)) (shape-size * (sin theta)) ((- shape-size) + 2 * (random-float shape-size)) render-turtle ] end ; horizontal cylinder to setup-cylinder-h ca ;generates a cylinder in a horizontal position with capped ends cct num-turtles [set temp-alpha (random 3) ; which surface (left, right, or body?) set theta (random-float 360) ifelse temp-alpha = 0 ; generating the actual cylinder [cartset ((- shape-size) + 2 * (random-float shape-size)) (shape-size * (cos theta)) (shape-size * (sin theta)) ] [ifelse temp-alpha = 1 ; generating sides [set temp-alpha -1] ; left side [set temp-alpha 1] ; right side cartset (temp-alpha * shape-size) ((random-float shape-size) * (cos theta)) ((random-float shape-size) * (sin theta)) ] render-turtle ] end to setup-pyramid ca cct num-turtles [set temp-alpha (- shape-size) + 2 * (random-float shape-size) ; z coordinate set theta (random 2) ; front/back or side? set temp-beta (shape-size - temp-alpha) / 2 set temp-gamma -1 + 2 * (random 2) ; left/right or front/back (-1 or 1) ifelse theta = 0 [cartset (temp-beta * temp-gamma) ; generating left/right surfaces ((- temp-beta) + 2 * (random-float temp-beta)) (temp-alpha) ] [cartset ((- temp-beta) + 2 * (random-float temp-beta)) ; generating front/back surfaces (temp-beta * temp-gamma) (temp-alpha) ] render-turtle ] end ;; convert from cartesian to spherical coordinates to cartset [x y z] set p sqrt((x ^ 2) + (y ^ 2) + (z ^ 2)) set phi (atan sqrt((x ^ 2) + (y ^ 2)) z) set theta (atan y x) end to go ; rotate-turtles on z axis ask turtles [set theta ((theta + theta-velocity) mod 360) ; incrementing the angle to simulate rotation render-turtle ] end to render-turtle calculate-turtle-position set-turtle-position end ;; convert from spherical to cartesian coordinates to calculate-turtle-position set y-pos (p * (sin phi) * (sin theta)) set x-pos (p * (sin phi) * (cos theta)) set z-pos (p * (cos phi)) end ;; set the turtle's position and color to set-turtle-position ifelse (view = "side") ; sideview [setxy x-pos z-pos set color scale-color display-color y-pos (- shape-size) shape-size ] [ifelse (view = "top") ; topview [setxy x-pos y-pos set color scale-color display-color z-pos (- shape-size) shape-size ] [setxy (p * (sin phi) * (cos theta)) ; bottomview (- (p * (sin phi) * (sin theta))) set color scale-color display-color (- z-pos) (- shape-size) shape-size ] ] end ; *** NetLogo Model Copyright Notice *** ; ; This model was originally created as part of the project: CONNECTED MATHEMATICS: ; MAKING SENSE OF COMPLEX PHENOMENA THROUGH BUILDING OBJECT-BASED PARALLEL ; MODELS (OBPML). The project gratefully acknowledges the support of the ; National Science Foundation (Applications of Advanced Technologies ; Program) -- grant numbers RED #9552950 and REC #9632612. ; ; Copyright 1998 by Uri Wilensky. All rights reserved. ; ; Permission to use, modify or redistribute this model is hereby granted, ; provided that both of the following requirements are followed: ; a) this copyright notice is included. ; b) this model will not be redistributed for profit without permission ; from Uri Wilensky. ; Contact Uri Wilensky for appropriate licenses for redistribution for ; profit. ; ; This model was converted to NetLogo (and was changed in the process to ; be an alternate version of the model) as part of the project: ; PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN ; CLASSROOMS. The project gratefully acknowledges the support of the ; National Science Foundation (REPP program) -- grant number REC #9814682. ; Converted from StarLogoT to NetLogo, 2001. Updated 2002. ; ; To refer to this model in academic publications, please use: ; Wilensky, U. (1998). NetLogo 3D Shapes model. ; http://ccl.northwestern.edu/netlogo/models/3DShapes. ; Center for Connected Learning and Computer-Based Modeling, ; Northwestern University, Evanston, IL. ; ; In other publications, please use: ; Copyright 1998 by Uri Wilensky. All rights reserved. See ; http://ccl.northwestern.edu/netlogo/models/3DShapes ; for terms of use. ; ; *** End of NetLogo Model Copyright Notice *** @#$#@#$#@ GRAPHICS-WINDOW 298 10 671 404 60 60 3.0 0 10 1 1 1 CC-WINDOW 298 405 671 506 Command Center BUTTON 176 38 269 71 go go T 1 T OBSERVER T SLIDER 158 136 288 169 num-turtles num-turtles 0 2000 500 1 1 NIL SLIDER 158 232 288 265 theta-velocity theta-velocity -10.0 10.0 10.0 1.0 1 NIL BUTTON 19 40 118 73 setup-sphere setup-sphere NIL 1 T OBSERVER T BUTTON 19 280 136 313 setup-cube-filled setup-cube-filled NIL 1 T OBSERVER T SLIDER 158 184 288 217 shape-size shape-size 10.0 40.0 37.6 0.1 1 NIL BUTTON 19 88 118 121 setup-cone setup-cone NIL 1 T OBSERVER T BUTTON 19 136 136 169 setup-cylinder-v setup-cylinder-v NIL 1 T OBSERVER T BUTTON 19 184 134 217 setup-cylinder-h setup-cylinder-h NIL 1 T OBSERVER T BUTTON 19 232 150 265 setup-cube-surface setup-cube-surface NIL 1 T OBSERVER T BUTTON 19 328 118 361 setup-pyramid setup-pyramid NIL 1 T OBSERVER T SLIDER 158 88 288 121 display-color display-color 5.0 135.0 25.0 10.0 1 NIL CHOICE 101 401 193 446 view view "side" "top" "bottom" 0 @#$#@#$#@ WHAT IS IT? ----------- This model maps turtles between cartesian and spherical 3-dimensional coordinates. To create the 3-d shapes the program randomly generates turtles about the shell of the shape in either cartesian (x, y, z) or spherical (theta, phi, z) coordinates, depending on which is easier to accomplish but always stores the information converting when necessary in spherical coordinates. To render the sphere in the NetLogo graphics window, it translates the turtles from spherical to cartesian coordinates using color to simulate depth. The positions of the turtles are always stored as spherical coordinates because they are rotated on the z-axis, and the simplest way to do so is to increase theta in spherical coordinates. Converting from cartesian to spherical coordinates: x = r * cos(theta) = p * sin(phi) * cos(theta) y = r * sin(theta) = p * sin(phi) * sin(theta) z = p * cos(theta) theta: angle of the turtle's projection on the x-y plane. phi: turtles angle of incidence to the z axis. p: distance of the turtle from the origin. HOW TO USE IT ------------- Click the different setup-SHAPE buttons to generate different 3d-shapes. The turtles are randomly distributed about the surface of the shape. Click the go (forever) button to run the model. GO starts rotating the model. COLOR determines the color that is used to simulate depth in generating the various shapes (uses predefined NetLogo color constants). NUM-TURTLES determines the number of turtles that are used to generate the various shapes. SHAPE-SIZE determines the overall size of the shape. Most often it is radius or edge length. THETA-VELOCITY determines the speed at which the turtles are rotated. (Rotating turtles in the rotate-turtles procedure is implemented simply by increasing each turtle's theta variable by theta-velocity! ROTATING turtles (on its z-axis) is easy in spherical coordinates. However it's far easier to TRANSPOSE turtles in cartesian coordinates.) THINGS TO NOTICE ---------------- Notice that turtles closer (positive) on the y-axis appear lighter in shade and turtles further away (negative) appear darker in shade. THINGS TO TRY ------------- Try adjusting the theta-vel or render-color slider as the model is running. This will provide real time feedback to your adjustments. EXTENDING THE MODEL ------------------- [EASY] Adjust the setup-square procedure to generate a rectangle. Create a procedure to transpose turtle coordinates. Remember that it is easier to transpose in cartesian coordinates. Create a procedure to generate new 3D geometries. Try animating the phi variable. Conceptually why does this not make sense? Create a procedure to rotate the geometries on a different axis. [VERY DIFFICULT] Create a procedure to view the geometries at ANY angle instead of the present three. NETLOGO FEATURES ------------------- Notice the use of scale-color to show the depth of turtles and thereby simulate 3-D. CREDITS AND REFERENCES ---------------------- To refer to this model in academic publications, please use: Wilensky, U. (2002). NetLogo 3D Shapes model. http://ccl.northwestern.edu/netlogo/models/3DShapes. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL. In other publications, please use: Copyright 2002 by Uri Wilensky. All rights reserved. See http://ccl.northwestern.edu/netlogo/models/3DShapes for terms of use. @#$#@#$#@ default true 0 Polygon -7566196 true true 150 5 40 250 150 205 260 250 arrow true 0 Polygon -7566196 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150 box true 0 Polygon -7566196 true true 45 255 255 255 255 45 45 45 circle true 0 Circle -7566196 true true 35 35 230 person false 0 Circle -7566196 true true 155 20 63 Rectangle -7566196 true true 158 79 217 164 Polygon -7566196 true true 158 81 110 129 131 143 158 109 165 110 Polygon -7566196 true true 216 83 267 123 248 143 215 107 Polygon -7566196 true true 167 163 145 234 183 234 183 163 Polygon -7566196 true true 195 163 195 233 227 233 206 159 spacecraft true 0 Polygon -7566196 true true 150 0 180 135 255 255 225 240 150 180 75 240 45 255 120 135 thin-arrow true 0 Polygon -7566196 true true 150 0 0 150 120 150 120 293 180 293 180 150 300 150 truck-down false 0 Polygon -7566196 true true 225 30 225 270 120 270 105 210 60 180 45 30 105 60 105 30 Polygon -8716033 true false 195 75 195 120 240 120 240 75 Polygon -8716033 true false 195 225 195 180 240 180 240 225 truck-left false 0 Polygon -7566196 true true 120 135 225 135 225 210 75 210 75 165 105 165 Polygon -8716033 true false 90 210 105 225 120 210 Polygon -8716033 true false 180 210 195 225 210 210 truck-right false 0 Polygon -7566196 true true 180 135 75 135 75 210 225 210 225 165 195 165 Polygon -8716033 true false 210 210 195 225 180 210 Polygon -8716033 true false 120 210 105 225 90 210 turtle true 0 Polygon -7566196 true true 138 75 162 75 165 105 225 105 225 142 195 135 195 187 225 195 225 225 195 217 195 202 105 202 105 217 75 225 75 195 105 187 105 135 75 142 75 105 135 105 @#$#@#$#@ NetLogo 2.0beta4 @#$#@#$#@ setup-sphere @#$#@#$#@ @#$#@#$#@