red dot revolution

This commit is contained in:
2025-05-29 13:01:58 +02:00
parent 54d87f99a6
commit 116a07ba27
4 changed files with 178 additions and 73 deletions

75
src/polygon_draw.rs Normal file
View File

@@ -0,0 +1,75 @@
use std::f32::consts::PI;
use iced::Vector;
use iced::mouse;
use iced::widget::canvas;
use iced::widget::canvas::Stroke;
use iced::widget::canvas::Style;
use iced::{Color, Rectangle, Renderer, Theme};
use std::time::SystemTime;
pub trait RotationExt {
fn rotate(&mut self, teta: f32) -> Self;
}
impl RotationExt for Vector {
fn rotate(&mut self, teta: f32) -> Self {
let temp_x = self.x;
let temp_y = self.y;
self.x = temp_x * teta.cos() - temp_y * teta.sin();
self.y = temp_x * teta.sin() + temp_y * teta.cos();
*self
}
}
pub struct PolygonFrame {
pub radius: f32,
pub teta: f32,
}
impl<Message> canvas::Program<Message> for PolygonFrame {
// No internal state
type State = ();
fn draw(
&self,
_state: &(),
renderer: &Renderer,
_theme: &Theme,
bounds: Rectangle,
_cursor: mouse::Cursor,
) -> Vec<canvas::Geometry> {
// We prepare a new `Frame`
let mut frame = canvas::Frame::new(renderer, bounds.size());
// We create a `Path` representing a simple circle
let circle = canvas::Path::circle(frame.center(), self.radius);
let mut vec = Vector::new(0.0, -self.radius);
let line = canvas::Path::line(
frame.center() + vec,
frame.center() + vec.rotate(2.0 * PI / 3.0),
);
let dot = canvas::Path::circle(frame.center() + vec.rotate(self.teta), 10.0);
frame.stroke(
&line,
Stroke {
width: 5.0,
..Stroke::default()
},
);
// And fill it with some color
frame.stroke(
&circle,
Stroke {
width: 5.0,
style: Style::Solid(Color::from_rgba(0.0, 0.0, 0.0, 1.0)),
..Stroke::default()
},
);
frame.fill(&dot, Color::from_rgb(1.0, 0.0, 0.0));
// Then, we produce the geometry
vec![frame.into_geometry()]
}
}