Skip to content

Commit

Permalink
Add constructors for RegularPolygon and Circle
Browse files Browse the repository at this point in the history
  • Loading branch information
rparrett committed Mar 2, 2022
1 parent f9d62b7 commit 4b68753
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
18 changes: 18 additions & 0 deletions crates/bevy_render/src/mesh/shape/regular_polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct RegularPolygon {
/// Number of sides.
pub sides: usize,
}

impl Default for RegularPolygon {
fn default() -> Self {
Self {
Expand All @@ -18,6 +19,13 @@ impl Default for RegularPolygon {
}
}

impl RegularPolygon {
// Creates a regular polygon in the xy plane
pub fn new(radius: f32, sides: usize) -> Self {
Self { radius, sides }
}
}

impl From<RegularPolygon> for Mesh {
fn from(polygon: RegularPolygon) -> Self {
let RegularPolygon { radius, sides } = polygon;
Expand Down Expand Up @@ -66,6 +74,16 @@ impl Default for Circle {
}
}

impl Circle {
/// Creates a circle in the xy plane
pub fn new(radius: f32) -> Self {
return Self {
radius,
..Default::default()
};
}
}

impl From<Circle> for RegularPolygon {
fn from(circle: Circle) -> Self {
Self {
Expand Down
19 changes: 5 additions & 14 deletions examples/2d/shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,18 @@ fn setup(

// Circle
commands.spawn_bundle(MaterialMesh2dBundle {
mesh: meshes.add(shape::Circle::default().into()).into(),
mesh: meshes.add(shape::Circle::new(50.).into()).into(),
material: materials.add(ColorMaterial::from(Color::PURPLE)),
transform: Transform::from_translation(Vec3::new(-100., 0., 0.))
.with_scale(Vec2::splat(100.0).extend(1.)),
transform: Transform::from_translation(Vec3::new(-100., 0., 0.)),

..default()
});

// Hexagon
commands.spawn_bundle(MaterialMesh2dBundle {
mesh: meshes
.add(
shape::RegularPolygon {
sides: 6,
..default()
}
.into(),
)
.into(),
mesh: meshes.add(shape::RegularPolygon::new(50., 6).into()).into(),
material: materials.add(ColorMaterial::from(Color::TURQUOISE)),
transform: Transform::from_translation(Vec3::new(100., 0., 0.))
.with_scale(Vec2::splat(100.0).extend(1.)),
transform: Transform::from_translation(Vec3::new(100., 0., 0.)),
..default()
});
}

0 comments on commit 4b68753

Please sign in to comment.