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
use line_drawing::Bresenham;
use line_drawing::XiaolinWu;

use Canvas;
use Drawable;

/// A drawable object that represents a line
pub struct Line {
    /// The first point of the line
    pub pt1: (usize, usize),
    /// The second point of the line
    pub pt2: (usize, usize),
    /// The color of the line
    pub color: [u8; 4],
    /// Decides whether the line will be antialiased
    pub antialiased: bool,
}

impl Line {
    /// Creates a new Line object
    pub fn new(
        pt1: (usize, usize),
        pt2: (usize, usize),
        color: [u8; 4],
        antialiased: bool,
    ) -> Line {
        Line {
            pt1,
            pt2,
            color,
            antialiased,
        }
    }
}

impl Drawable for Line {
    fn draw(&self, canvas: &mut Canvas) {
        if !self.antialiased || self.pt1.0 == self.pt2.0 || self.pt1.1 == self.pt2.1 {
            // Angled line without antialias
            for (x, y) in Bresenham::new(
                (self.pt1.0 as isize, self.pt1.1 as isize),
                (self.pt2.0 as isize, self.pt2.1 as isize),
            ) {
                if x < canvas.width as isize && y < canvas.height as isize {
                    canvas.draw_point(x as usize, y as usize, self.color)
                }
            }
        } else {
            // Angled line with antialias
            for ((x, y), coverage) in XiaolinWu::<f32, isize>::new(
                (self.pt1.0 as f32, self.pt1.1 as f32),
                (self.pt2.0 as f32, self.pt2.1 as f32),
            ) {
                if x < canvas.width as isize && y < canvas.height as isize {
                    let mut color = self.color;
                    color[3] = (f32::from(color[3]) * coverage) as u8;
                    canvas.draw_point(x as usize, y as usize, color)
                }
            }
        }
    }
}