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
use vga::colors::Color16;
use vga::writers::GraphicsWriter;

/// Struct for storage mode vga
pub struct Figures2D<T> {
    mode: T,
}

impl<T: GraphicsWriter<Color16>> Figures2D<T> {
    /// Creates a new `Figures2D`.
    pub const fn new(mode: T) -> Figures2D<T> {
        Figures2D { mode }
    }

    /// Draw pixel
    pub fn pixel(&self, x: isize, y: isize, color: Color16) {
        self.rectangle(x, y, x, y, color);
    }

    /// Draw line
    pub fn line(&self, x1: isize, y1: isize, x2: isize, y2: isize, color: Color16) {
        self.mode.draw_line((x1, y1), (x2, y2), color);
    }

    /// Draw rectangle
    pub fn rectangle(&self, x1: isize, y1: isize, x2: isize, y2: isize, color: Color16) {
        self.mode.draw_line((x1, y1), (x1, y2), color);
        self.mode.draw_line((x1, y1), (x2, y1), color);
        self.mode.draw_line((x2, y2), (x1, y2), color);
        self.mode.draw_line((x2, y2), (x2, y1), color);
    }

    /// Draw ellipse
    pub fn ellipse(&self, x: isize, y: isize, a: isize, b: isize, color: Color16) {
        let asq = a * a;
        let bsq = b * b;

        self.pixel(x, y + b, color);
        self.pixel(x, y - b, color);

        let mut wx = 0;
        let mut wy = b;
        let mut xa = 0;
        let mut ya = asq * 2 * b;
        let mut thresh = asq / 4 - asq * b;

        loop {
            thresh += xa + bsq;

            if thresh >= 0 {
                ya -= asq * 2;
                thresh -= ya;
                wy -= 1;
            }

            xa += bsq * 2;
            wx += 1;

            if xa >= ya {
                break;
            }

            self.pixel(x + wx, y - wy, color);
            self.pixel(x - wx, y - wy, color);
            self.pixel(x + wx, y + wy, color);
            self.pixel(x - wx, y + wy, color);
        }

        self.pixel(x + a, y, color);
        self.pixel(x - a, y, color);

        wx = a;
        wy = 0;
        xa = bsq * 2 * a;

        ya = 0;
        thresh = bsq / 4 - bsq * a;

        loop {
            thresh += ya + asq;

            if thresh >= 0 {
                xa -= bsq * 2;
                thresh = thresh - xa;
                wx -= 1;
            }

            ya += asq * 2;
            wy += 1;

            if ya > xa {
                break;
            }

            self.pixel(x + wx, y - wy, color);
            self.pixel(x - wx, y - wy, color);
            self.pixel(x + wx, y + wy, color);
            self.pixel(x - wx, y + wy, color);
        }
    }

    /// Draw polygon
    pub fn polygon(&self, arr: &[isize], color: Color16) {
        let len = arr.len();
        if len % 2 == 0 && len >= 2 {
            let mut i = 0;
            while i <= len - 4 {
                self.mode
                    .draw_line((arr[i], arr[i + 1]), (arr[i + 2], arr[i + 3]), color);
                i += 2;
            }
            self.mode
                .draw_line((arr[0], arr[1]), (arr[len - 2], arr[len - 1]), color);
        }
    }

    /// Draw text
    pub fn text(&self, x: usize, y: usize, s: &str, color: Color16) {
        for (i, ch) in s.chars().enumerate() {
            // i * 8 - so that the text does not stick together
            self.mode.draw_character(x + i * 8, y, ch, color);
        }
    }
}