# 3D Solids

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)

## WHAT IS IT?

This model creates 3D shapes out of 2D turtles by mapping turtles between cartesian and spherical three-dimensional coordinates.

## HOW IT WORKS

To create the 3D 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 view, 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 (around the 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 3D.

## HOW TO CITE

If you mention this model in a publication, we ask that you include these citations for the model itself and for the NetLogo software:

- Wilensky, U. (1998). NetLogo 3D Solids model. http://ccl.northwestern.edu/netlogo/models/3DSolids. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.
- Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.

## COPYRIGHT AND LICENSE

Copyright 1998 Uri Wilensky.

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.

This model was 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.

This model was converted to NetLogo as part of the projects: PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN CLASSROOMS and/or INTEGRATED SIMULATION AND MODELING ENVIRONMENT. The project gratefully acknowledges the support of the National Science Foundation (REPP & ROLE programs) -- grant numbers REC #9814682 and REC-0126227. Converted from StarLogoT to NetLogo, 2001.

## Comments and Questions

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 z-pos ;; NetLogo variables for turtles p ;; p, theta, and phi are the spherical coordinates theta phi ] to setup clear-all set-default-shape turtles "circle" end to setup-sphere setup ;; generate a sphere of radius SHAPE-SIZE crt 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 ] reset-ticks end ; a filled 3D cube to setup-cube-filled setup ;; generate a square with edge of length radius ;; placing a point randomly anywhere inside the square crt num-turtles [ cartesian ((- shape-size) + 2 * (random-float shape-size)) ((- shape-size) + 2 * (random-float shape-size)) ((- shape-size) + 2 * (random-float shape-size)) render-turtle ] reset-ticks end ; cube with turtles only on its surface to setup-cube-surface setup crt num-turtles [ let temp-alpha shape-size * (1 - 2 * (random 2)) ; +-shape-size ; random distribution bounded by +-shape-size let temp-beta shape-size - 2 * (random-float shape-size) let temp-gamma (random 2) ; front/back or left/right? ifelse temp-gamma = 0 ; generate front & back surfaces [ cartesian (temp-alpha) (temp-beta) (shape-size - (2 * (random-float shape-size))) ] [ cartesian (temp-beta) ; generating the side surfaces (temp-alpha) (shape-size - (2 * (random-float shape-size))) ] render-turtle ] reset-ticks end ; 3D cone to setup-cone setup crt num-turtles [ set theta (random-float 360) ; points have a random angle set p (random-float shape-size) cartesian (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 ] reset-ticks end ; vertical cylinder to setup-cylinder-v setup ;the code is almost the same as the setup-cone code ;except the xy-plane radius remains constant crt num-turtles [ let temp-alpha (random 3) - 1 ; which surface (left, right, or body?) set theta (random-float 360) ifelse temp-alpha = 0 [ cartesian (shape-size * (cos theta)) (shape-size * (sin theta)) ((- shape-size) + 2 * (random-float shape-size)) ] [ cartesian ((random-float shape-size) * (cos theta)) ((random-float shape-size) * (sin theta)) (temp-alpha * shape-size) ] render-turtle ] reset-ticks end ; horizontal cylinder to setup-cylinder-h setup ;generates a cylinder in a horizontal position with capped ends crt num-turtles [ let temp-alpha (random 3) - 1 ; which surface (left, right, or body?) set theta (random-float 360) ifelse temp-alpha = 0 ; generating the actual cylinder [ cartesian ((- shape-size) + 2 * (random-float shape-size)) (shape-size * (cos theta)) (shape-size * (sin theta)) ] [ cartesian (temp-alpha * shape-size) ((random-float shape-size) * (cos theta)) ((random-float shape-size) * (sin theta)) ] render-turtle ] reset-ticks end to setup-pyramid setup crt num-turtles [ let temp-alpha (- shape-size) + 2 * (random-float shape-size) ; z coordinate set theta (random 2) ; front/back or side? let temp-beta (shape-size - temp-alpha) / 2 let temp-gamma -1 + 2 * (random 2) ; left/right or front/back (-1 or 1) ifelse theta = 0 [ cartesian (temp-beta * temp-gamma) ; left & right surfaces ((- temp-beta) + 2 * (random-float temp-beta)) (temp-alpha) ] [ cartesian ((- temp-beta) + 2 * (random-float temp-beta)) ; front & back surfaces (temp-beta * temp-gamma) (temp-alpha) ] render-turtle ] reset-ticks end ;; convert from cartesian to spherical coordinates to cartesian [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 ; increment angle to simulate rotation render-turtle ] tick 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 ; Copyright 1998 Uri Wilensky. ; See Info tab for full copyright and license.

There are 10 versions of this model.

## Attached files

File | Type | Description | Last updated | |
---|---|---|---|---|

3D Solids.png | preview | Preview for '3D Solids' | almost 10 years ago, by Uri Wilensky | Download |

This model does not have any ancestors.

This model does not have any descendants.