decorated-links


Information

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


WHAT IS IT?


-------
A model to demonstrate a technique of applying turtles to decorate links, such as links that are under a specific length (or invisible, due to node overlap)

Procedures

NetLogo Version: NetLogo 4.0.4

globals
[ min-edge-length ;; if the edge length is less than this, the linked nodes are "decorated"
]

;; decora (singular decorum, like "data" and "datum") are turtles that decorate nodes when the link is too short
;; place first so that these turtles appear UNDER other turtles
;; make sure to define a shape "decorum" that has the appearance desired.
;; note that shapes scale proportionally, so "dumbbell" or other linear shapes may not perform as desired
breed [ decora decorum ]
decora-own [ my-edge ]

;; the other breeds for the netwrk
breed [ nodes node ]
undirected-link-breed [ edges edge ]

;; the breed for the mouse pointer
;; only needed for the draggable network display
;; defined last so pointer is on top of all other shapes
breed [ pointers pointer ]
pointers-own [ click? dragged ]


to setup
   ca
   set min-edge-length 2 
   ;; defines default shape for decora
   setup-decora
   ;; creates the network, including the decora
   setup-network 100
   ;; arranges the network for initial display  
   repeat 100 [ update-network-display ]
   update-decora-display
end

to go
   every .015
   [ ;; copy min-length from slider,
     ;; put in min-edge-length
     set min-edge-length min-length
     update-network-display
     do-mouse
     ;; always do this after all other display-changers are done
     ;; e.g. after mouse drag may have changed link length.
     update-decora-display
     tick
   ]
end   

to create-decorated-edge [ that-node ]
   let this-node self
   create-edge-with that-node
   [ let this-edge self
     ;; only turtles can use hatch, so we'll
     ;; get the calling turtle to hatch
     ;; the decorum we need
     ask this-node
     [ hatch-decora 1
       [ ;; decorum keeps a reference to the edge it decorates
         set my-edge this-edge
         ;; decorum is initially hidden
         hide-turtle 
         ;; this updates the size and position of the decorum
         update-decorum-display
       ]
     ]
   ]
end
 
to setup-network [ node-pop ]
   set-default-shape nodes "node"
   create-nodes node-pop
   [ 
     create-decorated-edge one-of other nodes 
     setxy random-xcor random-ycor
   ]
   update-network-display
 end

to update-network-display
   layout-spring nodes edges .5 3 .3
end

to setup-decora
   ;; defaine default shape for a decorum
   set-default-shape decora "decorum"
end

to update-decora-display
   ask decora [ update-decorum-display ]
end

to update-decorum-display
   ;; update the position, heading, and visibility of a decorum
   setxy [ edge-xcor ] of my-edge
         [ edge-ycor ] of my-edge
   ;; the sizing formula used depends on the look desired.
   ;; this formula makes the decorum fit around both end-points, with 
   ;; margin equal half to the larger endpoint size, and includes the length of the edge
   let edge-length [ link-length ] of my-edge
   
   set size [ link-length +  2 * max [ size ] of both-ends ] of my-edge
   ;; if the size of the link <= 0, then there is no heading, otherwise, get a heading
   set heading [ ifelse-value (link-length = 0) [ 0 ] [ link-heading ] ] of my-edge
     
   ;; hide or un-hide the decorum
   ifelse hidden? 
   [ if edge-length <= min-edge-length
     [ show-turtle ]
   ][ if edge-length > min-edge-length
     [ hide-turtle
     ]
   ]
end

to-report edge-xcor 
   report ( .5 * ( [ xcor ] of end1 + [ xcor ] of end2 ) )
end

to-report edge-ycor 
   report ( .5 * ( [ ycor ] of end1 + [ ycor ] of end2 ) )
end


;;
;; MOUSE STUFF
;;

to do-mouse
   if not any? pointers
   [ create-pointers 1
     [ set shape "pointer"
     set color white
     set size 5
     set heading 0
       set dragged nobody
       set click? false
     ]
   ]
   ask pointers
   [ if mouse-inside?
     [ let mx mouse-xcor
       let my mouse-ycor
       setxy mx my
       ifelse mouse-down?
       [ if not is-node? dragged 
         [ if any? nodes
           [ set dragged min-one-of nodes with [ quick-distance myself < 4 ] [ quick-distance myself ]
           ]
         ]
         if is-node? dragged
         [ ask dragged [ setxy mx my ]
         ]
       ]
       [ set dragged nobody
       ]
     ]
   ]
end

to-report quick-distance [ agent ]
   let xx [ xcor ] of agent - xcor
   let yy [ ycor ] of agent - ycor
   report (xx * xx + yy * yy)
end

          

                    


Download Link

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