PR3L-Uebung-VectorSelf/vector.zig

74 lines
1.8 KiB
Zig

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});
}