summaryrefslogtreecommitdiff
path: root/src/estd/graphics/line.zig
blob: faf801a2640a63a7ae438756797baf41a58b92b4 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const std = @import("std");
const graphics = @import("root.zig");

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

	start: Point,
	end: Point,

	color: graphics.Color,

	fn distance(a: u32, b: u32) u32 {
		if (a < b) {
			return b - a;
		} else {
			return a - b;
		}
	}

	pub fn render(self: *const Self, canvas: *const graphics.Canvas) void {
		var start = self.start;
		var end = self.end;
		const steep = distance(start.y, end.y) > distance(start.x, end.x);

		if (steep) {
			std.mem.swap(u32, &start.x, &start.y);
			std.mem.swap(u32, &end.x, &end.y);
		}

		if (start.x > end.x) {
			std.mem.swap(Point, &start, &end);
		}

		const dx: f64 = @floatFromInt(end.x - start.x);
		const dy: f64 = @floatFromInt(@as(i64, @intCast(end.y)) - @as(i64, @intCast(start.y)));
		var gradient: f64 = dy / dx;
		if  (dx == 0) {
			gradient = 1;
		}

		var intersect_y: f64 = @floatFromInt(start.y);

		if (steep) {
			for (start.x..end.x + 1) |x| {
				const y: u64 = @intFromFloat(intersect_y);
				const factor = intersect_y - @as(f64, @floatFromInt(y));

				var previous_color = canvas.buffer[x * canvas.width + y];
				canvas.buffer[x * canvas.width + y] = self.color.mix(&previous_color, factor);

				previous_color = canvas.buffer[canvas.width * x + y + 1];
				canvas.buffer[x * canvas.width + y + 1] = previous_color.mix(&self.color, factor);

				intersect_y += gradient;
			}
		} else {
			for (start.x..end.x + 1) |x| {
				const y: u64 = @intFromFloat(intersect_y);
				const factor = intersect_y - @as(f64, @floatFromInt(y));

				var previous_color = canvas.buffer[x + canvas.width * y];
				canvas.buffer[x + canvas.width * y] = self.color.mix(&previous_color, factor);

				previous_color = canvas.buffer[x + canvas.width * (y + 1)];
				canvas.buffer[x + canvas.width * (y + 1)] = previous_color.mix(&self.color, factor);

				intersect_y += gradient;
			}
		}
	}
};