Skip to content

Instantly share code, notes, and snippets.

@CaffeinatedDave
Created July 23, 2014 11:32
Show Gist options
  • Save CaffeinatedDave/1c2707378d0a0499ee30 to your computer and use it in GitHub Desktop.
Save CaffeinatedDave/1c2707378d0a0499ee30 to your computer and use it in GitHub Desktop.
West London Hack Night - Voronoi
object Voronoi extends App {
case class Point(x: Int, y: Int)
val pubs: List[Point] = List(Point(1,4), Point(2,1), Point(4,3))
def findDist(start: Point, pub: Point): Int = {
Math.abs(start.x - pub.x) + Math.abs(start.y - pub.y)
}
def getClosestForPoint(point: Point, pubList: List[Point]): List[Point] = {
pubList.groupBy(x => findDist(point, x)).toList.sortBy(x => x._1).head._2
}
def findClosestForGrid(pubList: List[Point]): List[(Point, List[Point])] = {
(for (x <- 0 to 4; y <- 1 to 5) yield {
(Point(x,y), getClosestForPoint(Point(x,y), pubList))
}).toList
}
val allClosest = findClosestForGrid(pubs).filter(x => x._2.length > 1)
//for (x <- allClosest) println(x)
def neighbours(p: Point): Seq[Point] = {
for(dx <- -1 to 1; dy <- -1 to 1 if dx != 0 || dy != 0) yield Point(p.x +dx, p.y+dy)
}
val indecisionPoints = allClosest map (x => x._1)
// for (i <- indecisionPoints) println(i)
val segments = indecisionPoints map (x => (x, neighbours(x) filter (p => indecisionPoints.contains(p))))
for (l <- segments) println(l)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment