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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

use ::sdl2::render::Canvas;
use ::sdl2::video::Window;
use ::sdl2::rect::Point;

pub struct Graphics {
    pub canvas: Canvas<Window>
}

impl Graphics {
    pub fn new(canvas: Canvas<Window>) -> Self {
        Graphics {
            canvas: canvas,
        }
    }

    pub fn draw(&mut self, shape: Draw) {
        match shape {
            Draw::CircleFill {x, y, r} => {
                draw_circle(&mut self.canvas, true, x + r, y + r, r);
            },
            Draw::CircleLine {x, y, r} => {
                draw_circle(&mut self.canvas, false, x + r, y + r, r);
            },
            Draw::RectangleFill {x, y, w, h} => {
                self.canvas.fill_rect(::sdl2::rect::Rect::new(x, y, w, h)).unwrap();
            },
            Draw::RectangleLine {x, y, w, h} => {
                self.canvas.draw_rect(::sdl2::rect::Rect::new(x, y, w, h)).unwrap();
            },
            Draw::EllipseFill {x, y, w, h} => {
                draw_ellipse(&mut self.canvas, true, x, y, w, h);
            },
            Draw::EllipseLine {x, y, w, h} => {
                draw_ellipse(&mut self.canvas, false, x, y, w, h);
            },
            //Draw::Triangle {x, y, b, h} => {},
            s => {
                unimplemented!("not implemented: {:?}", s);
            }
        }
    }

    pub fn set_color(&mut self, r: u8, g: u8, b: u8) {
        self.canvas.set_draw_color(::sdl2::pixels::Color::RGB(r, g, b));
    }
}

#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Draw {
    CircleFill {
        x: i32,
        y: i32,
        r: i32,
    },
    CircleLine {
        x: i32,
        y: i32,
        r: i32,
    },
    RectangleFill {
        x: i32,
        y: i32,
        w: u32,
        h: u32,
    },
    RectangleLine {
        x: i32,
        y: i32,
        w: u32,
        h: u32,
    },
    EllipseFill {
        x: i32,
        y: i32,
        w: i32,
        h: i32,
    },
    EllipseLine {
        x: i32,
        y: i32,
        w: i32,
        h: i32,
    },
    Triangle {
        x: i32,
        y: i32,
        b: i32,
        h: i32,
    }
}

fn draw_circle(canvas: &mut Canvas<Window>, fill: bool, cx: i32, cy: i32, r: i32) {
    let mut x = 0;
    let mut y = r;
    let mut d = 2 - 2 * r;
    let mut err;

    while y >= 0 {
        if fill {
            canvas.draw_line(Point::new(cx + x, cy + y), Point::new(cx - x, cy + y)).unwrap();
            canvas.draw_line(Point::new(cx + x, cy - y), Point::new(cx - x, cy - y)).unwrap();
        } else {
            canvas.draw_point(Point::new(cx + x, cy + y)).unwrap();
            canvas.draw_point(Point::new(cx - x, cy + y)).unwrap();
            canvas.draw_point(Point::new(cx + x, cy - y)).unwrap();
            canvas.draw_point(Point::new(cx - x, cy - y)).unwrap();
        }

        err = 2 * (d + y) - 1;
        if d < 0 && err <= 0 {
            x += 1;
            d += 2 * x + 1;
            continue;
        }

        err = 2 * (d - x) - 1;
        if d > 0 && err > 0 {
            y -= 1;
            d += 1 - 2 * y;
            continue;
        }

        x += 1;
        d += 2 * (x - y);
        y -= 1;
    }
}

fn draw_ellipse(canvas: &mut Canvas<Window>, fill: bool, ex: i32, ey: i32, w: i32, h: i32) {
    let ex = ex + w;
    let ey = ey + h;

    let a2 = w * w;
    let b2 = h * h;
    let fa2 = 4 * a2;
    let fb2 = 4 * b2;

    let mut x = 0;
    let mut y = h;
    let mut sigma = 2 * b2 + a2 * (1 - 2 * h);

    while b2 * x <= a2 * y {
        if fill {
            canvas.draw_line(Point::new(ex + x, ey + y), Point::new(ex - x, ey + y)).unwrap();
            canvas.draw_line(Point::new(ex + x, ey - y), Point::new(ex - x, ey - y)).unwrap();
        } else {
            canvas.draw_point(Point::new(ex + x, ey + y)).unwrap();
            canvas.draw_point(Point::new(ex - x, ey + y)).unwrap();
            canvas.draw_point(Point::new(ex + x, ey - y)).unwrap();
            canvas.draw_point(Point::new(ex - x, ey - y)).unwrap();
        }

        if sigma >= 0 {
            sigma += fa2 * (1 - y);
            y -= 1;
        }

        sigma += b2 * ((4 * x) + 6);
        x += 1;
    }

    let mut x = w;
    let mut y = 0;
    let mut sigma = 2 * a2 + b2 * (1 - 2 * w);

    while a2 * y <= b2 * x {
        if fill {
            canvas.draw_line(Point::new(ex + x, ey + y), Point::new(ex - x, ey + y)).unwrap();
            canvas.draw_line(Point::new(ex + x, ey - y), Point::new(ex - x, ey - y)).unwrap();
        } else {
            canvas.draw_point(Point::new(ex + x, ey + y)).unwrap();
            canvas.draw_point(Point::new(ex - x, ey + y)).unwrap();
            canvas.draw_point(Point::new(ex + x, ey - y)).unwrap();
            canvas.draw_point(Point::new(ex - x, ey - y)).unwrap();
        }

        if sigma >= 0 {
            sigma += fb2 * (1 - x);
            x -= 1;
        }

        sigma += a2 * ((4 * y) + 6);
        y += 1;
    }
}