Advertisement
PaleoCrafter

Untitled

May 26th, 2014
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.27 KB | None | 0 0
  1. class Point(p: (Int, Int)) extends Comparable[Point] {
  2.  
  3.   val _hashCode = 31 * x + y
  4.  
  5.   /**
  6.    * @return the X coordinate of this Point
  7.    */
  8.   def x: Int = p._1
  9.  
  10.   /**
  11.    * @return the Y coordinate of this Point
  12.    */
  13.   def y: Int = p._2
  14.  
  15.   def +(p: (Int, Int)) = Point(x + p._1, y + p._2)
  16.  
  17.   def +(p: Point) = Point(x + p.x, y + p.y)
  18.  
  19.   def -(p: (Int, Int)) = Point(x - p._1, y - p._2)
  20.  
  21.   def -(p: Point) = Point(x - p.x, y - p.y)
  22.  
  23.   def *(p: (Int, Int)) = Point(x * p._1, y * p._2)
  24.  
  25.   def *(p: Point) = Point(x * p.x, y * p.y)
  26.  
  27.   def /(p: (Int, Int)) = Point(x / p._1, y / p._2)
  28.  
  29.   def /(p: Point) = Point(x / p.x, y / p.y)
  30.  
  31.   /**
  32.    * @return the magnitude (length) of this point
  33.    */
  34.   def mag = {
  35.     math.sqrt(magSq)
  36.   }
  37.  
  38.   /**
  39.    * @return the squared magnitude (length) of this point
  40.    */
  41.   def magSq = {
  42.     x * x + y * y
  43.   }
  44.  
  45.   /**
  46.    * The distance between this point and the given coordinates
  47.    * @param x the X coordinate to calculate the distance to
  48.    * @param y the Y coordinate to calculate the distance to
  49.    * @return the distance between this point and the given coordinates
  50.    *         (this - (x, y)).mag
  51.    */
  52.   def distance(x: Int, y: Int) = {
  53.     (this -(x, y)).mag
  54.   }
  55.  
  56.   /**
  57.    * The distance between this point and the given coordinates
  58.    * @param p the coordinates to calculate the distance to
  59.    * @return the distance between this point and the given coordinates
  60.    *         (this - coords).mag
  61.    */
  62.   def distance(p: (Int, Int)) = {
  63.     (this - p).mag
  64.   }
  65.  
  66.   /**
  67.    * The distance between this point and the given coordinates
  68.    * @param p the coordinates to calculate the distance to
  69.    * @return the distance between this point and the given coordinates
  70.    *         (this - p).mag
  71.    */
  72.   def distance(p: Point) = {
  73.     (this - p).mag
  74.   }
  75.  
  76.   /**
  77.    * The squared distance between this point and the given coordinates
  78.    * @param x the X coordinate to calculate the distance to
  79.    * @param y the Y coordinate to calculate the distance to
  80.    * @return the squared distance between this point and the given coordinates
  81.    *         (this - (x, y)).magSq
  82.    */
  83.   def distanceSq(x: Int, y: Int) = {
  84.     (this -(x, y)).magSq
  85.   }
  86.  
  87.   /**
  88.    * The squared distance between this point and the given coordinates
  89.    * @param p the coordinates to calculate the distance to
  90.    * @return the squared distance between this point and the given coordinates
  91.    *         (this - p).magSq
  92.    */
  93.   def distanceSq(p: (Int, Int)) = {
  94.     (this - p).magSq
  95.   }
  96.  
  97.   /**
  98.    * The squared distance between this point and the given coordinates
  99.    * @param p the coordinates to calculate the distance to
  100.    * @return the squared distance between this point and the given coordinates
  101.    *         (this - p).magSq
  102.    */
  103.   def distanceSq(p: Point) = {
  104.     (this - p).magSq
  105.   }
  106.  
  107.   override def hashCode(): Int = _hashCode
  108.  
  109.   override def equals(obj: scala.Any): Boolean = {
  110.     obj match {
  111.       case p: Point => return p.x == x && p.y == y
  112.       case (pX, pY) => return pX == x && pY == y
  113.     }
  114.     false
  115.   }
  116.  
  117.   override def toString = "( " + x + ", " + y + " )"
  118.  
  119.   override def compareTo(o: Point): Int = {
  120.     if (x != o.x) return if (x < o.x) 1 else -1
  121.     if (y != o.y) return if (y < o.y) 1 else -1
  122.     0
  123.   }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement