added vecmath library (VERY MINIMAL)
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 11 Jun 2021 18:52:04 +0000 (13:52 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 11 Jun 2021 18:52:04 +0000 (13:52 -0500)
modules/vecmath/module.onyx [new file with mode: 0644]
modules/vecmath/vector2.onyx [new file with mode: 0644]

diff --git a/modules/vecmath/module.onyx b/modules/vecmath/module.onyx
new file mode 100644 (file)
index 0000000..aef3a27
--- /dev/null
@@ -0,0 +1,3 @@
+package vecmath
+
+#load "./vector2.onyx"
diff --git a/modules/vecmath/vector2.onyx b/modules/vecmath/vector2.onyx
new file mode 100644 (file)
index 0000000..1363907
--- /dev/null
@@ -0,0 +1,53 @@
+package vecmath
+
+#private_file io :: package core.io
+use package core.intrinsics.onyx { __zero_value }
+
+Vector2i :: #type Vector2(i32);
+Vector2f :: #type Vector2(f32);
+
+Vector2 :: struct (T: type_expr) {
+    x := __zero_value(T);
+    y := __zero_value(T);
+}
+
+#operator + vector2_add
+vector2_add :: (a: Vector2($T), b: Vector2(T)) -> Vector2(T) {
+    return .{ a.x + b.x, a.y + b.y };
+}
+
+#operator - vector2_sub
+vector2_sub :: (a: Vector2($T), b: Vector2(T)) -> Vector2(T) {
+    return .{ a.x - b.x, a.y - b.y };
+}
+
+#operator * vector2_mul
+vector2_mul :: (v: Vector2($T), scalar: T) -> Vector2(T) {
+    return .{ v.x * scalar, v.y * scalar };
+}
+
+#operator * vector2_dot
+vector2_dot :: (a: Vector2($T), b: Vector2(T)) -> T {
+    return a.x * b.x + a.y * b.y;
+}
+
+vector2_lerp :: (t: f32, start: Vector2($T), end: Vector(T)) -> Vector2(T) {
+    return .{
+        ~~(t * ~~(end.x - start.x)) + start.x,
+        ~~(t * ~~(end.y - start.y)) + start.y,
+    };
+}
+
+#operator == vector2_equal
+vector2_equal :: (a: Vector2($T), b: Vector2(T)) -> bool {
+    return a.x == b.x && a.y == b.y;
+}
+
+#add_overload io.write, vector2_write
+vector2_write :: (writer: ^io.Writer, v: Vector2($T)) {
+    io.write(writer, "Vector2(");
+    io.write(writer, v.x);
+    io.write(writer, ", ");
+    io.write(writer, v.y);
+    io.write(writer, ")");
+}