-
Notifications
You must be signed in to change notification settings - Fork 0
/
020_stroke_lines_round_caps.zig
134 lines (109 loc) · 5.43 KB
/
020_stroke_lines_round_caps.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// SPDX-License-Identifier: 0BSD
// Copyright © 2024 Chris Marchesi
//! Case: Renders multiple lines, round-capped, in various directions.
const debug = @import("std").debug;
const mem = @import("std").mem;
const z2d = @import("z2d");
pub const filename = "020_stroke_lines_round_caps";
pub fn render(alloc: mem.Allocator, aa_mode: z2d.options.AntiAliasMode) !z2d.Surface {
const width = 800;
const height = 600;
const sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);
var context: z2d.Context = .{
.surface = sfc,
.pattern = .{
.opaque_pattern = .{
.pixel = .{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } }, // White on black
},
},
.line_cap_mode = .round,
.line_join_mode = .round,
.line_width = 20,
.anti_aliasing_mode = aa_mode,
};
var path = z2d.Path.init(alloc);
defer path.deinit();
// sub-canvas dimensions
const sub_canvas_width = width / 4;
var sub_canvas_height: f64 = height / 3;
// Down and to the right
const margin = 30;
var x_offset: f64 = 0;
var y_offset: f64 = 0;
try path.moveTo(x_offset + margin, y_offset + margin);
try path.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 path.moveTo(x_offset + margin, y_offset + sub_canvas_height - margin - 1);
try path.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 path.moveTo(x_offset + sub_canvas_width - margin - 1, y_offset + margin);
try path.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 path.moveTo(x_offset + sub_canvas_width - margin - 1, y_offset + sub_canvas_height - margin - 1);
try path.lineTo(x_offset + margin, y_offset + margin);
// Horizontal (left -> right)
x_offset = sub_canvas_width * 2;
y_offset = 0;
try path.moveTo(x_offset + margin, y_offset + sub_canvas_height / 2);
try path.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 path.moveTo(x_offset + sub_canvas_width / 2, y_offset + margin);
try path.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 path.moveTo(x_offset + sub_canvas_width / 2, y_offset + sub_canvas_height - margin - 1);
try path.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 path.moveTo(x_offset + sub_canvas_width - margin - 1, y_offset + sub_canvas_height / 2);
try path.lineTo(x_offset + margin, y_offset + sub_canvas_height / 2);
// Joined, clockwise
x_offset = 0;
y_offset = sub_canvas_height * 2;
try path.moveTo(x_offset + margin, y_offset + margin);
try path.lineTo(x_offset + sub_canvas_width - margin - 1, y_offset + sub_canvas_height / 2 - 1);
try path.lineTo(x_offset + margin, y_offset + sub_canvas_height - margin - 1);
// Joined, counter-clockwise
x_offset = sub_canvas_width;
try path.moveTo(x_offset + sub_canvas_width - margin - 1, y_offset + margin);
try path.lineTo(x_offset + margin, y_offset + sub_canvas_height / 2 - 1);
try path.lineTo(x_offset + sub_canvas_width - margin - 1, y_offset + sub_canvas_height - margin - 1);
// Joined, clockwise up-down, down-up
x_offset = sub_canvas_width * 2;
sub_canvas_height = sub_canvas_height / 2;
try path.moveTo(x_offset + margin, y_offset + margin);
try path.lineTo(x_offset + sub_canvas_width / 2 - 1, y_offset + sub_canvas_height - margin - 1);
try path.lineTo(x_offset + sub_canvas_width - margin - 1, y_offset + margin);
// Joined, clockwise down-up, up-down
x_offset = sub_canvas_width * 3;
try path.moveTo(x_offset + margin, y_offset + sub_canvas_height - margin - 1);
try path.lineTo(x_offset + sub_canvas_width / 2 - 1, y_offset + margin);
try path.lineTo(x_offset + sub_canvas_width - margin - 1, y_offset + sub_canvas_height - margin - 1);
// Joined, counter-clockwise, down-up, up-down
x_offset = sub_canvas_width * 2;
y_offset = y_offset + sub_canvas_height;
try path.moveTo(x_offset + sub_canvas_width - margin - 1, y_offset + sub_canvas_height - margin - 1);
try path.lineTo(x_offset + sub_canvas_width / 2 - 1, y_offset + margin);
try path.lineTo(x_offset + margin, y_offset + sub_canvas_height - margin - 1);
// Joined, counter-clockwise, up-down, down-up
x_offset = sub_canvas_width * 3;
try path.moveTo(x_offset + sub_canvas_width - margin - 1, y_offset + margin);
try path.lineTo(x_offset + sub_canvas_width / 2 - 1, y_offset + sub_canvas_height - margin - 1);
try path.lineTo(x_offset + margin, y_offset + margin);
try context.stroke(alloc, path);
// We draw a hairline in the same path in red - this validates how the caps
// and joins are aligned.
context.pattern.opaque_pattern.pixel = .{ .rgb = .{ .r = 0xF3, .g = 0x00, .b = 0x00 } }; // Red
context.line_width = 1;
try context.stroke(alloc, path);
return sfc;
}