如何给定矩形内的色块着色 [英] How to color patches within a given rectangle

查看:219
本文介绍了如何给定矩形内的色块着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a grid containing several rectangles. These rectangles are represented by several orange patches and each rectangle is delimitated by white corridors.

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

How can I color patches within a given orange rectangle ?

谢谢.

这是代码的开头:

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

推荐答案

下面是一些制作橙色矩形补丁的示例代码:

Here's some example code for making an orange rectangle of patches:

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

我想这还不能完全回答您的问题.您已经有一些橙色色块和一些白色色块,这些色块应该以某种方式控制您要绘制的矩形的位置……类似吗?

I'm guessing that doesn't fully answer your question though. You have already have some orange patches and some white patches and those should somehow control the location of the rectangle you want to draw... something like that?

更新

好,这个问题现在更清楚了.

OK, the question is lot clearer now.

您可以通过许多不同的方法来解决此问题.这是我能想到的最优雅的一个:

There's a ton of different ways you can solve this. Here's the most elegant one I can think of:

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编写循环.

This is a completely different approach than the one you were trying. If you want to stick with your original solution approach, I would suggest writing your loops using while instead of foreach plus break.

这篇关于如何给定矩形内的色块着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