Reputation: 4090
I am pretty new in NetLogo and I would like to create patchy world surface, where patches (yellow) are in different distances from central patch (dashed red circles).
More specifically, I want to select 3 random patches at distances: 10, 20, 30 patches from central patch and turn them yellow. I'm sure it is pretty simple to do that but I'm not able to figure out the right utilization of distance measure.
Any suggestion is highly appreciated !
to setup
clear-all
setup-patches
end
to setup-patches
clear-all
ask patches [ set pcolor green ]
ask patch 0 0
[
set pcolor yellow
ask patches in-radius 10 [set pcolor yellow] ; how to include distance here??
]
ask n-of 5 patches [ set pcolor yellow] ; or here ??
end
Upvotes: 2
Views: 1296
Reputation: 30453
Here's one more alternative:
to setup
clear-all
ask patches [ set pcolor green ]
foreach [10 20 30] [
repeat 3 [
make-yellow-patch ?
]
]
end
to make-yellow-patch [dist]
create-turtles 1 [
rt random-float 360
fd dist
while [pcolor = yellow] [
bk dist
rt random-float 360
fd dist
]
set pcolor yellow
die
]
end
sample results:
observer> show sort [round distancexy 0 0] of patches with [pcolor = yellow ]
observer: [10 10 10 19 20 20 30 30 30]
I don't think the 19 is a bug. As Nicolas shows, you don't want to favor any particular part of the circle, so that means you have to allow some variation in the distance.
Upvotes: 2
Reputation: 14972
This is trickier than it looks! You'd think you'd be able to just use distance
:
foreach [ 10 20 30 40 ] [
ask n-of 3 patches with [ distance patch 0 0 = ? ] [
set pcolor yellow
]
]
But here is the result if we drop the n-of 3
to get all the patches at each distance to turn yellow:
The problem is that it looks for patch at the exact distance specified.
Maybe you could just give it a little bit of tolerance, then?
foreach [ 10 20 30 40 ] [
ask patches with [
distance patch 0 0 > ? - 0.5 and
distance patch 0 0 < ? + 0.5
] [
set pcolor yellow
]
]
This is better, but still not quite right. Some part of the circles are thicker than other:
But maybe it's good enough for your purpose, or you could tweak the tolerance a bit.
Finally, here is a wacky way of achieving a similar result:
to-report circle-at [ d ]
let dummies []
let circle []
create-turtles 2 [
set dummies lput self dummies
]
ask last dummies [
setxy 0 d
create-link-with first dummies [ tie ]
]
repeat 3600 [
ask first dummies [
right 0.1
ask last dummies [
set circle lput patch-here circle
]
]
]
ask turtle-set dummies [ die ]
report patch-set circle
end
It uses the tie
command to have one turtle turn around another and add the patches that it traverses to our result set. It's slow, but it works, and you get a nice smooth circle:
Using this procedure, your final code would be:
foreach [ 10 20 30 40 ] [
ask n-of 3 circle-at ? [
set pcolor yellow
]
]
Upvotes: 2