From 67a7449dd0deeb2aa4c7e138c4a16e3c91139ed8 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Fri, 11 Jun 2021 13:52:04 -0500 Subject: [PATCH] added vecmath library (VERY MINIMAL) --- modules/vecmath/module.onyx | 3 ++ modules/vecmath/vector2.onyx | 53 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 modules/vecmath/module.onyx create mode 100644 modules/vecmath/vector2.onyx diff --git a/modules/vecmath/module.onyx b/modules/vecmath/module.onyx new file mode 100644 index 00000000..aef3a27a --- /dev/null +++ b/modules/vecmath/module.onyx @@ -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 index 00000000..1363907a --- /dev/null +++ b/modules/vecmath/vector2.onyx @@ -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, ")"); +} -- 2.25.1