Abhishek Bhatia
Abhishek Bhatia

Reputation: 9796

Find the presence of other turtles in a given direction upto a distance

I wish to find out whether in a given turtle's heading there is another agent present upto a given distance.enter image description here

Here the Distance is "D".

Note:

Any agent present before D in the given direction should be also considered.

Even the direction doesn't coincide with the other's agent centre but just touches it ,even then that agent should be considered.

Problem:

No turtle-ahead procedure available. Combination of patch-ahead and turtles-on not applicable due to patch-size>> turtle-size. enter image description here Possible approach: 1.Represent the turtle's heading by the equation of a line.

to-report calculate-line[x y angle]
  let m tan angle
  let A m 
  let B -1 
  let C (- m * x + y) 
report (list A B C)
end
to-report heading-to-angle [ h ]
  report (90 - h) mod 360
end

let line-equ calculate-line (xcor) (ycor) (heading-to-angle heading)

2.Calculate the perpendicular distance from other turtles here, Check if there are within a range that the size of other turtles.

to-report value[A X1 Y1];;A is list of coefficents of line, x1 and y1 are coordinates of red turtle
  if-else(abs((item 0 A * X1 + item 1 A * Y1 + item 2 A) / (sqrt((item 0 A ^ 2) + (item 1 A ^ 2) ))) < [size] of wall )
 [ report "true"][report "false"]

end

3.To check if the red turtle is within D. One could obtain a line perpendicular to black one and compute the red turtle distance from it to check if it is less than or equal to D. But then that adds more complication.(Though one can simplify rotate the turtle by 90 left or right and get the line equation.)

Upvotes: 2

Views: 1244

Answers (2)

JenB
JenB

Reputation: 17678

This is what I meant by my comment. Run this code (as its own model). What it does is turn all the turtles on a few 'ahead' patches a different colour. I know this is not what you are trying to do, but the agentset candidates is a relatively small number of turtles. These are the only ones that you have to check whether they are on the correct path. So instead of turning them a different colour you could check the direction that they are from your initial turtle.

to setup
  clear-all
  set-patch-size 25
  resize-world -10 10 -10 10
  create-turtles 1000
  [ setxy random-xcor random-ycor
    set color yellow
    set size 0.4
  ]
  ask one-of turtles
  [ set color red
    set size 1
    check-from-me 5
  ]
end

to check-from-me [howfar]
  let counter 0
  let candidates turtles-here
  while [counter < howfar]
  [ set counter counter + 1
    set candidates (turtle-set candidates turtles-on patch-ahead counter)
  ]
  ask candidates [set color red]
end

Upvotes: 2

Abhishek Bhatia
Abhishek Bhatia

Reputation: 9796

   to-report check-wall 
   let return false
  hatch 1[
    set color black
    set size ([size] of one-of walls) / 2
    show (2.5 * ([size] of myself))
    while [distance myself < (2.5 * ([size] of myself))]
    [

      fd ([size] of one-of walls) / 64
      if any? walls in-radius size
      [
        set return true
        ]
      show distance myself
      ]

    ]
  report return
  end

The above works. But still is approx. I am looking for better solution with probably less maths as one elucidated in the question.

Upvotes: 1

Related Questions