---bounce.nlogo

来自「NETLOGO」· NLOGO 代码 · 共 266 行

NLOGO
266
字号
breeds [nodes edges]edges-own [a b]nodes-own [v]globals [x-max y-max diam init-v-max reinit-v-max filename tick stoptick]to setup  ca  set diam 4  set init-v-max 3  set reinit-v-max 10  set x-max screen-edge-x - (diam / 2)  set y-max screen-edge-y - (diam / 2)  set filename "" ; change to collect images .. or use command center after setup  set tick 0  set stoptick 0  ; set to some number to stop, generally for image collections  set-default-shape nodes "circle"  set-default-shape edges "line"    ask patches [set pcolor white]  ask patches with [pxcor = 0 or pycor = 0][set pcolor gray]  cct-nodes balls [    set color yellow    set size diam    set v random-float init-v-max    setxy random-float (x-max) * (2 * (random 2) - 1)          random-float (y-max) * (2 * (random 2) - 1)  ]  if balls > 1 [ask nodes [    connect-edge random-one-of nodes with [self != myself]  ]]endto go  if filename = 0 [setup]  no-display  set tick tick + 1  ask nodes [    set v v - gravity    ifelse (abs (ycor + v)) <= y-max [      set ycor ycor + v  ;set color yellow    ] [      set ycor y-max * sign ycor      set v (- v)      ifelse v >= 0 and v <= gravity        [set v random-float reinit-v-max] ; set color red]        [set v v - (sign v) * friction] ; set color blue]    ]   ]  ask edges [adjust-edge]  display  check-movie  if stoptick = tick [stop]endto check-movie  if length filename > 0 [    export-graphics filename + substring "0000" (int log tick 10) 3 + tick + ".png"  ]endto connect-edge [other-node] ; node proc: attach an edge between self and other  hatch 1 [    set breed edges    set a myself    set b other-node    set color red    adjust-edge  ]endto adjust-edge ; edge proc: reattach to a & b nodes  setxy xcor-of a ycor-of a  set size distance-nowrap b  set heading towards-nowrap b  jump size / 2end;;;; Utilitiesto-report sign [num]  ifelse num < 0 [report -1][report 1]end@#$#@#$#@GRAPHICS-WINDOW2041056539219199.0110111CC-WINDOW4417475536Command CenterBUTTON76962102NILsetupNIL1TOBSERVERTBUTTON6870123103NILgoT1TOBSERVERNILBUTTON12970184103stepgoNIL1TOBSERVERTSLIDER63418467ballsballs120511NILSLIDER7193183226gravitygravity051.00.251NILSLIDER8235184268frictionfriction010.250.251NILTEXTBOX111018331Setup Parameters and Controls.TEXTBOX10154165189Runtime Parameters.\nCan change while running!@#$#@#$#@WHAT IS IT?-----------A very simple first graph (edges and nodes) model.HOW IT WORKS------------Nodes are randomly created and linked, then are subject to gravity each step of the model.HOW TO USE IT-------------Change the number of balls slider if you'd prefer a different value.  Click "setup" to initialize the nodes and edges.  Click "Go" to run the model, or click "Step" to manually run the model one step at a time.THINGS TO NOTICE----------------Not much in the model, this is just a programming exercise to try the NetLogo edges and nodes facility.  You can read the code fairly easily to see how the graph is managed.THINGS TO TRY-------------Move the two sliders "friction" and "gravity" while the model is running to see how they change the bouncing balls.  Change the code to play with the edges and nodes.EXTENDING THE MODEL-------------------Have the nodes be attracted to the other nodes they are attracted to so that the nodes move in two directions.NETLOGO FEATURES----------------Edges and Nodes!  The cool thing is the model NetLogo uses for edges: they are simply agents located at the center of the edge, and long enough to connect the nodes the edge connects.  Using floating coordinates lets the graph continuously morph.RELATED MODELS--------------Check out the "layout" model which is a springs and forces model making the graph untangle itself.CREDITS AND REFERENCES----------------------Seth Tisue sent me a very similar model to start out with .. Thanks!@#$#@#$#@defaulttrue0Polygon -7566196 true true 150 5 40 250 150 205 260 250circlefalse0Circle -7566196 true true 35 35 230linetrue0Line -7566196 true 150 0 150 301@#$#@#$#@NetLogo 2.0.0@#$#@#$#@@#$#@#$#@@#$#@#$#@

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?