Created
May 9, 2016 07:14
-
-
Save binshuohu/fdeb9c9b6b6a2734ea89e345371bbc1e to your computer and use it in GitHub Desktop.
puzzle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://bitbucket.org/snippets/centaur/6K4kR | |
case class Point(x: Int, y: Int) | |
def isSquare(p1: Point, p2: Point, p3: Point, p4: Point): Boolean = { | |
val l = List(p1, p2, p3, p4).combinations(2).map{ | |
case List(a, b) => | |
val delta_x = a.x - b.x | |
val delta_y = a.y - b.y | |
delta_x * delta_x + delta_y * delta_y | |
}.toList.sorted | |
val (a, b) = l.splitAt(4) | |
a.forall(x => x == a.head) && | |
b.forall(x => x == b.head) && | |
(a.sum == b.sum) | |
} | |
assert(!isSquare(Point(1, 6), Point(6, 7), Point(2, 7), Point(9, 1))) | |
assert(!isSquare(Point(4, 1), Point(3, 4), Point(0, 5), Point(1, 2))) | |
assert(isSquare(Point(4, 6), Point(5, 5), Point(5, 6), Point(4, 5))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment