From dbba822c7ac26b783e564febb757a36131970480 Mon Sep 17 00:00:00 2001 From: kayomn Date: Sun, 12 Nov 2023 18:55:16 +0000 Subject: [PATCH] Add linear algebra utils for future graphics stuff --- source/ona/gfx.zig | 2 + source/ona/gfx/lina.zig | 181 +++++++++++++++++++++++++++++++++++++++ source/ona/kym/Chunk.zig | 3 - 3 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 source/ona/gfx.zig create mode 100644 source/ona/gfx/lina.zig diff --git a/source/ona/gfx.zig b/source/ona/gfx.zig new file mode 100644 index 0000000..7bc3c66 --- /dev/null +++ b/source/ona/gfx.zig @@ -0,0 +1,2 @@ + +pub const lina = @import("./gfx/lina.zig"); diff --git a/source/ona/gfx/lina.zig b/source/ona/gfx/lina.zig new file mode 100644 index 0000000..8022321 --- /dev/null +++ b/source/ona/gfx/lina.zig @@ -0,0 +1,181 @@ + +pub const Vector2 = struct { + x: f32, + y: f32, + + pub const Scalars = [2]f32; + + pub fn equals(self: Vector2, vector: Vector2) bool { + return self.x == vector.x and self.y == vector.y; + } + + pub fn from_scalar(scalar: f32) Vector2 { + return .{ + .x = scalar, + .y = scalar, + }; + } + + pub fn from_scalars(scalars: Scalars) Vector2 { + return .{ + .x = scalars[0], + .y = scalars[1], + }; + } + + pub fn scalar_added(self: Vector2, scalar: f32) Vector2 { + return .{ + .x = self.x + scalar, + .y = self.y + scalar, + }; + } + + pub fn scalar_divided(self: Vector2, scalar: f32) Vector2 { + return .{ + .x = self.x / scalar, + .y = self.y / scalar, + }; + } + + pub fn scalar_multiplied(self: Vector2, scalar: f32) Vector2 { + return .{ + .x = self.x * scalar, + .y = self.y * scalar, + }; + } + + pub fn to_scalars(self: Vector2) Scalars { + return .{self.x, self.y}; + } + + pub fn scalar_subtracted(self: Vector2, scalar: f32) Vector2 { + return .{ + .x = self.x - scalar, + .y = self.y - scalar, + }; + } + + pub fn vector_added(self: Vector2, vector: Vector2) Vector2 { + return .{ + .x = self.x + vector.x, + .y = self.y + vector.y, + }; + } + + pub fn vector_divided(self: Vector2, vector: Vector2) Vector2 { + return .{ + .x = self.x / vector.x, + .y = self.y / vector.y, + }; + } + + pub fn vector_multiplied(self: Vector2, vector: Vector2) Vector2 { + return .{ + .x = self.x * vector.x, + .y = self.y * vector.y, + }; + } + + pub fn vector_subtracted(self: Vector2, vector: Vector2) Vector2 { + return .{ + .x = self.x - vector.x, + .y = self.y - vector.y, + }; + } +}; + +pub const Vector3 = struct { + x: f32, + y: f32, + z: f32, + + pub const Scalars = [3]f32; + + pub fn equals(self: Vector3, vector: Vector3) bool { + return self.x == vector.x and self.y == vector.y and self.z == vector.z; + } + + pub fn from_scalar(scalar: f32) Vector3 { + return .{ + .x = scalar, + .y = scalar, + .z = scalar, + }; + } + + pub fn from_scalars(scalars: Scalars) Vector3 { + return .{ + .x = scalars[0], + .y = scalars[1], + .z = scalars[2], + }; + } + + pub fn scalar_added(self: Vector3, scalar: f32) Vector3 { + return .{ + .x = self.x + scalar, + .y = self.y + scalar, + .z = self.z + scalar, + }; + } + + pub fn scalar_divided(self: Vector3, scalar: f32) Vector3 { + return .{ + .x = self.x / scalar, + .y = self.y / scalar, + .z = self.z / scalar, + }; + } + + pub fn scalar_multiplied(self: Vector3, scalar: f32) Vector3 { + return .{ + .x = self.x * scalar, + .y = self.y * scalar, + .z = self.z * scalar, + }; + } + + pub fn scalar_subtracted(self: Vector3, scalar: f32) Vector3 { + return .{ + .x = self.x - scalar, + .y = self.y - scalar, + .z = self.z - scalar, + }; + } + + pub fn to_scalars(self: Vector3) Scalars { + return .{self.x, self.y, self.z}; + } + + pub fn vector_added(self: Vector3, other: Vector3) Vector3 { + return .{ + .x = self.x + other.x, + .y = self.y + other.y, + .z = self.z + other.z, + }; + } + + pub fn vector_divided(self: Vector3, other: Vector3) Vector3 { + return .{ + .x = self.x / other.x, + .y = self.y / other.y, + .z = self.z / other.z, + }; + } + + pub fn vector_multiplied(self: Vector3, other: Vector3) Vector3 { + return .{ + .x = self.x * other.x, + .y = self.y * other.y, + .z = self.z * other.z, + }; + } + + pub fn vector_subtracted(self: Vector3, other: Vector3) Vector3 { + return .{ + .x = self.x - other.x, + .y = self.y - other.y, + .z = self.z - other.z, + }; + } +}; diff --git a/source/ona/kym/Chunk.zig b/source/ona/kym/Chunk.zig index 7ff4688..8bdf4de 100644 --- a/source/ona/kym/Chunk.zig +++ b/source/ona/kym/Chunk.zig @@ -914,9 +914,6 @@ pub fn init(env: *kym.RuntimeEnv, name: []const coral.io.Byte, environment: *con try compiler.compile_environment(environment); - @import("../app.zig").log_info(""); - @import("../app.zig").log_info((try (try env.push(try chunk.dump(env))).to_string()).pop().?.is_string().?); - return chunk; }