Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Point(p: (Int, Int)) extends Comparable[Point] {
- val _hashCode = 31 * x + y
- /**
- * @return the X coordinate of this Point
- */
- def x: Int = p._1
- /**
- * @return the Y coordinate of this Point
- */
- def y: Int = p._2
- def +(p: (Int, Int)) = Point(x + p._1, y + p._2)
- def +(p: Point) = Point(x + p.x, y + p.y)
- def -(p: (Int, Int)) = Point(x - p._1, y - p._2)
- def -(p: Point) = Point(x - p.x, y - p.y)
- def *(p: (Int, Int)) = Point(x * p._1, y * p._2)
- def *(p: Point) = Point(x * p.x, y * p.y)
- def /(p: (Int, Int)) = Point(x / p._1, y / p._2)
- def /(p: Point) = Point(x / p.x, y / p.y)
- /**
- * @return the magnitude (length) of this point
- */
- def mag = {
- math.sqrt(magSq)
- }
- /**
- * @return the squared magnitude (length) of this point
- */
- def magSq = {
- x * x + y * y
- }
- /**
- * The distance between this point and the given coordinates
- * @param x the X coordinate to calculate the distance to
- * @param y the Y coordinate to calculate the distance to
- * @return the distance between this point and the given coordinates
- * (this - (x, y)).mag
- */
- def distance(x: Int, y: Int) = {
- (this -(x, y)).mag
- }
- /**
- * The distance between this point and the given coordinates
- * @param p the coordinates to calculate the distance to
- * @return the distance between this point and the given coordinates
- * (this - coords).mag
- */
- def distance(p: (Int, Int)) = {
- (this - p).mag
- }
- /**
- * The distance between this point and the given coordinates
- * @param p the coordinates to calculate the distance to
- * @return the distance between this point and the given coordinates
- * (this - p).mag
- */
- def distance(p: Point) = {
- (this - p).mag
- }
- /**
- * The squared distance between this point and the given coordinates
- * @param x the X coordinate to calculate the distance to
- * @param y the Y coordinate to calculate the distance to
- * @return the squared distance between this point and the given coordinates
- * (this - (x, y)).magSq
- */
- def distanceSq(x: Int, y: Int) = {
- (this -(x, y)).magSq
- }
- /**
- * The squared distance between this point and the given coordinates
- * @param p the coordinates to calculate the distance to
- * @return the squared distance between this point and the given coordinates
- * (this - p).magSq
- */
- def distanceSq(p: (Int, Int)) = {
- (this - p).magSq
- }
- /**
- * The squared distance between this point and the given coordinates
- * @param p the coordinates to calculate the distance to
- * @return the squared distance between this point and the given coordinates
- * (this - p).magSq
- */
- def distanceSq(p: Point) = {
- (this - p).magSq
- }
- override def hashCode(): Int = _hashCode
- override def equals(obj: scala.Any): Boolean = {
- obj match {
- case p: Point => return p.x == x && p.y == y
- case (pX, pY) => return pX == x && pY == y
- }
- false
- }
- override def toString = "( " + x + ", " + y + " )"
- override def compareTo(o: Point): Int = {
- if (x != o.x) return if (x < o.x) 1 else -1
- if (y != o.y) return if (y < o.y) 1 else -1
- 0
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement