initial commit

This commit is contained in:
2025-02-12 20:55:26 +01:00
commit 2109bb5bf8
4 changed files with 154 additions and 0 deletions

69
src/main.zig Normal file
View File

@@ -0,0 +1,69 @@
const rl = @import("raylib");
const std = @import("std");
const math = @import("math.zig");
const SCREENWIDTH = 1920;
const SCREENHEIGHT = 1080;
pub fn main() !void {
rl.setConfigFlags(.{ .msaa_4x_hint = true });
rl.initWindow(SCREENWIDTH, SCREENHEIGHT, "Koordinatni sistem");
defer rl.closeWindow();
rl.setTargetFPS(60);
while (!rl.windowShouldClose()) {
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(rl.Color.white);
drawAxis();
// markAxis();
plotGraph(0.01);
}
}
fn drawAxis() void {
const endy_on_abscis: i32 = @divTrunc(SCREENHEIGHT, 2);
const endx_on_ordinat: i32 = @divTrunc(SCREENWIDTH, 2);
// x
rl.drawLine(0, endy_on_abscis, SCREENWIDTH, endy_on_abscis, rl.Color.black);
// y
rl.drawLine(endx_on_ordinat, 0, endx_on_ordinat, SCREENHEIGHT, rl.Color.black);
}
// todo in the future we will do calculations only once and just repeat displaying
fn plotGraph(diff: f32) void {
// diff is currently 0.01
const thickness = 2.5;
var x_value: f32 = -SCREENWIDTH / 2;
while (x_value <= SCREENWIDTH) : (x_value += diff) {
const cur_pos = adjustForGridSystem(rl.Vector2{
.x = x_value,
.y = getY(x_value),
});
const next_pos = adjustForGridSystem(rl.Vector2{
.x = x_value + diff,
.y = getY(x_value + diff),
});
rl.drawLineEx(cur_pos, next_pos, thickness, rl.Color.dark_blue);
}
}
fn getY(x: f32) f32 {
const a = math.abs(x) - 2;
const b = 1;
return @divTrunc(a, b);
}
fn adjustForGridSystem(input_vector: rl.Vector2) rl.Vector2 {
const new_vector = rl.Vector2{
.x = input_vector.x + SCREENWIDTH / 2,
.y = -input_vector.y + SCREENHEIGHT / 2,
};
return new_vector;
}

27
src/math.zig Normal file
View File

@@ -0,0 +1,27 @@
pub fn abs(a: f32) f32 {
if (a < 0) {
return -a;
}
return a;
}
pub fn pow(a: f32, b: i32) f32 {
var res: f32 = 1.0;
var n: usize = undefined;
var f: f32 = undefined;
if (b < 0) {
f = 1 / a;
n = @intCast(-b);
} else {
f = a;
n = @intCast(b);
}
for (0..n) |_| {
res *= f;
}
return res;
}