1

谁能给出一些提示(或完整的解决方案!)如何将模型库代码示例“鼠标拖动多个示例”转换为非包装世界。我的尝试破坏了选择矩形(矩形的边在边缘堆积 - 因为每个部分都是单独处理的)。同样,选定的海龟只是堆积在边缘。谢谢你。

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
4

1 回答 1

0

这是完全未经测试的。听起来您需要预先测试潜在位置,以确保是否不会越过世界边缘,如果确实如此,则相应地减少移动量。

尝试修改此部分:

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 ]

和:

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 ]

您将需要添加边(仅选择此选项)并对 y 坐标执行类似操作。

于 2015-08-10T11:05:49.500 回答