3D Shapes
WHAT IS IT?
This project shows how to create and rotate simple three-dimensional objects in StarLogo. Objects are created by placing turtles randomly on the surface of the object.
HOW TO USE IT
The SPHERE, CYLINDER, and CUBE buttons create those objects. The TURN-X, TURN-Y, and TURN-Z buttons make the objects rotate around the indicated axes.
THINGS TO NOTICE
Depth (along the Z-axis) is indicated by shade of red. Some objects (such as the cube) are easy to "see" from certain perspectives, but seem like a mess of turtles from other perspectives.
EXPLORATIONS
* Create other three-dimensional objects.* Experiment with other ways of indicating depth along the Z-axis (other than shade of color). To display the turtles on the two-dimensional screen, the turtles are projected directly onto the XY-plane (as if viewed from inifinity). Change the program so that the user can control the position of the "camera" (so that points with the same X and Y values do not necessarily project to the same place on the XY plane).* It is not always obvious how to distribute turtles evenly on the surface of a three-dimensional object. Try other ways of creating spheres and cylinders.
STARLOGO FEATURES
The TURN procedures use actual values rather than the trig. functions, since the trig lookup tables in StarLogo do not have as many digits of accuracy.
Turtle procedures
turtles-own [
x1 ; x position
y1 ; y position
z1 ; z position
minz ; minimum z value
maxz ; maximum z value
vector-length ; length of vector
]
;; sphere procedure
;; called by the observer sphere procedure
;; Gets normalized values for x, y, and z, sets minz and maxZ.
;; Then projects the turtle position on the xy-plane and scales
;; the turtle color to provide depth-cueing.
;;
to sphere
set-sphere-point srandom 50 srandom 50 srandom 50 40
setminz -40
setmaxz 40
project-on-xy
end
;; cube procedure
;; called by the observer cube procedure
;; Gets values for x, y, and z on the cube, then sets minz and maxZ.
;; Projects the turtle position on the xy-plane and scales
;; the turtle color to provide depth-cueing.
;;
to cube
set-cube-point 30 random 6
setminz -30 * sqrt 2
setmaxz 30 * sqrt 2
project-on-xy
end
;; cylinder procedure
;; called by the observer cylinder procedure
;; Gets values for x, y, and z on the cylinder, then sets minz and maxZ.
;; Projects the turtle position on the xy-plane and scales
;; the turtle color to provide depth-cueing.
;;
to cylinder
set-cylinder-point random 360 random 360 30
setminz -30
setmaxz 30
project-on-xy
end
;; project-on-xy procedure
;; called by the turtle sphere, cube, and cylinder turtle procedures
;; Positions the turtle at x1,y1 then scales the color according to depth (z)
;;
to project-on-xy
setxy x1 y1
scale-color red z1 minz maxz
end
;; set-sphere-point procedure
;; called by turtle sphere procedure
;; takes four arguments: a random x, y, and z coord, and a fixed radius.
;; Computes the vector-length using pythagorean theorem
;; normalizes the vector to be of length radius.
;; then sets the values of the turtle parameters x1, x2, and x3
;;
to set-sphere-point :x2 :y2 :z2 :r
setvector-length sqrt ((:x2 * :x2) + (:y2 * :y2) + (:z2 * :z2))
setx1 :r * :x2 / vector-length
sety1 :r * :y2 / vector-length
setz1 :r * :z2 / vector-length
end
;; set-cylinder-point procedure
;; called by turtle cylinder procedure
;; takes three arguments: the angle theta and the depth phi
;; Computes the x1 and y1 using trigonometric functions
;; Computes z1 using the sin function to constrain the value
;; between 0 - 1.
;;
to set-cylinder-point :theta :phi :r
setx1 :r * (cos :theta)
sety1 :r * (sin :theta)
setz1 :r * (sin :phi)
end
;; set-cube-point procedure
;; called by turtle cube procedure
;; takes two arguments: a distance and a face number
;; Sets an x, y, and z value for the turtle dependent on which face was selected
;;
to set-cube-point :r :face
if :face = 0 [setxyz srandom :r srandom :r :r]
if :face = 1 [setxyz srandom :r srandom :r 0 - :r]
if :face = 2 [setxyz srandom :r :r srandom :r]
if :face = 3 [setxyz srandom :r 0 - :r srandom :r]
if :face = 4 [setxyz :r srandom :r srandom :r]
if :face = 5 [setxyz 0 - :r srandom :r srandom :r]
end
;; setxyz procedure
;; called by set-cube-point procedure
;; takes three arguments: the x, y, and z values to be loaded into
;; the turtle parameters.
;;
to setxyz :a :b :c
setx1 :a
sety1 :b
setz1 :c
end
;; srandom procedure
;; called by set-cube-point procedure
;; returns a random value
;;
to srandom :foo
output ((random (2 * :foo)) - :foo)
end
;; rotate-around-y procedure
;; called by turn-y procdure
;; Sets the new x and z position as if the turtle moved 1 degree in y,
;; uses fixed values rather than computing cos 1 and sin 1 repeatedly.
;;
to rotate-around-y
setx1 (x1 * 0.9998477) - (z1 * 0.0175241)
setz1 (x1 * 0.0175241) + (z1 * 0.9998477)
end
;; rotate-around-x procedure
;; called by turn-x procdure
;; Sets the new y and z position as if the turtle moved 1 degree in x,
;; uses fixed values rather than computing cos 1 and sin 1 repeatedly.
;;
to rotate-around-x
sety1 (y1 * 0.9998477) - (z1 * 0.0175241)
setz1 (y1 * 0.0175241) + (z1 * 0.9998477)
end
;; rotate-around-z procedure
;; called by turn-z procdure
;; Sets the new x and y position as if the turtle moved 1 degree in z,
;; uses fixed values rather than computing cos 1 and sin 1 repeatedly.
;;
to rotate-around-z
setx1 (x1 * 0.9998477) - (y1 * 0.0175241)
sety1 (x1 * 0.0175241) + (y1 * 0.9998477)
end
;; turn-y procedure
;; called by the button turn-y
;; rotates all the turtles around the y axis
;;
to turn-y
rotate-around-y project-on-xy
end
;; turn-x procedure
;; called by the button turn-x
;; rotates all the turtles around the x axis
;;
to turn-x
rotate-around-x project-on-xy
end
;; turn-z procedure
;; called by the button turn-z
;; rotates all the turtles around the z axis
;;
to turn-z
rotate-around-z project-on-xy
end
Observer procedures
;; sphere procedure
;; Called by sphere button
;; Clears all, creates turtles then asks each turtle to run the turtle
;; sphere procedure.
;;
to sphere
ca
crt 1000
ask-turtles [sphere]
end
;; cube procedure
;; Called by cube button
;; Clears all, creates turtles then asks each turtle to run the turtle
;; cube procedure.
;;
to cube
ca
crt 1000
ask-turtles [cube]
end
;; cylinder procedure
;; Called by cylinder button
;; Clears all, creates turtles then asks each turtle to run the turtle
;; cylinder procedure.
;;
to cylinder
ca
crt 1000
ask-turtles [cylinder]
end
;; startup procedure
;; Called upon program startup
;; Clears all
;;
to startup
ca
end
