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 sf::Shape::getGeometricCenter()
#2536
Conversation
56bbe6a
to
7d88a10
Compare
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## master #2536 +/- ##
==========================================
+ Coverage 27.15% 27.26% +0.11%
==========================================
Files 228 228
Lines 19637 19669 +32
Branches 4710 4716 +6
==========================================
+ Hits 5332 5363 +31
- Misses 13826 13847 +21
+ Partials 479 459 -20
... and 3 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
|
c53b8b8
to
f4b49aa
Compare
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.
Indifferent on whether we should add this to SFML or not in general
src/SFML/Graphics/ConvexShape.cpp
Outdated
if (not m_points.empty()) | ||
return m_points.front(); // a single point | ||
|
||
return Vector2f{}; // empty |
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.
Not too sure about this one. It's impossible to differentiate between a polygon whose geometrical center is in 0,0
and a polygon with no points. Maybe this function should return std::optional<Vector2f>
?
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.
That's an option or it could throw an exception or leave the case UB
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.
Or assert
.
I'm not sure about optional
here, the original problem is that sf::ConvexShape
can be in a "degenerate" state -- it would just make users dereference the optional
all the time, possibly causing more UB than assert
(depending on whether optional::operator*
has also an assert).
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.
@Bromeon Sounds like a good idea. What to do in NDEBUG
builds? std::terminate
or throw std::out_of_range
?
if (m_pointCount == 3) | ||
return (getPoint(0) + getPoint(1) + getPoint(2)) / 3.f; |
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.
What's the significance of special-casing for 3 points?
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.
@vittorioromeo It's the one case where the geometric center is not @ Vector2f(m_radius, m_radius)
.
049638b
to
c380f45
Compare
@vittorioromeo & @Bromeon Would it perhaps be better to place the generic algorithm in |
863b908
to
93a70fa
Compare
This adds virtual Vector2f getGeometricCenter() const = 0; Signed-off-by: Ted Lyngmo <ted@lyncon.se>
93a70fa
to
8e391ca
Compare
Which PR do you want to keep open. This one or #2537? I don't think we need both. |
I wasn't sure if you'd be ok with adding the implementation to the |
This adds
as discussed here:
https://en.sfml-dev.org/forums/index.php?topic=29011
One practical use can be to set a rotation point in the middle of a circle, rectangle or convex shape.
Example:
Tasks