84 lines
2.1 KiB
Rust
84 lines
2.1 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 = 300;
|
|
const DEFAULT_HEIGHT: u32 = 300;
|
|
const DEFAULT_BACKGROUND_COLOUR: Colour = Rgb([255u8, 255u8, 255u8]);
|
|
const DEFAULT_COLOUR: Colour = Rgb([0u8, 0u8, 0u8]);
|
|
const DEFAULT_POS: Coord = ((DEFAULT_WIDTH as f64/2.0) as i32, (DEFAULT_HEIGHT as f64/2.0) as i32);
|
|
|
|
pub struct Turtle {
|
|
buf: ImgBufU8,
|
|
colour: Colour,
|
|
pos: Coord,
|
|
step_length: u32,
|
|
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: DEFAULT_POS,
|
|
step_length: 10,
|
|
angle: 0.0,
|
|
}
|
|
}
|
|
|
|
pub fn position(&self) -> Coord {
|
|
self.pos
|
|
}
|
|
|
|
pub fn angle(&self) -> f64 {
|
|
self.angle.to_degrees()
|
|
}
|
|
|
|
pub fn forward(&mut self, steps_count: u32) {
|
|
let start_pos = self.pos;
|
|
|
|
let dy = self.angle.sin() * steps_count as f64 * self.step_length as f64;
|
|
let dx = self.angle.cos() * steps_count as f64 * self.step_length as f64;
|
|
|
|
self.pos.0 += dx.round() as i32;
|
|
self.pos.1 += dy.round() as i32;
|
|
|
|
self.draw_line(start_pos, self.pos);
|
|
}
|
|
|
|
pub fn right(&mut self, degrees: f64) {
|
|
self.angle += degrees.to_radians();
|
|
}
|
|
|
|
pub fn left(&mut self, degrees: f64) {
|
|
self.angle -= degrees.to_radians();
|
|
}
|
|
|
|
pub fn save<P>(&self, output_path: P) -> ImageResult<()>
|
|
where
|
|
P: AsRef<Path>
|
|
{
|
|
self.buf.save(output_path)
|
|
}
|
|
|
|
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
|
|
);
|
|
}
|
|
} |