Meow
Meow

Reputation: 330

NetLogo - how do I get all patches the turtle facing?

How do I get a patch set that contains all patches that the turtle is facing?

I know patch-ahead report the patch with a specific distance. But what if I want to get all patches in this direction instead of the single one with specific distance?

Upvotes: 0

Views: 243

Answers (1)

JenB
JenB

Reputation: 17678

What you can do is hatch a turtle and move it forward until it reaches the edge of the world, adding all the patches it crosses.

Here's a visible version to see the approach:

to testme
  clear-all
  create-turtles 1 [setxy random-xcor random-ycor]
  ask one-of turtles
  [ set pcolor red
    hatch 1
    [ while [can-move? 1]
      [ forward 1
        set pcolor red
      ]
      die
    ]
  ]
end

To actually do the patchset version, you need to start with the current patch and add the patches as the hatched turtle moves over them. Try this for a procedure version and a demonstration of how it can be used:

turtles-own [ my-path ]

to testme
  clear-all
  create-turtles 1 [setxy random-xcor random-ycor]
  ask one-of turtles
  [ set my-path get-patches-forward self
    print my-path
  ]
end

to-report get-patches-forward [ #me ] ; turtle procedure
  let front-patches patch-here
  hatch 1
  [ while [can-move? 1]
    [ forward 1
      set front-patches (patch-set front-patches patch-here)
    ]
    die
  ]
  report front-patches
end

This will return the wrong answer if the world is wrapped because the hatched turtle can keep on going indefinitely. Instead, you would need to check its coordinates rather than relying on the can-move? primitive.

Upvotes: 2

Related Questions