diff --git a/main.zig b/main.zig deleted file mode 100644 index e3884cd..0000000 --- a/main.zig +++ /dev/null @@ -1,37 +0,0 @@ -const std = @import("std"); -const writer = std.io.getStdOut().writer(); -const expect = std.testing.expect; - -fn Vec3(comptime T: type) type { - return struct { - const Self = @This(); - x: T, - y: T, - z: T, - fn init(this: *Self, x: T, y: T, z: T) void { - this.x = x; - this.y = y; - this.z = z; - } - - fn print(this: Self) !void { - try writer.print("X: {d}\nY: {d}\nZ: {d}\n", .{this.x, this.y, this.z}); - } - - fn add(this: *Self, other: Self) void { - this.x = this.x + other.x; - this.y = this.y + other.y; - this.z = this.z + other.z; - } - }; -} - -pub fn main() !void { - var vec1: Vec3(u64) = undefined; - var vec2: Vec3(u64) = undefined; - vec1.init(2, 4, 3); - vec2.init(1, 1, 1); - try vec1.print(); - vec1.add(vec2); - try vec1.print(); -} diff --git a/tests.zig b/tests.zig new file mode 100644 index 0000000..e3328e8 --- /dev/null +++ b/tests.zig @@ -0,0 +1,73 @@ +const std = @import("std"); +const expect = std.testing.expect; + +const Vec3 = @import("vector.zig").Vec3(u64); + + +test "Addition" { + var vec1: Vec3 = undefined; + var vec2: Vec3 = undefined; + + vec1.init(2, 4, 3); + vec2.init(20, 43, 12); + + vec1.add(vec2); + + try expect(vec1.x == 22); + try expect(vec1.y == 47); + try expect(vec1.z == 15); + + var vecResult: Vec3 = undefined; + vecResult.init(22, 47, 15); + + try expect(vec1.comp(vecResult)); +} + +test "Subtraction" { + var vec1: Vec3 = undefined; + var vec2: Vec3 = undefined; + + vec1.init(20, 43, 12); + vec2.init(10, 23, 2); + + vec1.sub(vec2); + + try expect(vec1.x == 10); + try expect(vec1.y == 20); + try expect(vec1.z == 10); + + var vecResult: Vec3 = undefined; + vecResult.init(10, 20, 10); + + try expect(vec1.comp(vecResult)); +} + +test "Scalar multiplication" { + var vec1: Vec3 = undefined; + + vec1.init(20, 43, 12); + const scalar: u64 = 5; + + vec1.mul(scalar); + + try expect(vec1.x == 100); + try expect(vec1.y == 215); + try expect(vec1.z == 60); + + var vecResult: Vec3 = undefined; + vecResult.init(100, 215, 60); + + try expect(vec1.comp(vecResult)); +} + +test "Scalar product" { + var vec1: Vec3 = undefined; + var vec2: Vec3 = undefined; + + vec1.init(7, 16, 12); + vec2.init(10, 4, 0); + + const result: u64 = vec1.prod(vec2); + + try expect(result == 134); +} diff --git a/vector.zig b/vector.zig new file mode 100644 index 0000000..2ca857f --- /dev/null +++ b/vector.zig @@ -0,0 +1,73 @@ +const std = @import("std"); +const expect = std.testing.expect; + +pub fn Vec3(comptime T: type) type { + return struct { + const Self = @This(); + x: T, + y: T, + z: T, + pub fn init(this: *Self, x: T, y: T, z: T) void { + this.x = x; + this.y = y; + this.z = z; + } + + pub fn print(this: Self) !void { + const writer = std.io.getStdOut().writer(); + try writer.print("({d}, {d}, {d})", .{this.x, this.y, this.x}); + } + + pub fn add(this: *Self, other: Self) void { + this.x = this.x + other.x; + this.y = this.y + other.y; + this.z = this.z + other.z; + } + + pub fn sub(this: *Self, other: Self) void { + this.x = this.x - other.x; + this.y = this.y - other.y; + this.z = this.z - other.z; + } + + pub fn mul(this: *Self, scal: T) void { + this.x = this.x * scal; + this.y = this.y * scal; + this.z = this.z * scal; + } + + pub fn prod(this: Self, other: Self) T { + return (this.x * other.x) + (this.y * other.y) + (this.z * other.z); + } + + pub fn comp(this: Self, other: Self) bool { + return + (this.x == other.x) and + (this.y == other.y) and + (this.z == other.z); + } + }; +} + +pub fn main() !void { + const writer = std.io.getStdOut().writer(); + const myType = Vec3(64); + const vec3: myType = undefined; + + vec3.init(1, 1, 1); + + var vec1: Vec3(u64) = undefined; + var vec2: Vec3(u64) = undefined; + vec1.init(2, 4, 3); + + vec2.init(1, 1, 1); + + try vec1.print(); + try writer.print(" + ", .{}); + try vec2.print(); + vec1.add(vec2); + try writer.print(" = ", .{}); + try vec1.print(); + + try writer.print("vec1.x: {d} \n", .{vec1.x}); +}