How to set patch color in the diagonal Netlogo - netlogo

Want to draw a river diagonal to the center of the of the world, however ive found that i would have to do a list with all the patches i want to color, is there a faster way to color the diagonal?
Thanks
I need this diagonal, form the middle of the map upwards, Its even tougher to me as its going backwords and doesnt end in the top most middle.

This code:
ask patches with [pxcor = pycor] [
set pcolor green
]
Draws this:
And here's a version that does the diagonal in the upper right corner:
ask patches with [pxcor + pycor = max-pxcor] [
set pcolor green
]

Related

Netlogo: Says its in turtle context when it is not

I have to make a button that colors in the patches in the bottom half of the grid randomly with darker shades of a given color based off of a slider, and the top of half of the grid randomly with lighter shades of a given color based off of the slider. When I wrote my code it says that I can't use my command because it is in patch-context because by command is in turtle-only context. I am confused because I am not using turtles, I am only using patches. Inside of my button I wrote "colorRandomShade2" which is the name of my command. The button is also in patch context and its display name is "colorRandomShade2".
Code:
to colorRandomShade2
if ycor > 0 [ set pcolor ( main_color + random 5 ) ]
if ycor < 0 [ set pcolor ( main_color + random 9 ) ]
end
Patch coordinates are pxcor and pycor, but you have used xcor and ycor, which are the variable names for coordinates of a turtle. So you are pressing the button to call the code and the first thing that NetLogo sees is a request to look at the ycor or y-coordinate of a turtle.

Netlogo: Creating a square area of patches in the center of the grid

I am trying to create a square of brown patches centered at the origin of my grid. Previously, I had a slider on the interface called "sink-patch-radius" that goes from 0 to 20. Then in my code, I created a circular set of patches centered at the origin that were colored brown + 2 and had a radius of "sink-patch-radius", and the surrounding patches were green. Here is the code that worked for that (thank you to JenB for this!):
;;create the 'sink'
let sink-centre patch 0 0
set sink-patches [patches in-radius sink-patch-radius] of sink-centre
ask sink-patches [ set pcolor brown + 2 ]
;; create the 'source'
set source-patches patches with [pcolor != brown + 2]
ask source-patches [ set pcolor green ]
So now, I want that idea to stay the same, but instead of a circular "sink area", I want it to be a square. I know the above code will have to change, maybe even quite a bit, because you cannot use "in-radius" to make a square. I'm thinking of maybe changing the slider to be "sink-patch-length" so that it adjusts the length of the sides of the square. My question would then be: how do I incorporate that into my code, so that I get a square of brown patches centered at the origin? For example, if "sink-patch-length" is set to 20, then I would want a 20x20 square (400 cells) centered at the origin to have a pcolor of brown + 2.
Any help is greatly appreciated! Thank you.
Use the inbuilt coordinate system. So you want (something like) patches with [abs(pxcor) <= sink-patch-length and abs(pycor) <= sink-patch-length]. If you want it centred somewhere other than the middle, you will need to do some fiddling to make the boundaries correct.

Netlogo random patches setup without them touching at all

