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
use std::cmp::{max, min};

use Canvas;
use Drawable;
use Endian;

/// 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 = min(self.pt1.1, self.pt2.1);
                let max_y = min(max(self.pt1.1, self.pt2.1), canvas.height - 1);
                for y in min_y..=max_y {
                    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 = min(self.pt1.0, self.pt2.0);
                let max_x = min(max(self.pt1.0, self.pt2.0), canvas.width - 1);
                for x in min_x..=max_x {
                    canvas.draw_point(x, self.pt1.1, self.color)
                }
            } else {
                // Angled line without antialias
                for (x, y) in bresenham(
                    self.pt1.0 as isize,
                    self.pt1.1 as isize,
                    self.pt2.0 as isize,
                    self.pt2.1 as isize,
                ) {
                    if x < canvas.width && y < canvas.height {
                        canvas.draw_point(x, y, self.color)
                    }
                }
            }
        } else {
            // Angled line with antialias
            for (x, y, coverage) in xiaolin_wu(
                self.pt1.0 as f32,
                self.pt1.1 as f32,
                self.pt2.0 as f32,
                self.pt2.1 as f32,
            ) {
                if x < canvas.width && y < canvas.height {
                    let mut color = self.color;
                    let base = canvas.stride * y + canvas.pixel_size * x;
                    if coverage != 1.0 {
                        if canvas.endianness == Endian::Little {
                            color[1] = (canvas.buffer[base + 2] as f32 * (1.0 - coverage)
                                + color[1] as f32 * coverage)
                                as u8;
                            color[2] = (canvas.buffer[base + 1] as f32 * (1.0 - coverage)
                                + color[2] as f32 * coverage)
                                as u8;
                            color[3] = (canvas.buffer[base] as f32 * (1.0 - coverage)
                                + color[3] as f32 * coverage)
                                as u8;
                        } else {
                            color[1] = (canvas.buffer[base + 1] as f32 * (1.0 - coverage)
                                + color[1] as f32 * coverage)
                                as u8;
                            color[2] = (canvas.buffer[base + 2] as f32 * (1.0 - coverage)
                                + color[2] as f32 * coverage)
                                as u8;
                            color[3] = (canvas.buffer[base + 3] as f32 * (1.0 - coverage)
                                + color[3] as f32 * coverage)
                                as u8;
                        }
                    }
                    canvas.draw_point(x as usize, y as usize, color)
                }
            }
        }
    }
}

fn bresenham(mut x0: isize, mut y0: isize, x1: isize, y1: isize) -> Vec<(usize, usize)> {
    let mut points: Vec<(usize, usize)> = Vec::new();
    let dx = (x1 - x0).abs();
    let sx = if x0 < x1 { 1 } else { -1 };
    let dy = -((y1 - y0).abs());
    let sy = if y0 < y1 { 1 } else { -1 };
    let mut err = dx + dy;

    loop {
        points.push((x0 as usize, y0 as usize));
        if x0 == x1 && y0 == y1 {
            break;
        };
        let e2 = 2 * err;
        if e2 >= dy {
            err += dy;
            x0 += sx;
        }
        if e2 <= dx {
            err += dx;
            y0 += sy;
        }
    }
    points
}

fn xiaolin_wu(mut x0: f32, mut y0: f32, mut x1: f32, mut y1: f32) -> Vec<(usize, usize, f32)> {
    let mut points: Vec<(usize, usize, f32)> = Vec::new();
    let steep = (y1 - y0).abs() > (x1 - x0).abs();
    if steep {
        std::mem::swap(&mut x0, &mut y0);
        std::mem::swap(&mut x1, &mut y1);
    }
    if x0 > x1 {
        std::mem::swap(&mut x0, &mut x1);
        std::mem::swap(&mut y0, &mut y1);
    }
    let dx = x1 - x0;
    let dy = y1 - y0;
    let gradient = if dx == 0.0 {
        1.0
    } else {
        dy as f32 / dx as f32
    };

    let mut intery = y0 + gradient;
    points.push((x0 as usize, y0 as usize, 1.0));
    points.push((x1 as usize, y1 as usize, 1.0));
    if steep {
        for x in x0 as usize + 1..=x1 as usize - 1 {
            points.push((intery as usize, x, 1.0 - intery.fract()));
            points.push((intery as usize + 1, x, intery.fract()));
            intery = intery + gradient;
        }
    } else {
        for x in x0 as usize + 1..=x1 as usize - 1 {
            points.push((x, intery as usize, 1.0 - intery.fract()));
            points.push((x, intery as usize + 1, intery.fract()));
            intery = intery + gradient;
        }
    }
    points
}