PR3L-Uebung-VectorSelf/main.zig

38 lines
915 B
Zig
Raw Normal View History

2024-11-05 16:14:43 +01:00
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 {
2024-11-06 14:41:06 +01:00
try writer.print("X: {d}\nY: {d}\nZ: {d}\n", .{this.x, this.y, this.z});
2024-11-05 16:14:43 +01:00
}
2024-11-06 14:41:06 +01:00
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;
}
2024-11-05 16:14:43 +01:00
};
}
pub fn main() !void {
var vec1: Vec3(u64) = undefined;
2024-11-06 14:41:06 +01:00
var vec2: Vec3(u64) = undefined;
2024-11-05 16:14:43 +01:00
vec1.init(2, 4, 3);
2024-11-06 14:41:06 +01:00
vec2.init(1, 1, 1);
try vec1.print();
vec1.add(vec2);
try vec1.print();
2024-11-05 16:14:43 +01:00
}