1

如何在下面的代码中完成以下操作:

补丁改变颜色以反映它们与“min-pycor”行的距离

例如,颜色从黄色变为红色,然后变为黑色(表示死亡)。

但这要考虑到黄色斑块的产生>红色>黑色。

turtles-own
[
 stem?     ;; true for stem cells, false for transitory cells
 age  ;; age of cell. changes color with age
 metastatic?  ;; false for progeny of stem cell 0, true for progeny of stem cell 1
]

globals
[
 cell-count
]

to setup
clear-all
set-default-shape turtles "square"
ask patches[
  if pycor = min-pycor [
    ifelse random 10 <= 2 
     [set pcolor white]
     [sprout 1 [set shape "square" set color blue] ] 
 ]
 ]
evaluate-params
reset-ticks
end

to go
ask patches with [pcolor = yellow]
[if count neighbors with [pcolor = black] > 0
 [ask one-of neighbors with [pcolor = black][set pcolor yellow]
   ]
]

ask patches with [pcolor = white] 
[if count neighbors with [pcolor = black] > 0
[ask one-of neighbors with [pcolor = black][set pcolor yellow]
  ]
]
tick
 end
 ;;transitional cells move and hatch more. Turtle proc.
  to move-transitional-cells
  if (not stem?)
 [
  set color ( red + 0.25 * age )
  fd 1
  if (age < 6)
  [
    hatch 1
    [  ;amplification
     rt random-float 360
     fd 1
    ]
  ]
   ]
  end

 to mitosis ;; turtle proc. - stem cells only
 if stem?
 [
  hatch 1
  [
  fd 1
  set color red
  set stem? false
  ifelse (who = 1)
    [ set age 16 ]
    [ set age 0 ]
   ]
   ]
  end

 to death   ;; turtle proc.
 if (not stem?) and (not metastatic?) and (age > 20)
  [ die ]
 if (not stem?) and metastatic? and (age > 4)
  [ die ]
 end

to evaluate-params
set cell-count count turtles  ;cell count
if (cell-count <= 0)
 [ stop ]
end

to kill-original-stem-cell
 ask turtle 0
 [ die ]
 end

to kill-moving-stem-cell
ask turtle 1
[ die ]
end

 to kill-transitory-cells
 ask turtles with [ age < 10 and not stem? ]
  [ die ]
 end
4

1 回答 1

2

您似乎有两个相互矛盾的要求,即基于代码中接近度的颜色变化,以及基于您询问的 PYCOR 的颜色变化。

暂时忽略代码,我们可以通过多种方式基于 PYCOR 设置颜色。例如,我们可以创建色带,我们可以创建颜色的抖动混合,或者我们可以创建颜色之间的“平滑”过渡。

第一个很容易。我们可以使用 IFELSE 结构。此示例创建偶数条带,但您可以更改“分割”变量以创建任意高度的条带。

let color1 red
let color2 yellow
let color3 black

let divide1 (min-pycor + world-height * 0.33)
let divide2 (min-pycor + world-height * 0.66)

ask patches
[ ifelse pycor < divide1 [ set pcolor color1 ][
  ifelse pycor < divide2 [ set pcolor color2 ][
                           set pcolor color3
  ]]
]

我们也可以用数学的方式来做。此示例创建偶数带。

let colors [ red yellow black ]
let bands length colors 
let band-height floor ( world-height / bands + .5 )
;; pycor - min-pycor shifts the range from 0 to world-height
ask patches [ set pcolor item ( floor ( ( pycor - min-pycor ) / band-height ) ) colors ]

我们可以通过在 pycor 中引入随机元素来创建抖动带。我们还必须添加一些测试以使随机数保持在范围内。

let colors [ red yellow black ]
let bands length colors 
let band-height floor (world-height / bands + .5)
let dither band-height * .5 ;; adjust this to change dithery-ness
ask patches
[ let py pycor - min-pycor + dither - random-float ( dither * 2 ) 
  ;; you might want to really study the above line to fully grok what's happening there
  if py < 0 [ set py 0 ]
  if py > world-height [ set py world-height ]
  set pcolor item ( floor ( py / band-height ) ) colors
]

渐变(渐变)色带更难,因为 NetLogo 色彩空间不做色调的渐变,只有色调和阴影,所以真的没有办法从红色变成黄色。所以我们必须使用 RGB(三值列表)颜色,而不是 NetLogo(单值)颜色。这超出了我目前愿意输入的范围,所以你去吧——作为一个锻炼者。

于 2013-02-05T14:12:51.413 回答