NETLOGO patches squares and rectangles - netlogo

I'm attempting to make squares and rectangles with patches on netlogo, with the variables x (pxcor) y (pycor) w (width) l (length). I wrote
ask patch random w random h [set pcolor blue]
and was able to create regular rectangles and squares with lengths and widths I enter, but they always appear with the lower left corner at 0, 0. How can I create these shapes and have them appear with the upper left corner the x and y coordinates that I enter. Please, any help would be appreciated

In fact, your code instructs NetLogo to turn ONE patch blue, not a rectangle of patches. This is because random w selects a random number from 0 to (w-1) and random h selects a random number from 0 to (h-1). If the two random numbers chosen happen to be 3 and 2, you are telling NetLogo to change the color of patch 3 2 to blue.
If you are actually getting rectangles, you must be repeatedly selecting one random patch, but that's not in the code you provided.
In NetLogo, patch 0 0 is the centre of the world (though that can be changed with settings). Think about what you are trying to do. If you want (0,0) to be the upper left corner, then you want the rectangle to cover the space from pxcor of 0 to w and pycor of -h to 0 (possibly different, depending on whether you want 0,0 in the rectangle).
So you want something more like:
ask patches with [pxcor <= 3 and pxcor > 0 and pycor < 0 and pycor >= -2]
[ set pcolor blue ]

Related

How to set patch color in the diagonal 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
]

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: turtle placement

My simulation space is a total of 1024 patches. I need to randomly place 113 turtles within the middle area of my simulation space which is only 78 patches. Does anyone have any input on how to do this and make sure that the turtles are only in that specified area? Thanks!
Positions are simply numbers so you can set xcor and ycor in appropriate ranges by using the random-float generator and rescaling as required. For example, set xcor 3 + random-float 5 will locate the turtle with an x coordinate in the interval from 3 to 8

Computing an agent on the further ends of cone of vision

