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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//! Wrapper around a pixel buffer.

use std::{cmp::Ordering, ops::Range};

use vek::{Disk, Extent2, LineSegment2, Vec2};

/// Simple wrapper around a pixel buffer that can be passed around to rendering calls.
pub struct Canvas<'a> {
    /// Size of the canvas in pixels.
    pub(crate) size: Extent2<usize>,
    /// Reference to the pixel buffer.
    pub(crate) buffer: &'a mut [u32],
}

impl<'a> Canvas<'a> {
    /// Set a pixel on the buffer at the coordinate passed.
    ///
    /// If the coordinate is out of bounds nothing will be done.
    ///
    /// This is quite a slow operation because it needs to calculate the index of the coordinate, if setting multiple pixels it might be more efficient to create a sprite from them.
    #[inline]
    pub fn set_pixel(&mut self, position: Vec2<f64>, color: u32) {
        if position.x < 0.0 || position.y < 0.0 {
            return;
        }

        let x = position.x.round() as usize;
        let y = position.y.round() as usize;
        if x >= self.size.w || y >= self.size.h {
            return;
        }

        let index = x + y * self.size.w;
        self.buffer[index] = color;
    }

    /// Draw a line using Bresenham's line algorithm.
    #[inline]
    pub fn draw_line(&mut self, start: Vec2<f64>, end: Vec2<f64>, color: u32) {
        let isize_width = self.size.w as isize;

        clipline::clipline(
            (start.as_().into_tuple(), end.as_().into_tuple()),
            ((0, 0), (self.size - Vec2::new(1, 1)).as_().into_tuple()),
            |x: isize, y: isize| {
                let index = x + y * isize_width;

                self.buffer[index as usize] = color;
            },
        );
    }

    /// Fill a horizontal line, very cheap operation.
    #[inline]
    pub fn draw_scanline(&mut self, y: usize, x: Range<usize>, color: u32) {
        if y >= self.size.h {
            return;
        }

        let y_index = y * self.size.w;

        // Flip if end is later than start
        let x_start = y_index + x.start.min(self.size.w - 1);
        let x_end = y_index + x.end.min(self.size.w);

        self.buffer[x_start..x_end].fill(color);
    }

    /// Draw a circle using Bresenham's circle algorithm.
    pub fn draw_circle_outline(&mut self, circle: Disk<f64, f64>, color: u32) {
        let radius = circle.radius.round() as i32;

        let mut x = 0;
        let mut y = -radius;
        let mut f_m = 1 - radius;
        let mut d_e = 3;
        let mut d_ne = -((radius) << 1) + 5;

        let center = circle.center.round();

        // Draw the missing horizontal line
        let mut draw_pixel_8_sides = |x: f64, y: f64| {
            self.set_pixel(center + Vec2::new(x, y), color);
            self.set_pixel(center + Vec2::new(-x, y), color);
            self.set_pixel(center + Vec2::new(-x, -y), color);
            self.set_pixel(center + Vec2::new(x, -y), color);
            self.set_pixel(center + Vec2::new(y, x), color);
            self.set_pixel(center + Vec2::new(-y, x), color);
            self.set_pixel(center + Vec2::new(-y, -x), color);
            self.set_pixel(center + Vec2::new(y, -x), color);
        };

        // Draw main corners
        draw_pixel_8_sides(circle.radius.round(), 0.0);

        while x < -y {
            if f_m <= 0 {
                f_m += d_e;
            } else {
                f_m += d_ne;
                d_ne += 2;
                y += 1;
            }

            d_e += 2;
            d_ne += 2;
            x += 1;

            draw_pixel_8_sides(x as f64, y as f64);
        }
    }

