Created
December 8, 2010 03:06
-
-
Save aereal/732826 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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