I am trying to set up random patches that have no corners touching at all, and the color of those patches are different colors of green (to visually represent each patch having a random quality value in my model). My conceptual idea of how to do this is to have the “false” part of my “ifelse” statement to basically tell the program to keep trying to assign the patch a different place if it has another patch touching any part of it, until all patches have a place with no other patches touching it all (including corners). I just have no idea if that is possible or what code could do that. Is there a netlogo primitive that is equivalent to "until" ? Any help or ideas would be appreciated!
Here is a part of my code:
patches-own [ quality ]
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to setup-patches
ask n-of number-of-patches patches
[ ifelse sum [ pcolor ] of neighbors = 0
[ set quality random (2 + random 8)
set pcolor scale-color green quality 0 10
]
[ set pcolor black ; this is the line I need to change
]
]
end
Have a look at the primitives while and neighbors in the NetLogo dictionary. I am not completely clear on the sequencing you want in your code so I can't provide a full answer. However, you want something like:
while any? neighbors [(however you define the ones you don't want to touch]
[(try again)
]
Note that patches always touch other patches because the world is constructed as a grid of patches. So you presumably want patches with specific conditions (such as high values of quality) to not touch each other.
As JenB says, patches are always touching other patches, so I'm assuming the question is "How can I generate number-of-patches green patches that aren't touching any other green patches?". Then I'd do this:
to setup-patches
while [ count patches with [ shade-of? green pcolor ] < number-of-patches ]
[ ask one-of patches with [ quality = 0 and count neighbors with [ shade-of? green pcolor ] = 0 ]
[ set quality random (2 + random 8)
set pcolor scale-color green quality 0 10 ] ]
end
Basically, while the number of green patches is less than the desired number of green patches, choose one of the patches that isn't green and doesn't have any green neighbors. Then turn it green.

How can I simulate a moving patch?

I'm trying to make a Frogger-like game where the patches move around and the turtle can only move onto a safe patch. If I have a few patches that are red, how can I 'move' them around as if they were turtles? I currently have this, but it seems to move more than 1 patch at a time occasionally and as a result, some red patches will be destroyed if there are more than one that is red:
if pcolor = red
[ ask patch-at 0 1
[ set pcolor red]
set pcolor black ]
You could ask the neighbor patch (left or right) to be painted red and the actual patch to be painted back to black or whatever the default color is.
For this you'll want to get the actual patch coordinates. Using patch-at asks the patch at 0,1 relative to the whole world.

Netlogo - Filling neighboring patches with specific colored turtles until full

I'm trying to ask white turtles to create yellow turtles to one of the 8 empty neighboring spaces. If there is no free space the turtle should produce nothing.
Note: white turtles stay white and produce yellow turtles which are able to reproduce themselves as well as other yellow turtles. In summary, at the end I would like to fill up the black spaces with yellow turtles.
breed [ cells cell ]
cells-own [ n ]
to setup
clear-all
set-default-shape cells "square"
ask patches [ if pycor = min-pycor [sprout-cells 1]]
ask cells [ ifelse random 10 < 2 [set color white] [set color yellow]]
Thanks for the reply.
I'll explain what I'm trying to do:
The white squares represent "stem cells" which have the ability to reproduce themselves and produce another type of cell (e.g. TA cell), so that, initially each white cell will produce another cell above it or at one of its above corners.
In the second step, each TA cell produces other cells randomly in any empty space around it.
Third, stem cells (white squares) repeat step 1 and at the same time TA cells fill in one of the empty spaces around them. The cells (both stem cells and TA cells) stop producing new cells once they have no empty space around them (when the 8 neighbors are already filled in).
At the final stage all of the black space should be filled in with TA cells. The yellow squares at the last row basically do nothing.
Thanks again for your help.
reset-ticks
end
to go
ask cells
[ set n count neighbors with [pcolor = yellow] ]
ask cells
[ if n >= 1
[ set color yellow] ]
tick
;ask cells
;[if ticks = 10
;[set color yellow]]
end
What exactly you are trying to do is not entirely clear to me. I am going to go with a likely interpretation, but whether or not my interpretation is correct, you should try to clarify your question.
Trying to understand your code, it seems to me like there is a bit of confusion between patches and turtles. In your go procedure, you ask cells to turn yellow if they have a yellow neighbor. I think that what you want is for a yellow square to appear on patches that are neighbors of yellow squares. The empty patches you are trying to fill don't have any cells on them yet, so ask cells to turn yellow will not help you here.
If you really want to stick with cell agents, keeping your current setup procedure, you could do something like:
to go
ask patches with [not any? turtles-here] [
if any? neighbors with [any? turtles-here] [
sprout-cells 1 [
set color yellow
]
]
]
end
(If you want your cells to grow from the bottom up only, you should turn off wrapping in the view settings by right clicking on the view and choosing Edit...)
Now, a completely different approach, that may be simpler if all you want is some kind of cellular automaton, would be to ditch agents and work only with patches. That would give you something like:
to setup
clear-all
ask patches [
if pycor = min-pycor [
ifelse random 10 < 2
[set pcolor white]
[set pcolor yellow]
]
]
reset-ticks
end
to go
ask patches with [pcolor = black] [
if any? neighbors with [pcolor = yellow or pcolor = white] [
set pcolor yellow
]
]
tick
end
Both approaches are valid. The first one is a bit more visually pleasing. The second one is slightly simpler because you don't need turtles at all. It depends on what you ultimately want to do.