    /// Fill a circle using Bresenham's circle algorithm.
    ///
    /// Based on: https://funloop.org/post/2021-03-15-bresenham-circle-drawing-algorithm.html
    pub fn draw_circle(&mut self, circle: Disk<f64, f64>, color: u32) {
        let center: Vec2<i32> = circle.center.round().as_();
        let radius = circle.radius.round() as i32;

        let mut x = 0;
        let mut y = -radius;
        let mut f_m = 1 - radius;
        let mut d_e = 3;
        let mut d_ne = -((radius) << 1) + 5;

        // Draw the missing horizontal line
        let left = (center.x - radius).max(0) as usize;
        let right = (center.x + radius).max(0) as usize;
        self.draw_scanline(center.y.max(0) as usize, left..right, color);

        self.set_pixel(circle.center + Vec2::new(0.0, circle.radius), color);
        self.set_pixel(circle.center - Vec2::new(0.0, circle.radius), color);

        while x < -y {
            if f_m <= 0 {
                f_m += d_e;
            } else {
                f_m += d_ne;
                d_ne += 2;
                y += 1;
            }

            d_e += 2;
            d_ne += 2;
            x += 1;

            let mut draw_scanline = |x: i32, y: i32| {
                let negative_y = (center.y - y).max(0) as usize;
                let positive_y = (center.y + y).max(0) as usize;

                let absolute_min_x = (center.x - x).max(0) as usize;
                let absolute_max_x = (center.x + x).max(0) as usize;

                self.draw_scanline(negative_y, absolute_min_x..absolute_max_x, color);
                self.draw_scanline(positive_y, absolute_min_x..absolute_max_x, color);
            };

            draw_scanline(x, y);
            draw_scanline(-y, x);
        }
    }

    /// Fill a triangle.
    ///
    /// Based on: https://joshbeam.com/articles/triangle_rasterization/
    pub fn draw_triangle(&mut self, corners: [Vec2<f64>; 3], color: u32) {
        // Create the 3 edges from the triangle
        let edges = [
            Edge::new(corners[0], corners[1]),
            Edge::new(corners[1], corners[2]),
            Edge::new(corners[2], corners[0]),
        ];

        // Find the longest edge
        let Some((longest_edge_index, longest_edge)) =
            edges.iter().enumerate().max_by(|(_, edge1), (_, edge2)| {
                edge1
                    .y_length()
                    .partial_cmp(&edge2.y_length())
                    .unwrap_or(Ordering::Equal)
            })
        else {
            // Something weird happened, just don't do anything in that case
            return;
        };

        // Find the other two edges
        let short_edge1 = &edges[(longest_edge_index + 1) % 3];
        let short_edge2 = &edges[(longest_edge_index + 2) % 3];

        // Draw the spans between both edges
        self.draw_span_between_edges(longest_edge, short_edge1, color);
        self.draw_span_between_edges(longest_edge, short_edge2, color);
    }

    /// Fill a polygon with 4 corners.
    ///
    ///
    /// If any of the 4 points falls inside the triangle of the other three only a single triangle will be drawn.
    ///
    /// Based on: https://stackoverflow.com/a/2122620
    pub fn draw_quad(&mut self, corners: [Vec2<f64>; 4], color: u32) {
        fn signed_area(a: Vec2<f64>, b: Vec2<f64>, c: Vec2<f64>) -> f64 {
            (a.y - b.y) * c.x + (b.x - a.x) * c.y + (a.x * b.y - b.x * a.y)
        }

        let abc_is_clockwise = signed_area(corners[0], corners[1], corners[2]) > 0.0;

        let abd_is_clockwise = signed_area(corners[0], corners[1], corners[3]) > 0.0;
        let bcd_is_clockwise = signed_area(corners[1], corners[2], corners[3]) > 0.0;
        let cad_is_clockwise = signed_area(corners[2], corners[0], corners[3]) > 0.0;

        // Match by checking the other triangle signs against ABC
        match (
            abd_is_clockwise == abc_is_clockwise,
            bcd_is_clockwise == abc_is_clockwise,
            cad_is_clockwise == abc_is_clockwise,
        ) {
            // ABC ABD
            (false, true, true) => {
                self.draw_triangle([corners[0], corners[1], corners[2]], color);
                self.draw_triangle([corners[0], corners[1], corners[3]], color);
            }
            // ABC BCD
            (true, false, true) => {
                self.draw_triangle([corners[0], corners[1], corners[2]], color);
                self.draw_triangle([corners[1], corners[2], corners[3]], color);
            }
            // ABC CAD
            (true, true, false) => {
                self.draw_triangle([corners[0], corners[1], corners[2]], color);
                self.draw_triangle([corners[2], corners[0], corners[3]], color);
            }

            // D is inside ABC
            (true, true, true) => self.draw_triangle([corners[0], corners[1], corners[2]], color),
            // C is inside ABD
            (true, false, false) => self.draw_triangle([corners[0], corners[1], corners[3]], color),
            // B is inside CAD
            (false, false, true) => self.draw_triangle([corners[0], corners[2], corners[3]], color),
            // A is inside BCD
            (false, true, false) => self.draw_triangle([corners[1], corners[2], corners[3]], color),

            // Shouldn't happen
            _ => (),
        }
    }

