Files
compost/src/circle.rs
2022-04-18 20:53:57 +02:00

43 lines
949 B
Rust

use std::f64::consts::PI;
use tui::style::Color;
use tui::widgets::canvas::{Painter, Shape};
pub struct Circle {
pub x: f64,
pub y: f64,
pub r: f64,
pub start: u16,
pub stop: u16,
pub color: Color,
}
impl Default for Circle {
fn default() -> Self {
Self {
x: 0.0,
y: 0.0,
r: 1.0,
start: 0,
stop: 360,
color: Color::White,
}
}
}
impl Shape for Circle {
fn draw(&self, painter: &mut Painter) {
let (x, y) = (self.x, self.y - self.r);
for angle in (self.start..self.stop).map(|n| n as f64 / 180.0 * PI) {
let (x, y) = rotate(x, y, angle);
if let Some((x, y)) = painter.get_point(x, y) {
painter.paint(x, y, self.color);
}
}
}
}
fn rotate(x: f64, y: f64, a: f64) -> (f64, f64) {
((x * a.cos() - y * a.sin()), (x * a.sin() + y * a.cos()))
}