summaryrefslogtreecommitdiff
path: root/src/estd/graphics/curve.zig
blob: d4be1d73e5c8cb43eeb3a425b13f76e1d1a60818 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const std = @import("std");
const graphics = @import("root.zig");

pub const Curve = struct {
	const Self = @This();
	const Point = struct {
		x: u32,
		y: u32,
	};

	start: Point,
	middle: Point,
	end: Point,

	color: graphics.Color,

	pub fn render(self: *const Self, canvas: *const graphics.Canvas) void {
		const start_x: f64 = @floatFromInt(self.start.x);
		const start_y: f64 = @floatFromInt(self.start.y);
		const middle_x: f64 = @floatFromInt(self.middle.x);
		const middle_y: f64 = @floatFromInt(self.middle.y);
		const end_x: f64 = @floatFromInt(self.end.x);
		const end_y: f64 = @floatFromInt(self.end.y);

		for (0..100) |index| {
			const t: f64 = @as(f64, @floatFromInt(index)) / 100;
			const x: u32 = @intFromFloat(@round(
				(1 - t) * ((1 - t) * start_x + t * middle_x) + t * ((1 - t) * middle_x + t * end_x)
			));
			const y: u32 = @intFromFloat(@round(
				(1 - t) * ((1 - t) * start_y + t * middle_y) + t * ((1 - t) * middle_y + t * end_y)
			));
			canvas.buffer[x + canvas.width * y] = self.color;
		}
	}
};