Bryan Head
Bryan Head

Reputation: 12580

Get relative coordinates of other agents

What's the best way to get coordinates of one agent relative to the coordinates of another agent in such a way that respects world wrapping?

That is, the naive solution is:

to-report relative-xcor [ other-agent ]
  report [ xcor ] of other-agent - xcor
end

(and similar for ycor) The problem with that code is that it will be wrong when, for instance, the main agent is on the far right side of the screen and other-agent is on the far left.

Ideas? My best idea so far is to use towards and trigonometry, but I'm wondering if there's a better way.

Edit:

To give an example of why the above is wrong, suppose your world has min-pxcor = -5 and max-pxcor = 5, and has wrapping on. If we have turtle 0 at xcor = 5 and turtle 1 at xcor = -5, then [ relative-xcor turtle 1 ] of turtle 0 would give -10 whereas the correct answer is 1 (due to world wrapping). I suppose implicit in my question is that it should give the smallest (by absolute value) relative coordinate.

Upvotes: 0

Views: 75

Answers (1)

Bryan Head
Bryan Head

Reputation: 12580

(OP here) So the only solution I've come up with so far is:

to-report rel-xcor [ other-agent ]
  report ifelse-value (self = other-agent) [
    0
  ] [
    0 - (sin towards other-agent) * distance other-agent
  ]
end

to-report rel-ycor [ other-agent ]
  report ifelse-value (self = other-agent) [
    0
  ] [
    0 - (cos towards other-agent) * distance other-agent
  ]
end

(recall that since netlogo angles are flipped, cos gives y coordinate and sin gives x)

But that seems overly complicated. But if anyone stumbles on this question wondering the same thing, this is what I'm currently using. I'll happily accept a better answer though.

Upvotes: 1

Related Questions