-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 2-dimension single-precision vector. | ||
type Vector2(x: float, y: float) #unboxed { | ||
def add(that: Vector2) -> Vector2 { | ||
return Vector2(this.x + that.x, this.y + that.y); | ||
} | ||
def sub(that: Vector2) -> Vector2 { | ||
return Vector2(this.x - that.x, this.y - that.y); | ||
} | ||
def scale(factor: float) -> Vector2 { | ||
return Vector2(this.x * factor, this.y * factor); | ||
} | ||
def inverse() -> Vector2 { | ||
return Vector2(0f - this.x, 0f - this.y); | ||
} | ||
def dot(that: Vector2) -> float { | ||
return this.x * that.x + this.y * that.y; | ||
} | ||
def unit() -> Vector2 { | ||
var len = length(); | ||
return if(len == 1f, this, scale(1f / len)); | ||
} | ||
def length() -> float { | ||
return float.sqrt(this.x * this.x + this.y * this.y); | ||
} | ||
def cross(that: Vector2) -> float { | ||
return this.x * that.y - this.y * that.x; | ||
} | ||
} | ||
|
||
// 3-dimension single-precision vector. | ||
type Vector3(x: float, y: float, z: float) #unboxed { | ||
def add(that: Vector3) -> Vector3 { | ||
return Vector3(this.x + that.x, this.y + that.y, this.z + that.z); | ||
} | ||
def sub(that: Vector3) -> Vector3 { | ||
return Vector3(this.x - that.x, this.y - that.y, this.z - that.z); | ||
} | ||
def scale(factor: float) -> Vector3 { | ||
return Vector3(this.x * factor, this.y * factor, this.z * factor); | ||
} | ||
def inverse() -> Vector3 { | ||
return Vector3(0f - this.x, 0f - this.y, 0f - this.z); | ||
} | ||
def dot(that: Vector3) -> float { | ||
return this.x * that.x + this.y * that.y + this.z * that.z; | ||
} | ||
def unit() -> Vector3 { | ||
var len = length(); | ||
return if(len == 1f, this, scale(1f / len)); | ||
} | ||
def length() -> float { | ||
return float.sqrt(this.x * this.x + this.y * this.y + this.z * this.z); | ||
} | ||
def cross(that: Vector3) -> Vector3 { | ||
return Vector3(this.y * that.z - this.z * that.y, | ||
0f - (this.x * that.z - this.z * that.x), | ||
this.x * that.y - this.y * that.x); | ||
} | ||
} |