From 14a0678835cc92cc9ccb45fe89241de9b9632cb1 Mon Sep 17 00:00:00 2001 From: Marto Date: Thu, 20 Feb 2025 21:13:35 +0100 Subject: [PATCH] uni update --- src/main.zig | 49 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/src/main.zig b/src/main.zig index c1c8dba..dfdc55a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -16,19 +16,16 @@ pub fn main() !void { defer rl.closeWindow(); rl.setTargetFPS(60); - rl.beginDrawing(); - - rl.clearBackground(rl.Color.light_gray); - - plotGraph(); - drawAxis(); - try markAxis(allocator); - calculateArea(); - - rl.endDrawing(); while (!rl.windowShouldClose()) { rl.beginDrawing(); + + rl.clearBackground(rl.Color.white); + + plotGraph(); + drawAxis(); + try markAxis(allocator); + rl.endDrawing(); } } @@ -142,6 +139,34 @@ fn adjustForGridSystem(input_vector: rl.Vector2) rl.Vector2 { return new_vector; } -fn calculateArea() void { - rl.drawText("Test", SCREENWIDTH - 75, 25, 20, rl.Color.red); +fn calculateArea(val: f32) !void { + var buf: u8[20] = undefined; + + const txt = try std.fmt.bufPrintZ(&buf, "{}", .{val}); + rl.drawText(txt, SCREENWIDTH - 75, 25, 20, rl.Color.red); +} + +const SimpsonsError = error{ + InvalidParValue, +}; + +fn simpsonsRule(start: f32, end: f32, n: usize) !f32 { + if (start > end) return SimpsonsError.InvalidParValue; + + const h = (end - start) / @as(f32, @floatFromInt(n)); + + var sum: f32 = getY(start) + getY(end); + + var i: usize = 1; + + // we iterate through amount of subintervals + while (i < n) : (i += 1) { + // calculate x of the slice we're adding (start + ) + const x = start + i * h; + + // determine weights 4 for odd, 2 for even + sum += if (x % 2 == 0) 2 * getY(x) else 4 * getY(x); + } + + return (h / 3.0) * sum; }