From 71e22ebaae28d32b46cd8b6abfdfd81737b3ca91 Mon Sep 17 00:00:00 2001 From: Marto Date: Thu, 13 Feb 2025 17:17:27 +0100 Subject: [PATCH] rename & scale implementation --- build.zig | 10 +++--- build.zig.zon | 2 +- src/main.zig | 89 +++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 82 insertions(+), 19 deletions(-) diff --git a/build.zig b/build.zig index c22fcb0..0982e08 100644 --- a/build.zig +++ b/build.zig @@ -4,7 +4,7 @@ const rlz = @import("raylib-zig"); pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - + const raylib_dep = b.dependency("raylib-zig", .{ .target = target, .optimize = optimize, @@ -15,7 +15,7 @@ pub fn build(b: *std.Build) !void { //web exports are completely separate if (target.query.os_tag == .emscripten) { - const exe_lib = try rlz.emcc.compileForEmscripten(b, "integral-display-calculation", "src/main.zig", target, optimize); + const exe_lib = try rlz.emcc.compileForEmscripten(b, "koordinatni-sistem", "src/main.zig", target, optimize); exe_lib.linkLibrary(raylib_artifact); exe_lib.root_module.addImport("raylib", raylib); @@ -29,18 +29,18 @@ pub fn build(b: *std.Build) !void { b.getInstallStep().dependOn(&link_step.step); const run_step = try rlz.emcc.emscriptenRunStep(b); run_step.step.dependOn(&link_step.step); - const run_option = b.step("run", "Run integral-display-calculation"); + const run_option = b.step("run", "Run koordinatni-sistem"); run_option.dependOn(&run_step.step); return; } - const exe = b.addExecutable(.{ .name = "integral-display-calculation", .root_source_file = b.path("src/main.zig"), .optimize = optimize, .target = target }); + const exe = b.addExecutable(.{ .name = "koordinatni-sistem", .root_source_file = b.path("src/main.zig"), .optimize = optimize, .target = target }); exe.linkLibrary(raylib_artifact); exe.root_module.addImport("raylib", raylib); const run_cmd = b.addRunArtifact(exe); - const run_step = b.step("run", "Run integral-display-calculation"); + const run_step = b.step("run", "Run koordinatni-sistem"); run_step.dependOn(&run_cmd.step); b.installArtifact(exe); diff --git a/build.zig.zon b/build.zig.zon index de8f467..df9edac 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,5 +1,5 @@ .{ - .name = "integral-display-calculation", + .name = "koordinatni-sistem", .version = "0.0.1", .dependencies = .{ .@"raylib-zig" = .{ diff --git a/src/main.zig b/src/main.zig index 4d49e31..686f6cf 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4,22 +4,34 @@ const math = @import("math.zig"); const SCREENWIDTH = 1920; const SCREENHEIGHT = 1080; +const SCALE = 50; pub fn main() !void { + // std.debug.print("{d}\n", .{SCREENWIDTH / 2}); + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const allocator = gpa.allocator(); + rl.setConfigFlags(.{ .msaa_4x_hint = true }); rl.initWindow(SCREENWIDTH, SCREENHEIGHT, "Koordinatni sistem"); defer rl.closeWindow(); rl.setTargetFPS(60); + // todo move away from being a loop, a static trigonometry should not be redrawn every frame + rl.beginDrawing(); + + rl.clearBackground(rl.Color.white); + + try markAxis(allocator); + plotGraph(); + drawAxis(); + + rl.endDrawing(); + while (!rl.windowShouldClose()) { rl.beginDrawing(); - defer rl.endDrawing(); - - rl.clearBackground(rl.Color.white); - drawAxis(); - // markAxis(); - plotGraph(0.01); + rl.endDrawing(); } } @@ -32,9 +44,50 @@ fn drawAxis() void { rl.drawLine(endx_on_ordinat, 0, endx_on_ordinat, SCREENHEIGHT, rl.Color.black); } +fn markAxis(allocator: std.mem.Allocator) !void { + const diff = 10; + const font_size = 20; + const start_x = SCREENWIDTH / 2; + const start_y = SCREENHEIGHT / 2; + const radius = SCALE / 15; + var buf: [20]u8 = undefined; + + var cnt: usize = 0; + var i: i32 = start_x; + while (i <= SCREENWIDTH) : (i += SCALE) { + // get + const text = try std.fmt.bufPrintZ(&buf, "{}", .{cnt}); + cnt += 1; + // 0 is due to us working with C-style strings + var text_back = try allocator.allocSentinel(u8, text.len + 1, 0); + defer allocator.free(text_back); + @memcpy(text_back[0..1], "-"); + @memcpy(text_back[1..], text); + + // +x + rl.drawCircle(@intCast(i), start_y, radius, rl.Color.black); + rl.drawText(text, @as(i32, @intCast(i)) - diff / 2, start_y + diff, font_size, rl.Color.black); + + if (i == start_x) continue; // prevent 0 from being written/drawn twice + + // -x + rl.drawCircle(SCREENWIDTH - i, start_y, radius, rl.Color.black); + rl.drawText(text_back, SCREENWIDTH - i - diff - diff / 2, start_y + diff, font_size, rl.Color.black); + } + + i = start_y; + while (i <= SCREENHEIGHT) : (i += SCALE) { + // +y + rl.drawCircle(start_x, @intCast(i), radius, rl.Color.black); + + // -y + rl.drawCircle(start_x, SCREENHEIGHT - i, radius, 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 +fn plotGraph() void { + const diff: f32 = 0.01; const thickness = 2.5; var x_value: f32 = -SCREENWIDTH / 2; @@ -49,20 +102,30 @@ fn plotGraph(diff: f32) void { .y = getY(x_value + diff), }); + // test + if (cur_pos.x == 0 or next_pos.x == 0) { + continue; + } + rl.drawLineEx(cur_pos, next_pos, thickness, rl.Color.dark_blue); } } +// f(x) fn getY(x: f32) f32 { - const a = math.abs(x) - 2; - const b = 1; - return @divTrunc(a, b); + const min_threshold = 1e-3; + const a = x / 10; + var b: f32 = 1.0; + if (b == 0) b = a / min_threshold; + + return a / b; } fn adjustForGridSystem(input_vector: rl.Vector2) rl.Vector2 { + // std.debug.print("{d} => {d}\n", .{ input_vector.x, input_vector.x * SCALE }); const new_vector = rl.Vector2{ - .x = input_vector.x + SCREENWIDTH / 2, - .y = -input_vector.y + SCREENHEIGHT / 2, + .x = input_vector.x * SCALE + SCREENWIDTH / 2, + .y = -input_vector.y * SCALE + SCREENHEIGHT / 2, // minus is there because y works the opposite in gui libraries than in coordination system }; return new_vector;