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
use crate::drawing::backend::{BackendCoord, BackendStyle, DrawingErrorKind};
use crate::drawing::DrawingBackend;

use crate::style::Color;

pub(crate) fn draw_rect<B: DrawingBackend, S: BackendStyle>(
    b: &mut B,
    upper_left: BackendCoord,
    bottom_right: BackendCoord,
    style: &S,
    fill: bool,
) -> Result<(), DrawingErrorKind<B::ErrorType>> {
    if style.as_color().alpha() == 0.0 {
        return Ok(());
    }
    let (upper_left, bottom_right) = (
        (
            upper_left.0.min(bottom_right.0),
            upper_left.1.min(bottom_right.1),
        ),
        (
            upper_left.0.max(bottom_right.0),
            upper_left.1.max(bottom_right.1),
        ),
    );

    if fill {
        if bottom_right.0 - upper_left.0 < bottom_right.1 - upper_left.1 {
            for x in upper_left.0..=bottom_right.0 {
                b.draw_line((x, upper_left.1), (x, bottom_right.1), style)?;
            }
        } else {
            for y in upper_left.1..=bottom_right.1 {
                b.draw_line((upper_left.0, y), (bottom_right.0, y), style)?;
            }
        }
    } else {
        b.draw_line(
            (upper_left.0, upper_left.1),
            (upper_left.0, bottom_right.1),
            style,
        )?;
        b.draw_line(
            (upper_left.0, upper_left.1),
            (bottom_right.0, upper_left.1),
            style,
        )?;
        b.draw_line(
            (bottom_right.0, bottom_right.1),
            (upper_left.0, bottom_right.1),
            style,
        )?;
        b.draw_line(
            (bottom_right.0, bottom_right.1),
            (bottom_right.0, upper_left.1),
            style,
        )?;
    }
    Ok(())
}