initial commit
This commit is contained in:
commit
2109bb5bf8
47
build.zig
Normal file
47
build.zig
Normal file
@ -0,0 +1,47 @@
|
||||
const std = @import("std");
|
||||
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,
|
||||
});
|
||||
|
||||
const raylib = raylib_dep.module("raylib");
|
||||
const raylib_artifact = raylib_dep.artifact("raylib");
|
||||
|
||||
//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);
|
||||
|
||||
exe_lib.linkLibrary(raylib_artifact);
|
||||
exe_lib.root_module.addImport("raylib", raylib);
|
||||
|
||||
// Note that raylib itself is not actually added to the exe_lib output file, so it also needs to be linked with emscripten.
|
||||
const link_step = try rlz.emcc.linkWithEmscripten(b, &[_]*std.Build.Step.Compile{ exe_lib, raylib_artifact });
|
||||
//this lets your program access files like "resources/my-image.png":
|
||||
link_step.addArg("--embed-file");
|
||||
link_step.addArg("resources/");
|
||||
|
||||
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");
|
||||
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 });
|
||||
|
||||
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");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
|
||||
b.installArtifact(exe);
|
||||
}
|
11
build.zig.zon
Normal file
11
build.zig.zon
Normal file
@ -0,0 +1,11 @@
|
||||
.{
|
||||
.name = "integral-display-calculation",
|
||||
.version = "0.0.1",
|
||||
.dependencies = .{
|
||||
.@"raylib-zig" = .{
|
||||
.url = "git+https://github.com/Not-Nik/raylib-zig?ref=devel#5004bb2316fc34a416644f3910c8c79dd08d7c63",
|
||||
.hash = "1220223e9881fc9510eeb30fc2ee73fb49ee645bf1859cc46a92b649d35ecfd3351a",
|
||||
},
|
||||
},
|
||||
.paths = .{""},
|
||||
}
|
69
src/main.zig
Normal file
69
src/main.zig
Normal 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
27
src/math.zig
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user