Skip to content

Instantly share code, notes, and snippets.

@notyy
Created May 9, 2016 06:52
Show Gist options
  • Save notyy/2318acecb83bd79f32ebd53832b46ac7 to your computer and use it in GitHub Desktop.
Save notyy/2318acecb83bd79f32ebd53832b46ac7 to your computer and use it in GitHub Desktop.
import scala.math._
case class Point(x:Int, y:Int)
def isSquare(p1: Point, p2: Point, p3: Point, p4: Point): Boolean = {
val points = Set(p2,p3,p4)
(for{
p1_x_Neighbor <- points.find(_.x == p1.x)
p1_y_Neighbor <- points.find(_.y == p1.y)
if abs(p1_y_Neighbor.x - p1.x) == abs(p1_x_Neighbor.y - p1.y)
} yield{
val lastPoint = (points -- Set(p1_x_Neighbor, p1_y_Neighbor)).head
val sideLength = abs(p1_x_Neighbor.y - p1.y)
(abs(lastPoint.x - p1.x) == sideLength) && (abs(lastPoint.y - p1.y) == sideLength)
}).getOrElse(false)
}
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