bezier-spline_2009


Information

Created with NetLogo version NetLogo 4.0.4
Running with NetLogoLite.jar version 404.



Procedures

NetLogo Version: NetLogo 4.0.4

globals
[ p1x p1y
  p2x p2y
  
  c1x c1y
  c2x c2y
  
  ep1 ep2
  cp1 cp2 

   steps
   inc
   
   dragged   
]

undirected-link-breed [ pathlinks pathlink ]
directed-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 target ]

to startup setup end

to setup
   ca
   
   ask patches [ checkers ]
   
   set steps 10
   
   set p1x random-xcor
   set p1y min-pycor + 3
   
   set p2x random-xcor
   set p2y max-pycor - 3
   
   set c1x random-xcor
   set c2x random-xcor
   
   set c1y random-ycor
   set c2y random-ycor

   create-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
   create-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-pathlink prevpoint
     
     set prevpoint self
     set hidden? hide-dots?
   ]
   
   create-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-pathlink prevpoint
     set label-color black
   ]
   
   create-endpoints 1
   [ set cp1 self
     setxy c1x c1y
     set label 1
     set size 2
     set shape "circle"
     create-cplink-to ep1
     [ set shape "cplink" 
       set color green 
     ]
     set label-color black
   ]
   create-endpoints 1
   [ set cp2 self
     setxy c2x c2y
     set label 2
     set shape "circle"
     set size 2
     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
   ]
   display
end

to create-pathlink [ agent ]
     create-pathlink-with agent [ set color (blue - 2) set thickness .5 ]
end



to go
   ifelse animate?
   [ ;; animate? = true, so move automatically
     ;; 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 
       [ if is-patch? target [ ask target [ checkers ] ]
         set newx random-xcor
         set newy random-ycor
         set target patch (round newx) (round newy)
         ask target [ set pcolor red + 30 * [label] of myself]
         ;; next move time is 2 to 5 seconds from now
         set next-move int (timer + 2 + random 4)
        ]
     ]
   ]
   [ ;; animate? = false, so read mouse for user input
   
     ;; read mouse, allow dragging of endpoints with mouse 
     if mouse-inside?
     [ ;; mouse is inside view
       ifelse mouse-down?
       [ ;; mouse button is down
         ;; record mouse pointer position
         let mx mouse-xcor
         let my mouse-ycor
         ;; if the variable dragged is a turtle, and an endpoint
         ;; then an endpoint is being dragged
         ;; move the endpoint to the mouse position
         ifelse is-turtle? dragged
         [ ask dragged
           [ if breed = endpoints
             [ ;;;  ;; if endpoint is one of the line endpoints
               ;;;  ;; then moving the endpoint moves the attached control point, too!
               ;;;  if label = 3 or label = 0
               ;;;  [ let .my-cplink one-of __my-in-cplinks
               ;;;    let new-end-x limitx (mx - value-from .my-cplink [ dx * size ])
               ;;;    let new-end-y limity (my - value-from .my-cplink [ dy * size ])
               ;;;    ask .my-cplink
               ;;;    [ ask __other-end
               ;;;      [ setxy new-end-x new-end-y
               ;;;      ]
               ;;;    ]
               ;;;  ]
               
               ;; finally, move the endpoint
               setxy mx my 
             ]
             ;if breed = cplinks
             ;[ let endx dx * size * .5
             ;  let endy dy * size * .5 
             ;  ask end1
             ;  [ setxy limitx [ mx - endx ] of myself 
             ;          limity [ my - endy ] of myself
             ;  ]
             ;  ask end2
             ;  [ setxy limitx [ mx + endx ] of myself
             ;          limity [ my + endy ] of myself
             ;  ]
             ;]
           ]
         ]
         [ ;; dragged is not an endpoint or link, so maybe
           ;; there is an endpoint nearby?
           let draggable [ endpoints in-radius 1 ] of patch mx my
           ;if not any? draggable
           ;[ ;; no, no endpoint nearby, maybe a control point link?
           ;  set draggable [ cplinks in-radius 1 ] of patch-at mx my
           ;]
           if any? draggable
           [ ;; there is an endpoint or link near the mouse click.
             ;; make that the dragged endpoint
             set dragged min-one-of draggable [ who ]
           ]
         ]
       ]
       [ ;; mouse is up, so end drag
         set dragged nobody 
       ]
     ]
        
   ]

   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? pathlinks with [ hidden? != hide-line? ]
     [ ask pathlinks [ set hidden? hide-line? ]
     ]
   ]
   tick
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       

to checkers
   set pcolor sky + 4.5 - .5 * ((pxcor + pycor ) mod 2)
end   

to-report limitx [ xx ]
   report max (list min-pxcor min (list max-pxcor xx))
end
to-report limity [ yy ]
   report max (list min-pycor min (list max-pycor yy))
end

                    


Download Link

View or download the complete model file (to download: right-click, save-link-as):
-- Download bezier-spline_2009 --