-
Notifications
You must be signed in to change notification settings - Fork 1
/
014_stroke_lines.zig
77 lines (61 loc) · 2.9 KB
/
014_stroke_lines.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
67
68
69
70
71
72
73
74
75
76
77
// SPDX-License-Identifier: 0BSD
// Copyright © 2024 Chris Marchesi
//! Case: Renders multiple lines with the default thickness in different
//! directions, unclosed.
const mem = @import("std").mem;
const z2d = @import("z2d");
pub const filename = "014_stroke_lines";
pub fn render(alloc: mem.Allocator, aa_mode: z2d.options.AntiAliasMode) !z2d.Surface {
const width = 800;
const height = 400;
var sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);
var context = try z2d.Context.init(alloc, &sfc);
defer context.deinit();
context.setSource(.{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } });
context.setAntiAliasingMode(aa_mode);
// sub-canvas dimensions
const sub_canvas_width = width / 4;
const sub_canvas_height = height / 2;
// Down and to the right
const margin = 10;
comptime var x_offset = 0;
comptime var y_offset = 0;
try context.moveTo(x_offset + margin, y_offset + margin);
try context.lineTo(x_offset + sub_canvas_width - margin - 1, y_offset + sub_canvas_height - margin - 1);
// Up and to the right
x_offset = sub_canvas_width;
try context.moveTo(x_offset + margin, y_offset + sub_canvas_height - margin - 1);
try context.lineTo(x_offset + sub_canvas_width - margin - 1, y_offset + margin);
// Down and to the left
x_offset = 0;
y_offset = sub_canvas_height;
try context.moveTo(x_offset + sub_canvas_width - margin - 1, y_offset + margin);
try context.lineTo(x_offset + margin, y_offset + sub_canvas_height - margin - 1);
// Up and to the left
x_offset = sub_canvas_width;
y_offset = sub_canvas_height;
try context.moveTo(x_offset + sub_canvas_width - margin - 1, y_offset + sub_canvas_height - margin - 1);
try context.lineTo(x_offset + margin, y_offset + margin);
// Horizontal (left -> right)
x_offset = sub_canvas_width * 2;
y_offset = 0;
try context.moveTo(x_offset + margin, y_offset + sub_canvas_height / 2);
try context.lineTo(x_offset + sub_canvas_width - margin - 1, y_offset + sub_canvas_height / 2);
// Vertical (up -> down)
x_offset = sub_canvas_width * 2;
y_offset = sub_canvas_height;
try context.moveTo(x_offset + sub_canvas_width / 2, y_offset + margin);
try context.lineTo(x_offset + sub_canvas_width / 2, y_offset + sub_canvas_height - margin - 1);
// Vertical (down -> up)
x_offset = sub_canvas_width * 3;
y_offset = 0;
try context.moveTo(x_offset + sub_canvas_width / 2, y_offset + sub_canvas_height - margin - 1);
try context.lineTo(x_offset + sub_canvas_width / 2, y_offset + margin);
// Horizontal (right -> left)
x_offset = sub_canvas_width * 3;
y_offset = sub_canvas_height;
try context.moveTo(x_offset + sub_canvas_width - margin - 1, y_offset + sub_canvas_height / 2);
try context.lineTo(x_offset + margin, y_offset + sub_canvas_height / 2);
try context.stroke();
return sfc;
}