-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
InRadiusSimple.scala
33 lines (22 loc) · 1.01 KB
/
InRadiusSimple.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// (C) Uri Wilensky. https://github.com/NetLogo/NetLogo
package org.nlogo.agent
import
java.util.{ List => JList }
object InRadiusSimple {
def apply(world: World)(agent: Agent, sourceSet: AgentSet, radius: Double, wrap: Boolean): JList[Agent] = {
import scala.collection.JavaConverters.{ iterableAsScalaIterableConverter, seqAsJavaListConverter }
val getAgentXAndY: PartialFunction[Agent, (Double, Double)] = {
case t: Turtle => (t.xcor, t.ycor)
case p: Patch => (p.pxcor, p.pycor)
}
val (originX, originY) = getAgentXAndY(agent)
val isInRadius = (agent: Agent) => {
val (xcor, ycor) = getAgentXAndY(agent)
world.protractor.distance(xcor, ycor, originX, originY, wrap) <= radius
}
// `collect` is used in place of `map`ping to `Agent` and `filter`ing, in
// order to reduce iterations over the collection. --JAB (6/19/14)
val newAgents = sourceSet.agents.asScala.collect { case agent: Agent if isInRadius(agent) => agent }
newAgents.toSeq.asJava
}
}