Attract Repel
Submitted by librarian on Fri, 2006-01-20 23:41. :: Mathematics | Physics
WHAT IS IT?
This project demonstrates how simple procedures can cause turtles to repel one another or form a ring.
HOW TO USE IT
Press the setup button to generate num turtles. Then play with the adjust and repel buttons. Change the values of desired-distance, repel-strength, and convergence, then see the effects.
THINGS TO NOTICE
Using repel alone gives us four areas of turtles, why?
EXPLORATIONS
Experiment with using adjust and repel buttons. Change parameters and watch. What would happen if repel used next and prev turtles to repel from rather than a random turtle?
Turtle procedures
turtles-own [
next-turtle ; the next turtle in line (by who number)
prev-turtle ; the previous turtle in line (by who number)
heading-1 ; heading to take to face next-turtle
dist-1 ; a distance away from next-turtle
heading-2 ; heading to take to face prev-turtle
dist-2 ; a distance away from prev-turtle
repel-turtle ; which turtle to repel from
]
;; setup procedure
;; called by the observer setup procedure
;; Sets the turtle's color to red, sets a random heading and
;; moves forward 25 steps. Sets turtle parameters prev and next
to setup
setc red
seth random 360
fd random 25
setnext-turtle who + 1
if next-turtle >= total-turtles [setnext-turtle 0]
setprev-turtle who - 1
if prev-turtle < 0 [setprev-turtle total-turtles - 1]
end
;; adjust procedure
;; called by adjust button
;; Computes heading and distance to prev-turtle and next-turtle
;; Moves between prev and next turtle using the heading and distance information
;;
to adjust
setheading-1 towards xcor-of next-turtle ycor-of next-turtle
setdist-1
(convergence / 100) *
((distance xcor-of next-turtle ycor-of next-turtle)
- desired-distance)
setheading-2 towards xcor-of prev-turtle ycor-of prev-turtle
setdist-2
(convergence / 100) *
((distance xcor-of prev-turtle ycor-of prev-turtle)
- desired-distance)
seth heading-1
fd dist-1
seth heading-2
fd dist-2
end
;; repel procedure
;; called by repel button
;; Selects a random turtle to repel away from, sets heading to face this
;; turtle then backs up some amount derived using the user-defined var
;; repel-strength
;;
to repel
setrepel-turtle random total-turtles
seth towards xcor-of repel-turtle ycor-of repel-turtle
bk repel-strength / 100
end
Observer procedures
4763 reads