Given:
The wall(grey agents) are in a constant place along the top of the
world.
The blue agents always directly below but at various
distances. But they be off to the side of the gap but nevertheless
can be rotated so that they face the gap.
That the cone of vision angle is same for all blue turtles.
In the above figures, the blue agent's cone of vision is depicted. I wish to calculate the grey wall which meet the ends of the cone of vision ,that is, one on right and one on left.Also could I somehow calculate the x-coordinate at that point. Not the grey agent's coordinate as that would be a approximation.
To Compute:
The x coordinates where the extremes of cone of vision intersect grey turtles. Or those grey turtles they intersect.
Rough Figure:
So I wish to compute x_1 and x_2 in the below figure.
One way could as suggested by #JenB to divide it into three cases and and calculate A in each case.(Primarily on left or right). Then use trigonometry. I am correct. Are there any other ways as well?
If this is a 2D problem, it is simply a case of intersecting lines.
I would avoid using multiple cases; that is very prone to errors.
You will have a line that describes your wall of turtles, and two lines that describe your FOV boundaries. You can formulate each of these three lines in parametric form as [o.x,o.y] + [v.x, v.y] * s, which is a fixed point [o.x,o.y] plus a normal vector [v.x,v.y] scaled by s.
The wall of turtles is only defined for a certain domain of 's'; let's say domain of wall.s = [0 to 0.4, and 0.6 to 1]
I would describe how to find the intersection points, but intersections of parametric 2D lines is pretty standard fare, and is better shown in a PDF, so I'll refer you to this...
http://www.ahinson.com/algorithms_general/Sections/Geometry/ParametricLineIntersection.pdf
(remember never to divide by zero)
Once you know the values of the scale parameters 'left.wall.s' and 'right.wall.s', you can tell whether the domain of the turtle wall is within the view of the player. Also you can determine the intersection points simply by plugging back into the parametric line formulas.
dwn's answer covers computing the precise point of intersection.
You said you were also interested in just finding out what patch the answer lies on. Here's code for that:
to setup
clear-all
create-turtles 1 [
set heading -30 + random 60
]
ask turtles [
;; show center of vision cone
ask boundary-patch [ set pcolor red ]
;; show edges of 20 degree vision cone
lt 10
ask boundary-patch [ set pcolor blue ]
rt 20
ask boundary-patch [ set pcolor blue ]
;; restore turtle's original heading
lt 10
]
end
;; answers the question, what patch on the top row of the
;; world is the turtle currently facing?
to-report boundary-patch ;; turtle procedure
let n 0
while [true] [
let target patch-ahead n
if target = nobody or [pycor = max-pycor] of target [
report target
]
set n n + 1
]
end
Sample result:
Of course, it would actually be computationally more efficient to compute the answer directly, via a formula. (With an optional rounding step at the end, depending on whether you want a point or a patch.) But this code shows how to do it without having to do any tricky math.
The following trigonometry approach(suggested by #JenB) works perfect:
to-report calx2 [x0 y0 x1 y1 A]
report x0 + (y1 - y0) * tan ( A + atan (x1 - x0) (y1 - y0))
end
to start
ask turtles[
set corner-1 list calx2 xcor ycor ([pxcor] of patch-goal)([pycor] of patch-goal - 0.4) (-45) ([pycor] of patch-goal - 0.4)
set corner-2 list calx2 xcor ycor ([pxcor] of patch-goal)([pycor] of patch-goal - 0.4) ( 45) ([pycor] of patch-goal - 0.4)
]
The problem just arises when the left edge goes beyond 180 and right edge go beyond 0. I didn't consider that cases. Anyways, the above code solves the problem.

Distributing turtles at a patch

In the image below, the black box represents a patch. I wish to create agents of size-
(Patch-size/8)
and distribute them at the top of the patch as such.
Also, possible to to create a code such that it takes :
Patch-Size as input and distributes them accordingly.
Previous Code Used:
My previous approach sprout at the centre and moves them to align with the desired positions but it is considerably length and not effective if I wish to variate number of agents.
`
Ok, so, forget about patch-size for a moment, as we are going to be working with unit patches, without regard to the number of pixels.
I'm assuming the number of turtles is variable, and that we want the turtles to fit exactly inside the width of the patch, and with the tops of the turtles aligned with the top edge of the patch.
If the number of turtles is constant, or the maximum number is constant, and/or the size is constant, then the code can be a little more simple, as we can avoid recalculating some things.
Anyway, for C turtles:
The gap between the turtles is 1 / C
The "half-gap" is also useful, that's gap / 2
This is also the size of the turtles
Hence, the turtles will definitely just fit across the patch
The ycor of the turtles will be pxyor + .5 - half-gap
Since gap is the size, this puts the turtles along the top
The xcor will be pxcor - .5 + gap * N - half-gap
N is the number of the current turtle. So, the xcor varies from a half-gap from the left edge to a half-gap from the right edge.
Here, pxcor - .5 shifts the turtle center all the way to the left edge
Then, + gap * N shifts the turtle over N turtle widths,
Then, - half-gap shifts the turtle back one half turtle size.
This makes sure the first and last turtles are just touching the edge.
So, let's do it:
to align-inside-at-top ;; patch procedure
let counter count turtles-here ;; we will use this as a count-down, after using it in some calculations
if counter > 0 ;; could assume there are turtles, but we are not.
[ let gap 1 / counter ;; size of turtles, gap between turtles
let half-gap gap / 2 ;; half-size of turtles
let ytop pycor + .5 - half-gap
let xleft pxcor - .5 - half-gap
ask turtles-here
[ set size gap
set ycor ytop
set xcor xleft + gap * counter
set counter counter - 1 ;; so we're placing them from right to left
]
]
end
Hope this helps!
keep in mind no matter the patch-size patches are always 1 step across.
( pxcor - .5, pxcor + .5) X (pycor - .5, pycor +.5)
Patch-size is a sort of zoom it doesn't normally effect a simulation.
With that said and assuming you are doing this as some sort of demonstration or visualization. Anything you do for aesthetics tends to slow things down but here you go.
To line-up
Let c count turtles-here
Ask turtles-here
[
Set ycor pycor - .45
Set xcor pxcor + .45 - c / patch-size
Let c c - 1
]
End
If turtles on a patch > patch size it will mess up.