如何为给定矩形内的色块着色

时间:2013-10-15 13:33:39

标签: netlogo

我创建了一个包含几个矩形的网格。这些矩形由几个橙色斑块表示,每个矩形由白色走廊划界。

如何在给定的橙色矩形内对色块进行着色?

提前致谢。

这是代码的开头:

to create-yellow-patches

ask one-of patches with [pcolor = orange] [ 
 set pcolor yellow
 foreach list pxcor to max-pxcor [ ;; I don't know how to define a list from pxcor to max-pxcor
  let x ?
  foreach list min-pycor to max-pycor [ ;; I don't know how to define a list from min-pycor to max-pycor
   let y ?
    ifelse [pcolor] of patches with [pxcor = x and pycor = y ] = orange 
    [ set pcolor yellow ] 
    [ break ] ] ] ;; I don't know what is the equivalent of break in netlogo

  foreach pxcor - 1 to min-pxcor [ ;; I don't know how to define a list from pxcor - 1 to min-pxcor
   let x ?
   foreach min-pycor to max-pycor [ ;; I don't know how to define a list from min-pycor to max-pycor
    let y ?
    ifelse [pcolor] of patches with [pxcor = x and pycor = y ] = orange 
    [ set pcolor yellow ]     
    [ break ] ] ] ;; I don't know what is the equivalent of break in netlogo
end

1 个答案:

答案 0 :(得分:1)

这是一些用于制作橙色矩形补丁的示例代码:

ask patches with [pxcor >= -3 and pxcor <= 6 and pycor >= -5 and pycor <= 7] [
  set pcolor orange
]

我猜这并不能完全回答你的问题。你已经有了一些橙色补丁和一些白色补丁,那些应该以某种方式控制你想要绘制的矩形的位置......就像那样?

<强>更新

好的,现在问题更清楚了。

有很多不同的方法可以解决这个问题。这是我能想到的最优雅的一个:

to setup
  clear-all
  ask patches [
    ifelse pxcor mod 5 = 0 or pycor mod 5 = 0
      [ set pcolor white ]
      [ set pcolor orange ]
  ]
end

to create-yellow-patches
  let seed one-of patches with [pcolor = orange] 
  ask seed [ turn-yellow ]
end

to turn-yellow  ;; patch procedure
  set pcolor yellow
  ask neighbors4 with [pcolor = orange] [
    turn-yellow
  ]
end

这是一种完全不同于您尝试的方法。如果您想坚持使用原始解决方案,我建议您使用while代替foreachbreak来编写循环。