Perimeters and Areas
No preview image
Model was written in NetLogo 4.1pre1
•
Viewed 256 times
•
Downloaded 31 times
•
Run 0 times
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
VERSION
$Id: Perimeters and Areas.nlogo 39722 2008-05-01 18:38:33Z everreau $
Comments and Questions
Please start the discussion about this model!
(You'll first need to log in.)
Click to Run Model
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Variable and Breed declarations ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; globals [ ;; lists used to create the various turtles shape-names ;; list that holds the names of the non-sick shapes a student's turtle can have colors ;; list that holds the colors used for students' turtles color-names ;; list that holds the names of the colors used for students' turtles used-shape-colors ;; list that holds the shape-color pairs that are already being used ;; misc max-possible-codes ;; total number of possible unique shape/color combinations the-points ] breed [ edges edge ] breed [ students student ] ;; created and controlled by the clients students-own [ base-shape ;; original shape of a turtle user-id ;; unique id, input by the client when they log in, to identify each student turtle step-size edge-1 edge-2 angle-between-edges ] edges-own [a b] ;;;;;;;;;;;;;;;;;;;;; ;; Setup Functions ;; ;;;;;;;;;;;;;;;;;;;;; to startup setup hubnet-set-client-interface "COMPUTER" [] hubnet-reset end ;; Initializes the display, and creates a list that contains the names of the shapes ;; used by turtles in this activity. Also initializes the data lists. to setup cp ct clear-output setup-vars set-default-shape edges "line" end ;; initialize global variables to setup-vars set shape-names [ "airplane" "android" "butterfly" "cactus" "cat" "cow skull" "ghost" "heart" "leaf" "monster" ] set colors (list brown green yellow (violet + 1) (sky - 1)) set color-names ["brown" "green" "yellow" "purple" "blue"] set max-possible-codes (length colors * length shape-names) set used-shape-colors [] set the-points [] end ;; give the user some information about what the setup button does so they can ;; know whether they want to proceed before actually doing the setup to setup-prompt if user-yes-or-no? (word "The SETUP button should only be used when starting " "over with a new group (such as a new set of students) since " "all data is lost. Use the RE-RUN button for continuing with " "an existing group." "\\n\\nDo you really want to setup the model?") [ user-message (word "Before closing this dialog, please do the following:" "\\n -Have everyone that is currently logged in, log off and " "then kick all remaining clients with the HubNet Console.") setup ] end ;;;;;;;;;;;;;;;;;;;;;;; ;; Runtime Functions ;; ;;;;;;;;;;;;;;;;;;;;;;; to go every 0.1 [ ;; get commands and data from the clients listen-clients display-angles update-clients-info ] end to edgify ask edges [ die ] let smarties [self] of students with [user-id != 0] let dummies [self] of students with [user-id = 0] ifelse fixed-together? [ set the-points sentence (shuffle smarties) (shuffle dummies) ] [ set the-points shuffle (sentence smarties dummies) ] if length the-points >= 2 [ foreach n-values length the-points [?] [ ask item ? the-points [ connect-to item ((? + 1) mod length the-points) the-points ] ] ] display-angles update-clients-info end to make-fixed-point create-students 1 [ set-unique-shape-and-color set color white setxy random-pxcor random-pycor set heading 0 ] end to move-fixed-point let fixed-points students with [user-id = 0 ] if mouse-down? [ let closest min-one-of fixed-points [distancexy-nowrap mouse-xcor mouse-ycor ] ask closest [ set xcor mouse-xcor set ycor mouse-ycor ] ask edges [reposition] display-angles update-clients-info update-plots ] end to display-angles ask patches [set plabel ""] ask edges [ ask a [ let e1 myself let e2 one-of edges with [ b = myself ] ; here myself is the asking a let n1 [b] of e1 let n2 [a] of e2 let angle subtract-headings towards n1 towards n2 if label-dood != nobody [ ask label-dood [ set plabel-color [color] of myself set plabel precision abs angle 0 ask myself [set angle-between-edges abs angle] ] ] set edge-1 [size] of e1 set edge-2 [size] of e2 ] ] end to-report label-dood report one-of neighbors with [ distance myself = distance-nowrap myself and not any? students-here ] end ;; report the amount we can move in the current direction ;; if we don't want to wrap around the screen to-report move-amount [direction] ;; turtle procedure let amount step-size let me self while [ abs (xcor + (amount * dx * direction)) > max-pxcor or abs (ycor + (amount * dy * direction)) > max-pycor or (amount > 0 and any? [ students-here with [self != me] ] of patch-ahead (amount * direction)) ] [ ifelse amount < 1 [ set amount 0 ] [ set amount amount - 1 ] ] report amount end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Code for interacting with the clients ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; determines which client sent a command, and what the command was to listen-clients while [ hubnet-message-waiting? ] [ hubnet-fetch-message ifelse hubnet-enter-message? [ create-new-student ] [ ifelse hubnet-exit-message? [ remove-student ] [ execute-command hubnet-message-tag ]] ] end ;; NetLogo knows what each student turtle is supposed to be ;; doing based on the tag sent by the node: to execute-command [command] ifelse command = "Change Appearance" [ execute-change-turtle ] [ ifelse command = "step-size" [ ask students with [user-id = hubnet-message-source] [ set step-size read-from-string hubnet-message ;; we only want non integers when it's less than 1 if step-size > 1 [ let closest-int round step-size ifelse closest-int = ceiling step-size [ set step-size floor step-size ] [ set step-size ceiling step-size ] ] ] ] [ ifelse command = "Lt" [ execute-turn -45 ] [ ifelse command = "Rt" [ execute-turn 45 ] [ ifelse command = "Up" [ execute-move 0 ] [ ifelse command = "Down" [ execute-move 180 ] [ ifelse command = "Right" [ execute-move 90 stop ] [ ifelse command = "Left" [ execute-move 270 ] [ ifelse command = "Fd" [ execute-go 1 ] [ ifelse command = "Bk" [ execute-go -1 ] [ ]]]]]] ;; executed no matter which movement command it was if any? edges [ update-plots ] ] if hubnet-message-tag = "View" [ ask students with [ user-id = hubnet-message-source ] [ move-to-click ] ] ]]] end to move-to-click ; student procedure let patch-clicked-on patch (round item 0 hubnet-message) (round item 1 hubnet-message) setxy [pxcor] of patch-clicked-on [pycor] of patch-clicked-on ask edges [ reposition ] end ;; Create a turtle, set its shape, color, and position ;; and tell the node what its turtle looks like and where it is to create-new-student create-students 1 [ setup-student-vars send-init-to-client ] end ;; sets the turtle variables to appropriate initial values to setup-student-vars ;; turtle procedure set user-id hubnet-message-source set step-size 1 set-unique-shape-and-color while [ any? other students-here ] [ setxy random-pxcor random-pycor ] set heading 0 end ;; sends the appropriate monitor information back to the client to send-init-to-client hubnet-send user-id "You are a:" (word (color-string color) " " base-shape) hubnet-send user-id "Located at:" (word "(" pxcor "," pycor ")") hubnet-send user-id "Heading:" heading-text hubnet-send user-id "Perimeter:" perimeter hubnet-send user-id "Area:" area end ;; pick a base-shape and color for the turtle to set-unique-shape-and-color let code random max-possible-codes while [member? code used-shape-colors and count students < max-possible-codes] [ set code random max-possible-codes ] set used-shape-colors (lput code used-shape-colors) set base-shape item (code mod length shape-names) shape-names set shape base-shape set color item (code / length shape-names) colors set label-color color end ;; report the string version of the turtle's color to-report color-string [color-value] report item (position color-value colors) color-names end to-report heading-text ifelse heading = 0 [ report "^" ] [ ifelse heading = 45 [ report "/^" ] [ ifelse heading = 90 [ report ">" ] [ ifelse heading = 135 [ report "\\\\v" ] [ ifelse heading = 180 [ report "v" ] [ ifelse heading = 225 [ report "v/" ] [ ifelse heading = 270 [ report "<" ] [ if heading = 315 [ report "^\\\\" ]]]]]]]] end ;; Kill the turtle, set its shape, color, and position ;; and tell the node what its turtle looks like and where it is to remove-student ask students with [user-id = hubnet-message-source] [ set used-shape-colors remove my-code used-shape-colors die ] end ;; translates a student turtle's shape and color into a code to-report my-code report (position base-shape shape-names) + (length shape-names) * (position color colors) end ;XXXXXXXXXX these 3 procedures should be combined if possible ;; Cause the students to move forward step-size in new-heading's heading to execute-move [new-heading] ask students with [user-id = hubnet-message-source] [ set heading new-heading jump move-amount 1 ; 1 means forward direction set heading 0 ask edges [ reposition ] ] end ;XXXXXXXX terrible name to execute-go [direction] ask students with [user-id = hubnet-message-source] [ jump direction * move-amount direction ask edges [ reposition ] ] end to execute-turn [amount] ask students with [user-id = hubnet-message-source] [ rt amount ] end to update-clients-info let user-id-dooder 0 foreach [ self ] of students with [user-id != 0] [ set user-id-dooder [user-id] of ? hubnet-send user-id-dooder "Perimeter:" perimeter hubnet-send user-id-dooder "Area:" area hubnet-send user-id-dooder "Heading:" [ heading-text ] of ? hubnet-send user-id-dooder "step-size" [step-size] of ? hubnet-send user-id-dooder "Located at:" (word "(" precision [xcor] of ? 1 "," precision [ycor] of ? 1 ")") ] end to-report perimeter report precision sum [size] of edges 1 end to-report area let vertices the-points let result 0 foreach n-values length vertices [?] [ let vertex item ? vertices let other-vertex item ((? + 1) mod length vertices) vertices let addend (([pxcor] of vertex * [pycor] of other-vertex) - ([pycor] of vertex * [pxcor] of other-vertex)) set result result + addend ] report precision abs (result / 2) 1 end to update-plots set-current-plot "perimeter" plot perimeter set-current-plot "area" plot area end to execute-change-turtle ask students with [user-id = hubnet-message-source] [ set used-shape-colors remove my-code used-shape-colors show-turtle set-unique-shape-and-color hubnet-send user-id "You are a:" (word (color-string color) " " base-shape) ] end to connect-to [other-node] ;; node procedure hatch 1 [ set breed edges set a myself set b other-node set color white set label-color white reposition ] end to reposition ;; edge procedure setxy ([xcor] of a) ([ycor] of a) set heading towards-nowrap b set size distance-nowrap b jump (distance-nowrap b) / 2 set label precision size 1 end
There are 2 versions of this model.
Attached files
No files
This model does not have any ancestors.
This model does not have any descendants.