1use crate::{BackendCoord, BackendStyle, DrawingBackend, DrawingErrorKind};
2
3pub fn draw_rect<B: DrawingBackend, S: BackendStyle>(
4 b: &mut B,
5 upper_left: BackendCoord,
6 bottom_right: BackendCoord,
7 style: &S,
8 fill: bool,
9) -> Result<(), DrawingErrorKind<B::ErrorType>> {
10 if style.color().alpha == 0.0 {
11 return Ok(());
12 }
13 let (upper_left, bottom_right) = (
14 (
15 upper_left.0.min(bottom_right.0),
16 upper_left.1.min(bottom_right.1),
17 ),
18 (
19 upper_left.0.max(bottom_right.0),
20 upper_left.1.max(bottom_right.1),
21 ),
22 );
23
24 if fill {
25 if bottom_right.0 - upper_left.0 < bottom_right.1 - upper_left.1 {
26 for x in upper_left.0..=bottom_right.0 {
27 check_result!(b.draw_line((x, upper_left.1), (x, bottom_right.1), style));
28 }
29 } else {
30 for y in upper_left.1..=bottom_right.1 {
31 check_result!(b.draw_line((upper_left.0, y), (bottom_right.0, y), style));
32 }
33 }
34 } else {
35 b.draw_line(
36 (upper_left.0, upper_left.1),
37 (upper_left.0, bottom_right.1),
38 style,
39 )?;
40 b.draw_line(
41 (upper_left.0, upper_left.1),
42 (bottom_right.0, upper_left.1),
43 style,
44 )?;
45 b.draw_line(
46 (bottom_right.0, bottom_right.1),
47 (upper_left.0, bottom_right.1),
48 style,
49 )?;
50 b.draw_line(
51 (bottom_right.0, bottom_right.1),
52 (bottom_right.0, upper_left.1),
53 style,
54 )?;
55 }
56 Ok(())
57}
58