Implement canvas::Path::circle helper method

This commit is contained in:
Héctor Ramón Jiménez 2020-04-14 06:54:12 +02:00
parent c545af3577
commit 46cd0891d2
3 changed files with 15 additions and 11 deletions

View File

@ -95,11 +95,13 @@ impl From<chrono::DateTime<chrono::Local>> for LocalTime {
impl canvas::Drawable for LocalTime { impl canvas::Drawable for LocalTime {
fn draw(&self, frame: &mut canvas::Frame) { fn draw(&self, frame: &mut canvas::Frame) {
use canvas::Path;
let center = frame.center(); let center = frame.center();
let radius = frame.width().min(frame.height()) / 2.0; let radius = frame.width().min(frame.height()) / 2.0;
let offset = Vector::new(center.x, center.y); let offset = Vector::new(center.x, center.y);
let clock = canvas::Path::new(|path| path.circle(center, radius)); let clock = Path::circle(center, radius);
frame.fill(&clock, Color::from_rgb8(0x12, 0x93, 0xD8)); frame.fill(&clock, Color::from_rgb8(0x12, 0x93, 0xD8));

View File

@ -141,8 +141,8 @@ impl canvas::Drawable for State {
} }
}); });
let sun = Path::new(|path| path.circle(center, Self::SUN_RADIUS)); let sun = Path::circle(center, Self::SUN_RADIUS);
let orbit = Path::new(|path| path.circle(center, Self::ORBIT_RADIUS)); let orbit = Path::circle(center, Self::ORBIT_RADIUS);
frame.fill(&space, Color::BLACK); frame.fill(&space, Color::BLACK);
frame.fill(&stars, Color::WHITE); frame.fill(&stars, Color::WHITE);
@ -168,10 +168,7 @@ impl canvas::Drawable for State {
); );
frame.translate(Vector::new(Self::ORBIT_RADIUS, 0.0)); frame.translate(Vector::new(Self::ORBIT_RADIUS, 0.0));
let earth = Path::new(|path| { let earth = Path::circle(Point::ORIGIN, Self::EARTH_RADIUS);
path.circle(Point::ORIGIN, Self::EARTH_RADIUS)
});
let shadow = Path::rectangle( let shadow = Path::rectangle(
Point::new(0.0, -Self::EARTH_RADIUS), Point::new(0.0, -Self::EARTH_RADIUS),
Size::new(Self::EARTH_RADIUS * 4.0, Self::EARTH_RADIUS * 2.0), Size::new(Self::EARTH_RADIUS * 4.0, Self::EARTH_RADIUS * 2.0),
@ -186,10 +183,7 @@ impl canvas::Drawable for State {
); );
frame.translate(Vector::new(0.0, Self::MOON_DISTANCE)); frame.translate(Vector::new(0.0, Self::MOON_DISTANCE));
let moon = Path::new(|path| { let moon = Path::circle(Point::ORIGIN, Self::MOON_RADIUS);
path.circle(Point::ORIGIN, Self::MOON_RADIUS)
});
frame.fill(&moon, Color::WHITE); frame.fill(&moon, Color::WHITE);
}); });

View File

@ -43,6 +43,14 @@ impl Path {
Self::new(|p| p.rectangle(top_left, size)) Self::new(|p| p.rectangle(top_left, size))
} }
/// Creates a new [`Path`] representing a circle given its center
/// coordinate and its radius.
///
/// [`Path`]: struct.Path.html
pub fn circle(center: Point, radius: f32) -> Self {
Self::new(|p| p.circle(center, radius))
}
#[inline] #[inline]
pub(crate) fn raw(&self) -> &lyon::path::Path { pub(crate) fn raw(&self) -> &lyon::path::Path {
&self.raw &self.raw