-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.zig
66 lines (53 loc) · 1.91 KB
/
main.zig
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
const std = @import("std");
const ppm = @import("ppm.zig");
const cli = @import("cli.zig");
const Point = struct { x: i16, y: i16 };
const Circle = struct {
center: Point,
radius: i16,
fn contains(self: Circle, point: Point) bool {
const x: i16 = point.x - self.center.x;
const y: i16 = point.y - self.center.y;
const result = std.math.pow(i16, x, 2) + std.math.pow(i16, y, 2);
return result <= std.math.pow(i16, self.radius, 2);
}
};
fn draw_image(width: usize, height: usize, circle: Circle, allocator: *std.mem.Allocator) ![][]u8 {
var matrix = try allocator.alloc([]u8, height);
for (matrix, 0..) |_, index| {
matrix[index] = try allocator.alloc(u8, width);
}
var curr_point: Point = undefined;
// Optimize cache access by iterating the rows (x) sequentially
for (0..height) |y| {
for (0..width) |x| {
curr_point = Point{ .x = @intCast(x), .y = @intCast(y) };
if (circle.contains(curr_point)) {
matrix[y][x] = '.';
} else {
matrix[y][x] = 'x';
}
}
}
return matrix;
}
pub const Image = struct {
width: usize,
height: usize,
circle: Circle,
data: [][]u8,
};
pub fn main() !void {
const filename_allocator = std.heap.page_allocator;
const arguments = try cli.parse_arguments(filename_allocator);
defer filename_allocator.free(arguments.filename);
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var allocator = gpa.allocator();
const width: usize = 64;
const height: usize = 48;
const circle = Circle{ .center = Point{ .x = 32, .y = 24 }, .radius = 5 };
var data: [][]u8 = try draw_image(width, height, circle, &allocator);
var image = Image{ .width = 64, .height = 48, .circle = circle, .data = data };
defer allocator.free(data);
try ppm.save(arguments.filename, arguments.ppm_type, image);
}