1. object Vec {
  2.   val X_AXIS = Vec(1,0)
  3.   val Y_AXIS = Vec(0,1)
  4. }
  5.  
  6. case class Vec(x: Double, y: Double) {
  7.   def dotProduct(v: Vec) = x*v.x+y*v.y
  8.   def crossProduct(v: Vec) = x*v.y-y*v.x;
  9.   def length = math.sqrt(x*x+y*y)
  10.   def rotate(theta: Double) = {
  11.       val sin = math.sin(theta);
  12.       val cos = math.cos(theta);
  13.       Vec(x*cos-y*sin,
  14.           x*sin+y*cos);
  15.   }
  16.   def rotate90 = Vec(-y, x)
  17.   def +(v: Vec) = Vec(x+v.x, y+v.y)
  18.   def -(v: Vec) = Vec(x-v.x, y-v.y)
  19.   def *(d: Double) = Vec(x*d, y*d)
  20.   def /(d: Double) = Vec(x/d, y/d)
  21.   def unary_- = Vec(-x, -y)
  22.   override def toString = f"Vec($x%.3f, $y%.3f)"
  23. }
  24.  
  25. object Matrix {
  26.   def rotationMatrix(theta: Double) = {
  27.     val v = Vec.X_AXIS.rotate(theta)
  28.     Matrix(v, v.rotate90)
  29.   }
  30.   def apply(a: Double, b: Double,
  31.             c: Double, d: Double): Matrix = Matrix(Vec(a,c), Vec(b,d))
  32. }
  33.  
  34. case class Matrix(c1: Vec, c2: Vec) {
  35.   def *(v: Vec) = c1*v.x + c2*v.y
  36.   def *(d: Double) = Matrix(c1*d, c2*d)
  37.   def determinant = c1.crossProduct(c2)
  38.   def inverse = Matrix( c2.y, -c2.x,
  39.                        -c1.y,  c1.x) * (1/determinant)
  40.   override def toString = f"Matrix(${c1.x}%.3f, ${c2.x}%.3f\n" +
  41.                           f"       ${c1.y}%.3f, ${c2.y}%.3f)"
  42. }
  43.  
  44. case class CoordinateSystem(origin: Vec, xAxis: Vec, yAxis: Vec) {
  45.   val matrix = Matrix(xAxis, yAxis)
  46.   val inverseMatrix = matrix.inverse
  47.   def translate(v: Vec) = matrix*v + origin
  48.   def translateBack(v: Vec) = inverseMatrix*(v-origin)
  49. }
  50.  
  51. val cs = CoordinateSystem(Vec(500,100),
  52.                           Vec(1,1),
  53.                           Vec(-1,1))
  54. val v = Vec(2,3)
  55. val v2 = cs.translate(v)
  56. println(v2)
  57. println(cs.translateBack(v2))