-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPoint.scala
27 lines (25 loc) · 970 Bytes
/
Point.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package scalajsGames
import scala.scalajs.js
case class Point(x: Double, y: Double) {
def +(other: Point) = Point(x + other.x, y + other.y)
def -(other: Point) = Point(x - other.x, y - other.y)
def %(other: Point) = Point(x % other.x, y % other.y)
def <(other: Point) = x < other.x && y < other.y
def >(other: Point) = x > other.x && y > other.y
def /(value: Double) = Point(x / value, y / value)
def *(value: Double) = Point(x * value, y * value)
def *(other: Point) = x * other.x + y * other.y
def length = js.Math.sqrt(lengthSquared)
def lengthSquared = x * x + y * y
def within(a: Point, b: Point, extra: Point = Point(0, 0)) = {
import math.{max, min}
x >= min(a.x, b.x) - extra.x &&
x < max(a.x, b.x) + extra.y &&
y >= min(a.y, b.y) - extra.x &&
y < max(a.y, b.y) + extra.y
}
def rotate(theta: Double) = {
val (cos, sin) = (Math.cos(theta), math.sin(theta))
Point(cos * x - sin * y, sin * x + cos * y)
}
}