Skip to content

Instantly share code, notes, and snippets.

@hisui
Created August 24, 2015 14:11
Show Gist options
  • Save hisui/2b37bf2f70fadb0f5137 to your computer and use it in GitHub Desktop.
Save hisui/2b37bf2f70fadb0f5137 to your computer and use it in GitHub Desktop.
class Coord private (val degree: Int) {
override def toString: String = degree + "deg"
}
object Coord {
def normalize(deg: Int): Int = if (deg < 0) normalize(deg + 360) else deg % 360
def apply(deg: Int): Coord = new Coord(normalize(deg))
}
case class Vision(lower: Coord, upper: Coord) { lhs =>
type Pair = (Int, Int)
def extract(o: Vision): Seq[Pair] =
if (o.lower.degree > o.upper.degree)
Seq((o.lower.degree, o.upper.degree + 360))
else Seq(
(o.lower.degree , o.upper.degree ),
(o.lower.degree+360, o.upper.degree+360)
)
@inline
def sub(a: Pair, b: Pair): Seq[Pair] =
if (a._1 > b._2 || a._2 < b._1) Seq()
else {
Seq((a._1, b._1),
(b._2, a._2)).filter(e => e._1 < e._2)
}
def - (rhs: Vision): Seq[Vision] = for {
a <- extract(lhs)
b <- extract(rhs)
c <- sub(a, b)
} yield Vision(Coord(c._1), Coord(c._2))
}
object Vision {
def apply(a: Int, b: Int): Vision = Vision(Coord(a), Coord(b))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment