57 lines
1.3 KiB
Rust
57 lines
1.3 KiB
Rust
#![allow(dead_code)]
|
|
|
|
use image::{ImageBuffer, Rgb, ImageResult};
|
|
use imageproc::drawing;
|
|
use std::path::Path;
|
|
|
|
type ImgBufU8 = ImageBuffer<Rgb<u8>, Vec<u8>>;
|
|
type Colour = Rgb<u8>;
|
|
type Coord = (i32, i32);
|
|
|
|
const DEFAULT_WIDTH: u32 = 800;
|
|
const DEFAULT_HEIGHT: u32 = 800;
|
|
const DEFAULT_BACKGROUND_COLOUR: Colour = Rgb([255u8, 255u8, 255u8]);
|
|
const DEFAULT_COLOUR: Colour = Rgb([0u8, 0u8, 0u8]);
|
|
|
|
pub struct Turtle {
|
|
buf: ImgBufU8,
|
|
colour: Colour,
|
|
pos: Coord,
|
|
angle: f64,
|
|
}
|
|
|
|
impl Turtle {
|
|
pub fn new() -> Self {
|
|
let mut buf = ImageBuffer::new(DEFAULT_WIDTH, DEFAULT_HEIGHT);
|
|
for pixel in buf.pixels_mut() {
|
|
*pixel = DEFAULT_BACKGROUND_COLOUR;
|
|
}
|
|
|
|
Turtle {
|
|
buf,
|
|
colour: DEFAULT_COLOUR,
|
|
pos: (0, 0),
|
|
angle: 0.0,
|
|
}
|
|
}
|
|
|
|
pub fn position(&self) -> Coord {
|
|
self.pos
|
|
}
|
|
|
|
pub fn draw_line(&mut self, start: Coord, end: Coord) {
|
|
drawing::draw_line_segment_mut(
|
|
&mut self.buf,
|
|
(start.0 as f32, start.1 as f32),
|
|
(end.0 as f32, end.1 as f32),
|
|
self.colour
|
|
);
|
|
}
|
|
|
|
pub fn save<P>(&self, output_path: P) -> ImageResult<()>
|
|
where
|
|
P: AsRef<Path>
|
|
{
|
|
self.buf.save(output_path)
|
|
}
|
|
} |