globals [ p1x p1y p2x p2y c1x c1y c2x c2y ep1 ep2 cp1 cp2 steps inc dragged ] breed [ links link ] breed [ cplinks cplink ] breed [ waypoints waypoint ] breed [ endpoints endpoint ] waypoints-own [ index t u t2u t3 u2t u3] endpoints-own [ newx newy next-move] to startup setup end to setup ca ask patches [ set pcolor sky + 4.5 - .5 * ((pxcor + pycor ) mod 2) ] set steps 10 set p1x random-xcor set p1y min-pycor set p2x random-xcor set p2y max-pycor set c1x random-xcor set c2x random-xcor set c1y random-ycor set c2y random-ycor cct-endpoints 1 [ set ep1 self set label 0 setxy p1x p1y set color blue set shape "circle" __set-line-thickness .1 set size 2 set label-color black ] set inc 1 / steps let windex 1 let prevpoint ep1 cct-waypoints steps - 1 [ set index windex set t (inc * index) set shape "circle" set color 3 + windex * 10 set u (1 - t) set u2t u * u * t set u3 u * u * u set t2u t * t * u set t3 t * t * t set-waypoint-xy set windex windex + 1 create-link prevpoint set prevpoint self set hidden? hide-dots? ] cct-endpoints 1 [ set ep2 self set label 3 setxy p2x p2y set color blue set shape "circle" __set-line-thickness .1 set size 2 create-link prevpoint set label-color black ] cct-endpoints 1 [ set cp1 self setxy c1x c1y set label 1 set size 0 __create-cplink-to ep1 [ set shape "cplink" set color green ] set label-color black ] cct-endpoints 1 [ set cp2 self setxy c2x c2y set label 2 set size 0 __create-cplink-to ep2 [ set shape "cplink" set color green] set label-color black ] ask endpoints [ set newx xcor set newy ycor set next-move int timer ] end to create-link [ agent ] __create-link-with agent [ set color (blue - 2) __set-line-thickness .5 ] end to go no-display ifelse manual? [ ;; read mouse, allow dragging of endpoints with mouse if mouse-inside? [ ifelse mouse-down? [ let mx mouse-xcor let my mouse-ycor ifelse is-turtle? dragged and breed-of dragged = endpoints [ ask dragged [ setxy mx my ] ] [ if any? (turtles-from (turtles-on patch-at mx my) [ endpoints in-radius 2 ]) [ set dragged one-of (turtles-from (turtles-on patch-at mx my) [ endpoints in-radius 2 ]) ] ] ] [ set dragged nobody ] ] ] [ ;; every few seconds, pick a new set of endpoints, ;; and cause the control point turtles to ;; gradually slide into the new control point positions ask endpoints [ ;; is the current location too far from the desired location? if abs (xcor - newx) > .1 or abs ( ycor - newy) > .1 [ ;; interpolate between the current location ;; and the desired location set xcor .99 * xcor + .01 * newx set ycor .99 * ycor + .01 * newy ] ;; is it time to pick a new location? if timer > next-move [ set newx random-xcor set newy random-ycor ;; next move time is 2 to 5 seconds from now set next-move int (timer + 2 + random 4) ] ] ] let points [ "ep1" "ep2" "cp1" "cp2" ] let epx [ "p1x" "p2x" "c1x" "c2x" ] let epy [ "p1y" "p2y" "c1y" "c2y" ] (foreach points epx epy [ run (word "set " ?2 " xcor-of " ?1 "\n" "set " ?3 " ycor-of " ?1 "\n" ) ] ) ask waypoints [ set-waypoint-xy ] ;; update the display on a seperate schedule. ;; this makes using the speed slider to slow down the model ;; *also* appear to increase the resolution of the model ;; even though the only difference is that when the model ;; is moving quickly, fewer screen updates occur ;; this probably sort of simulates netlogo's own ;; screen-update scheduling every .02 [ ;; at this time, see if the value of the hide-dots? switch has changed if any? waypoints with [ hidden? != hide-dots? ] [ ask waypoints [ set hidden? hide-dots? ] ] if any? links with [ hidden? != hide-line? ] [ ask links [ set hidden? hide-line? ] ] display ] end to set-waypoint-xy set xcor p1x * u3 + 3 * c1x * u2t + 3 * c2x * t2u + p2x * t3 set ycor p1y * u3 + 3 * c1y * u2t + 3 * c2y * t2u + p2y * t3 ; set xcor p1x * u + p2x * t ; set ycor p1y * u + p2y * t end @#$#@#$#@ GRAPHICS-WINDOW 122 10 442 351 15 15 10.0 1 18 1 1 1 0 0 0 1 -15 15 -15 15 CC-WINDOW 5 365 451 460 Command Center 0 BUTTON 9 10 64 43 NIL setup NIL 1 T OBSERVER T NIL BUTTON 10 46 65 79 NIL go T 1 T OBSERVER NIL NIL SWITCH 9 82 107 115 hide-dots? hide-dots? 0 1 -1000 SWITCH 7 214 97 247 manual? manual? 1 1 -1000 SWITCH 8 119 102 152 hide-line? hide-line? 1 1 -1000 @#$#@#$#@ WHAT IS IT? ----------- A model that produces a Bézier Curve, a kind of Spline. HOW IT WORKS ------------ A spline is generated between endpoints ep1 and ep1, influenced by control points cp1 and cp2, and interpolated among "steps" waypoints. As U varies from 1 to 0, the spline is interpolated from ep1 to ep2. At each step n that is 1/steps of the way along the curve, a waypoint calculates the interpolated value of the bezier function for UsubN EXTENDING THE MODEL ------------------- Encapsulate the spline generator for general purpose use Generate other kinds of splines, such as cubic Generate splines with more than 2 control points Generate smoothly connected multiple splines Enable dragging of the link between the control- and end- points to modify the spline. Use splines to define smooth paths for agents in a simulation NETLOGO FEATURES ---------------- The netlogo LINKS features are used to generate the automatically updating lines between the waypoints no-display and display are used to speed up graphics rendering every is used to speed up graphics rendering timer is used to time the changes of location the technique of gradual interated linear interpolation is used to shift the location from the current location to the new location CREDITS AND REFERENCES ---------------------- An old Byte magazine from 20 or so years ago that I can't find anymore that talked about bezier curves (or was it Compute! magazine, or Scientific American?) http://mathworld.wolfram.com/BezierCurve.html http://mathworld.wolfram.com/Spline.html http://en.wikipedia.org/wiki/Spline_(mathematics) @#$#@#$#@ default true 0 Polygon -7500403 true true 150 5 40 250 150 205 260 250 link true 0 Line -7500403 true 150 0 150 300 link direction false 6 circle false 6 Circle -13840069 false true 15 15 268 cplink true 6 Line -13840069 true 150 30 150 0 Line -13840069 true 150 45 150 75 Line -13840069 true 150 90 150 120 Line -13840069 true 150 135 150 165 Line -13840069 true 150 180 150 210 Line -13840069 true 150 225 150 255 Line -13840069 true 150 270 150 300 dot false 0 Circle -7500403 true true 90 90 120 @#$#@#$#@ NetLogo 3.1.1 @#$#@#$#@ @#$#@#$#@ @#$#@#$#@ @#$#@#$#@