    /// Fill the canvas with a single color.
    #[inline]
    pub fn fill(&mut self, color: u32) {
        self.buffer.fill(color);
    }

    /// Get the raw buffer of pixels.
    #[inline]
    pub fn raw_buffer(&mut self) -> &mut [u32] {
        self.buffer
    }

    /// Width in pixels.
    #[inline]
    pub fn width(&self) -> usize {
        self.size.w
    }

    /// Height in pixels.
    #[inline]
    pub fn height(&self) -> usize {
        self.size.h
    }

    /// Size in pixels.
    #[inline]
    pub fn size(&self) -> Extent2<usize> {
        self.size
    }
}

/// Drawing helpers.
impl<'a> Canvas<'a> {
    /// Draw a span between two edges, this fills everything in between.
    fn draw_span_between_edges(&mut self, long: &Edge, short: &Edge, color: u32) {
        let long_y_length = long.y_length();
        // Ignore horizontal edges
        if long_y_length.abs() < 0.5 {
            return;
        }

        let short_y_length = short.y_length();
        // Ignore horizontal edges
        if short_y_length.abs() < 0.5 {
            return;
        }

        let long_x_diff = long.x_diff();
        let short_x_diff = short.x_diff();

        // Calculate interpolation factors
        let mut long_factor = (short.0.start.y - long.0.start.y) / long_y_length;
        let long_factor_step = long_y_length.recip();
        let mut short_factor = 0.0;
        let short_factor_step = short_y_length.recip();

        // Clamp to the canvas
        let start_y = (short.0.start.y.floor().max(0.0) as usize).min(self.size.h);
        let end_y = (short.0.end.y.ceil().max(0.0) as usize).min(self.size.h);

        for y in start_y..end_y {
            // Calculate the X based on the interpolation by Y
            let long_x = long.0.start.x + long_x_diff * long_factor;
            let short_x = short.0.start.x + short_x_diff * short_factor;

            let (start_x, end_x) = if long_x < short_x {
                (long_x, short_x)
            } else {
                (short_x, long_x)
            };

            // Clamp to the buffer
            let start_x = (start_x.floor().max(0.0) as usize).min(self.size.w);
            let end_x = (end_x.ceil().max(0.0) as usize).min(self.size.w);

            // Draw the pixels
            self.draw_scanline(y, start_x..end_x, color);

            // Increase interpolation factors
            long_factor += long_factor_step;
            short_factor += short_factor_step;
        }
    }
}

/// Edge helper wrapper for line segments.
struct Edge(LineSegment2<f64>);

impl Edge {
    /// Create an edge, sorting the Y coordinates.
    pub fn new(point1: Vec2<f64>, point2: Vec2<f64>) -> Self {
        if point1.y < point2.y {
            Self(LineSegment2 {
                start: point1,
                end: point2,
            })
        } else {
            Self(LineSegment2 {
                start: point2,
                end: point1,
            })
        }
    }

    /// Difference across the X axis, not length because it can be negative.
    fn x_diff(&self) -> f64 {
        self.0.end.x - self.0.start.x
    }

    /// Length across the Y axis.
    fn y_length(&self) -> f64 {
        self.0.end.y - self.0.start.y
    }
}