-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add more constructors and math helpers for primitive shapes #10632
Conversation
/// Get the area of the ellipse | ||
pub fn area(&self) -> f32 { | ||
PI * self.half_width * self.half_height | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I would've also added a perimeter
method, but there is no trivial method of computing it for ellipses outside of complex approximations
/// Create a new `Rectangle` from a given half-size | ||
pub fn from_half_size(half_size: Vec2) -> Self { | ||
Self { | ||
half_width: size.x / 2., | ||
half_height: size.y / 2., | ||
half_width: half_size.x, | ||
half_height: half_size.y, | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cuboid
stores a half_size
vector, but Rectangle
stores it with separate properties. Should Rectangle
store a half_size
as well? It would make this constructor and the half_size
method obsolete and make the shape representations more consistent.
/// | ||
/// This is the angle formed by two adjacent sides with points | ||
/// within the angle being in the interior of the polygon | ||
pub fn internal_angle_degrees(&self) -> f32 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it fine to have separate _degrees
and _radians
versions, or should there be just one, possibly with degrees? Using separate versions makes the computation slightly more efficient since the user doesn't need to convert the units.
Compiler should optimize these
I ended up splitting the |
Co-authored-by: IQuick 143 <IQuick143cz@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hope I didn't miss anything, but I think the PR is good.
8b69a81
to
5965561
Compare
…ne#10632) # Objective Working towards finishing a part of bevyengine#10572, this PR adds a ton of math helpers and useful constructors for primitive shapes. I also tried fixing some naming inconsistencies. ## Solution - Add mathematical helpers like `area`, `volume`, `perimeter`, `RegularPolygon::inradius` and so on, trying to cover all core mathematical properties of each shape - Add some constructors like `Rectangle::from_corners`, `Cuboid::from_corners` and `Plane3d::from_points` I also derived `PartialEq` for the shapes where it's trivial. Primitives like `Line2d` and `Segment2d` are not trivial because you could argue that they would be equal if they had an opposite direction. All mathematical methods have tests with reference values computed by hand or with external tools. ## Todo - [x] Add tests to verify that the values from mathematical helpers are correct --------- Co-authored-by: IQuick 143 <IQuick143cz@gmail.com>
Objective
Working towards finishing a part of #10572, this PR adds a ton of math helpers and useful constructors for primitive shapes. I also tried fixing some naming inconsistencies.
Solution
area
,volume
,perimeter
,RegularPolygon::inradius
and so on, trying to cover all core mathematical properties of each shapeRectangle::from_corners
,Cuboid::from_corners
andPlane3d::from_points
I also derived
PartialEq
for the shapes where it's trivial. Primitives likeLine2d
andSegment2d
are not trivial because you could argue that they would be equal if they had an opposite direction.All mathematical methods have tests with reference values computed by hand or with external tools.
Todo