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
use std::f32::consts::FRAC_PI_2;
use image::{ImageBuffer, Rgb};
use imageproc::drawing::{draw_convex_polygon_mut, draw_filled_circle_mut, Point};
pub fn fill_mut(buffer: &mut ImageBuffer<Rgb<u8>, Vec<u8>>, color: Rgb<u8>) {
for pixel in buffer.pixels_mut() {
*pixel = color;
}
}
pub fn draw_line_mut(
buffer: &mut ImageBuffer<Rgb<u8>, Vec<u8>>,
x1: u32,
y1: u32,
x2: u32,
y2: u32,
thickness: f32,
color: Rgb<u8>,
) {
assert!(thickness > 0.0);
let angle = {
if x1 == x2 {
FRAC_PI_2
} else {
(((y2 - y1) / (x2 - x1)) as f32).atan()
}
};
let perpendicular_angle = angle + FRAC_PI_2;
let p1 = Point::new(
(x1 as f32 + thickness * perpendicular_angle.cos()) as i32,
(y1 as f32 + thickness * perpendicular_angle.sin()) as i32,
);
let p2 = Point::new(
(x1 as f32 - thickness * perpendicular_angle.cos()) as i32,
(y1 as f32 - thickness * perpendicular_angle.sin()) as i32,
);
let p3 = Point::new(
(x2 as f32 + thickness * perpendicular_angle.cos()) as i32,
(y2 as f32 + thickness * perpendicular_angle.sin()) as i32,
);
let p4 = Point::new(
(x2 as f32 - thickness * perpendicular_angle.cos()) as i32,
(y2 as f32 - thickness * perpendicular_angle.sin()) as i32,
);
draw_convex_polygon_mut(buffer, &[p1, p3, p4, p2], color);
draw_filled_circle_mut(buffer, (x1 as i32, y1 as i32), thickness as i32, color);
draw_filled_circle_mut(buffer, (x2 as i32, y2 as i32), thickness as i32, color);
}