## Re: [netlogo-users] i need help

Expand Messages
• ... Baishakinaha, That s a tricky one! There s lots of different ways to solve that problem, and one important question that may help choose the best way is:
Message 1 of 2 , Oct 31, 2004
• 0 Attachment
baishakinaha <baishakinaha@...> wrote:

> can someone help me. I need to make a procedure in netlogo so that
> everytime the turtle makes a square i want it to fill in with color
> in that area. Please email me back if you can help me.

Baishakinaha,

That's a tricky one!

There's lots of different ways to solve that problem, and one
important question that may help choose the best way is: "How is the
turtle drawing the square?"

And the next is: Why are we filling in the square?

See, if the answer is, "because we want to QUICKLY paint filled-in
squares, well, here's the way I would do it:

;; this code is run by a turtle on the patch.
;; makes a filled in square with the current patch or turtle as the
south-west corner.
;; fills the patches with the color of the patch calling this procedure.
;; ROUNDS the turtle heading to the nearest 90 degrees
;; takes as input the length of one side of the square:

to draw-filled-square [ side ]
[ pxcor >= pxcor-of myself
and pxcor <= pxcor-of myself + side - 1
and pycor >= pycor-of myself
and pycor <= pycor-of myself + side - 1
]
[ set pcolor pcolor-of myself ]
end

However, this could be considered "cheating'" if the goal of the
procedure was to do something clever with turtles.

Here's another way, that's more "turtle-centric" (and recursive, to boot!)

to draw [ side ]
; marks this spot
stamp color
; drop the pen, and prepare to draw
pen-down
; draw a square this size
; subtract one, because just staying put is a 1 unit square...
repeat 4
[ fd side - 1
rt 90
]
; stop drawing
pen-up
; if side is big enough that there is still interior to be filled in,
if side >= 3
[ ; move into interior
jump 1
rt 90
jump 1
lt 90
; draw a smaller square
draw (side - 2)
; move back to starting point.
jump -1
rt 90
jump -1
lt 90
]
end

Hope this helps!

~~James.
Your message has been successfully submitted and would be delivered to recipients shortly.