Skip to main content

chart/
decimate.rs

1//! Thinning a line to what its pixels can show.
2
3/// M4: of each run of points in one pixel column, the first, the lowest, the
4/// highest and the last, in their order. A line drawn through what is kept
5/// covers the same pixels as one drawn through every point. `points` run in
6/// ascending x.
7pub fn m4(points: &[[f32; 2]]) -> Vec<[f32; 2]> {
8    let mut kept = Vec::new();
9    let mut start = 0;
10    while start < points.len() {
11        let column = points[start][0].floor();
12        let (mut low, mut high, mut end) = (start, start, start);
13        while end < points.len() && points[end][0].floor() == column {
14            if points[end][1] < points[low][1] {
15                low = end;
16            }
17            if points[end][1] > points[high][1] {
18                high = end;
19            }
20            end += 1;
21        }
22        let mut picks = [start, low, high, end - 1];
23        picks.sort_unstable();
24        for (index, &pick) in picks.iter().enumerate() {
25            if index == 0 || pick != picks[index - 1] {
26                kept.push(points[pick]);
27            }
28        }
29        start = end;
30    }
31    kept
32}