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
use std::cmp::min;

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 {
            if self.pt1.0 == self.pt2.0 && self.pt1.0 < canvas.width {
                let (min_y, max_y) = if self.pt1.1 > self.pt2.1 {
                    (self.pt2.1, self.pt1.1)
                } else {
                    (self.pt1.1, self.pt2.1)
                };
                for y in min_y..min(max_y, canvas.height - 1) + 1 {
                    canvas.draw_point(self.pt1.0, y, self.color)
                }
            } else if self.pt1.1 == self.pt2.1 && self.pt1.1 < canvas.height {
                let (min_x, max_x) = if self.pt1.0 > self.pt2.0 {
                    (self.pt2.0, self.pt1.0)
                } else {
                    (self.pt1.0, self.pt2.0)
                };
                for x in min_x..min(max_x, canvas.width - 1) + 1 {
                    canvas.draw_point(x, self.pt1.1, self.color)
                }
            } else {
                // 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)
                }
            }
        }
    }
}