This repository has been archived by the owner on Apr 5, 2024. It is now read-only.
forked from ocen-lang/ocen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec.oc
69 lines (54 loc) · 1.85 KB
/
vec.oc
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//* Mathematical vector library
import std::math
//* A type representing a 3D vector
struct Vec {
x: f32
y: f32
z: f32
}
def Vec::print(this) {
println("%f %f %f\n", .x, .y, .z)
}
def Vec::add(this, other: Vec): Vec => Vec(.x + other.x, .y + other.y, .z + other.z)
def Vec::addf(this, val: f32): Vec => Vec(.x + val, .y + val, .z + val)
def Vec::sub(this, other: Vec): Vec => Vec(.x - other.x, .y - other.y, .z - other.z)
def Vec::subf(this, val: f32): Vec => Vec(.x - val, .y - val, .z - val)
def Vec::mult(this, other: Vec): Vec => Vec(.x * other.x, .y * other.y, .z * other.z)
def Vec::multf(this, val: f32): Vec => Vec(.x * val, .y * val, .z * val)
def Vec::div(this, other: Vec): Vec => Vec(.x / other.x, .y / other.y, .z / other.z)
def Vec::divf(this, val: f32): Vec => Vec(.x / val, .y / val, .z / val)
def Vec::dot(this, other: Vec): f32 => .x * other.x + .y * other.y + .z * other.z
def Vec::cross(this, other: Vec): Vec {
return Vec(
.y * other.z - .z * other.y,
.z * other.x - .x * other.z,
.x * other.y - .y * other.x,
)
}
def Vec::length(this): f32 => (.x * .x + .y * .y + .z * .z).sqrt()
def Vec::length_sq(this): f32 => .x * .x + .y * .y + .z * .z
def Vec::normalized(this): Vec => .divf(.length())
def Vec::reflect(this, normal: Vec): Vec {
return .sub(normal.multf(2.0 * .dot(normal)));
}
def Vec::rotateX(&this, angle: f32): Vec {
let c = angle.cos()
let s = angle.sin()
let y = .y * c - .z * s
let z = .y * s + .z * c
return Vec(.x, y, z)
}
def Vec::rotateY(&this, angle: f32): Vec {
let c = angle.cos()
let s = angle.sin()
let z = .z * c - .x * s
let x = .z * s + .x * c
return Vec(x, .y, z)
}
def Vec::rotateZ(&this, angle: f32): Vec {
let c = angle.cos()
let s = angle.sin()
let x = .x * c - .y * s
let y = .x * s + .y * c
return Vec(x, y, .z)
}