Netlogo modifying library Mouse Drag Multiple Example to a non wrapping world

101 Views Asked by At

Could anyone give some hints (or a full solution!) to how to convert the model library code example 'Mouse Drag Multiple Example' to a non wrapping world. My attempts destroy the selection rectangle (the sides of the rectangle pile up at the edge - because each part is treated separately). Similarly the selected turtles just pile up at the edge. Thank you.

Here's the main procedure from the model - it gives a runtime error on the second setxy when the side of the selection rectangle gets to the edge of the non-wrapping world:

to handle-drag
;; remember where the mouse pointer was located when
;; the user pressed the mouse button
let old-x mouse-xcor
let old-y mouse-ycor
if selected? old-x old-y [              ;; selected? is a reporter defined below
while [mouse-down?] [
  let new-x mouse-xcor
  let new-y mouse-ycor
  ;; we need to move both the selected turtles and the sides
  ;; of the selection rectangle by the same amount that the
  ;; mouse has moved.  we do this by subtracting the current
  ;; mouse coordinates from the previous mouse coordinates
  ;; and adding the results to the coordinates of the turtles
  ;; and sides.
  ask selected
    [ setxy xcor + new-x - old-x
            ycor + new-y - old-y ]
  ask sides
    [ setxy xcor + new-x - old-x
            ycor + new-y - old-y ]
  set old-x new-x
  set old-y new-y
  ;; update the view, otherwise the user can't see
  ;; what's going on
  display
]
]
end
1

There are 1 best solutions below

1
On

This is completely untested. It sounds like what you need is to pretest the potential position to make sure if doesn't go past the edge of the world and, if it does, reduce the amount of movement accordingly.

Try amending this section:

ask selected
[ setxy xcor + new-x - old-x
        ycor + new-y - old-y ]
ask sides
[ setxy xcor + new-x - old-x
        ycor + new-y - old-y ]

with:

let move-x new-x - old-x
let proposed-max-x max [x-cor] of selected + move-x
let proposed-min-x min [x-cor] of selected + move-x
if proposed-max-x > max-pxcor + 0.5 [let move-x max-pxcor + 0.5 - max [x-cor] of selected]
if proposed-min-x < min-pxcor - 0.5 [let move-x min-pxcor - 0.5 + min [x-cor] of selected]
ask selected
    [ setxy xcor + move-x ]

You will need to add in the sides (this only does selected) and do similar for y coordinates.