Skip to content

Instantly share code, notes, and snippets.

@aereal
Created December 8, 2010 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aereal/732826 to your computer and use it in GitHub Desktop.
Save aereal/732826 to your computer and use it in GitHub Desktop.
class MyVector(val x: Double, val y: Double) {
override def toString: String = {
"MyVector(" + x + ", " + y + ")"
}
def length: Double = {
Math.sqrt(x * x + y * y)
}
def direction: Double = {
(length * length + x * x - y * y) / 2 * x * length / x
}
def in_parallel_with(that: MyVector) = {
this.direction == that.direction
}
def unary_- : MyVector = {
new MyVector(-x, -y)
}
def equals(that: MyVector): Boolean = {
in_parallel_with(that) && this.length == that.length
}
def +(that: MyVector): MyVector = {
new MyVector(x + that.x, y + that.y)
}
def -(that: MyVector): MyVector = {
this + -that
}
def *(that: Double): MyVector = {
new MyVector(x * that, y * that)
}
}
object MyVector extends Application {
val vec = new MyVector(3, 5)
println(vec)
println(vec.length)
println(vec.direction)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment