polygon struct and draw polygons
This commit is contained in:
@@ -12,17 +12,16 @@ pub trait RotationExt {
|
||||
}
|
||||
|
||||
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
|
||||
fn rotate(&mut self, teta: f32) -> Vector {
|
||||
Vector::new(
|
||||
self.x * teta.cos() - self.y * teta.sin(),
|
||||
self.x * teta.sin() + self.y * teta.cos(),
|
||||
)
|
||||
}
|
||||
}
|
||||
pub struct PolygonFrame {
|
||||
pub radius: f32,
|
||||
pub teta: f32,
|
||||
pub polygons: Vec<Polygon>,
|
||||
}
|
||||
|
||||
impl<Message> canvas::Program<Message> for PolygonFrame {
|
||||
@@ -37,27 +36,34 @@ impl<Message> canvas::Program<Message> for PolygonFrame {
|
||||
bounds: Rectangle,
|
||||
_cursor: mouse::Cursor,
|
||||
) -> Vec<canvas::Geometry> {
|
||||
// We prepare a new `Frame`
|
||||
let mut frame = canvas::Frame::new(renderer, bounds.size());
|
||||
let radius = frame.size().width / 2.0 - 32.0;
|
||||
let mut vec = Vector::new(0.0, -radius);
|
||||
let c = frame.center();
|
||||
|
||||
// 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);
|
||||
// Draw all polygons by there points
|
||||
for poly in &self.polygons {
|
||||
let l = poly.points_teta.len();
|
||||
let mut line: canvas::Path;
|
||||
let mut t1: f32;
|
||||
let mut t2: f32;
|
||||
for i in 0..l {
|
||||
t1 = poly.points_teta[i] + poly.global_teta;
|
||||
t2 = poly.points_teta[(i + 1) % l] + poly.global_teta;
|
||||
line = canvas::Path::line(c + vec.rotate(t1), c + vec.rotate(t2));
|
||||
frame.stroke(
|
||||
&line,
|
||||
Stroke {
|
||||
width: 5.0,
|
||||
style: Style::Solid(Color::BLACK),
|
||||
..Stroke::default()
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
frame.stroke(
|
||||
&line,
|
||||
Stroke {
|
||||
width: 5.0,
|
||||
..Stroke::default()
|
||||
},
|
||||
);
|
||||
|
||||
// And fill it with some color
|
||||
// Draw Circle
|
||||
let circle = canvas::Path::circle(frame.center(), radius);
|
||||
frame.stroke(
|
||||
&circle,
|
||||
Stroke {
|
||||
@@ -66,9 +72,35 @@ impl<Message> canvas::Program<Message> for PolygonFrame {
|
||||
..Stroke::default()
|
||||
},
|
||||
);
|
||||
// Draw the red dot on the current position
|
||||
let dot = canvas::Path::circle(frame.center() + vec.rotate(self.teta), 5.0);
|
||||
frame.fill(&dot, Color::from_rgb(1.0, 0.0, 0.0));
|
||||
|
||||
// Draw the frame
|
||||
frame.stroke_rectangle(
|
||||
iced::Point { x: 0.0, y: 0.0 },
|
||||
frame.size(),
|
||||
Stroke {
|
||||
width: 16.0,
|
||||
..Stroke::default()
|
||||
},
|
||||
);
|
||||
|
||||
// Then, we produce the geometry
|
||||
vec![frame.into_geometry()]
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Polygon {
|
||||
pub global_teta: f32,
|
||||
pub points_teta: Vec<f32>,
|
||||
}
|
||||
|
||||
impl Polygon {
|
||||
pub fn triangle(teta: f32) -> Self {
|
||||
Polygon {
|
||||
global_teta: teta,
|
||||
points_teta: vec![0.0, 2.0 * PI / 3.0, 4.0 * PI / 3.0],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user