SFML community forums
General => SFML development => Topic started by: TedLyngmo on April 22, 2023, 01:42:13 pm
-
Hi! I wonder if there would be any interest to add
virtual Vector2f sf::Shape::getCentroid() const = 0;
I found myself wanting this function when I created polygons using sf::ConvexShape and I wanted the rotation point to be "in the middle" of the polygon so to speak.
I've now inherited sf::ConvexShape, as shown here https://stackoverflow.com/a/76079129/7582247, to do it automatically, but I think it would be useful to support it out of the box.
I can implement it and do a pull request if the idea isn't rejected.
-
How do you define this in the general case for all polygons?
-
> How do you define this in the general case for all polygons?
My plan was to do it using the code I shared in the link. For some (most) sf::Shape decendants I'm assuming shortcuts can be made.
The actual algorithms used in the different classes is perhaps less important than if the function would be a nice addition to the library or not.
-
I think it could make sense to add something like this.
Some considerations:
- Due to the complexity, we'd probably need to cache the calculation
- Well except for simple shapes where we can use simpler formulas
- The result should probably be in local coordinate space
- I personally would prefer getGeometricCenter() over Centroid
-
I think it could make sense to add something like this.
Great! I just made my first PR https://github.com/SFML/SFML/pull/2536 and hope I didn't miss anything.
- Due to the complexity, we'd probably need to cache the calculation
Yes, either that or document the complexity and suggest only calling it once after a Shape has been defined. A user wanting to call it many times could then cache the result.
- The result should probably be in local coordinate space
Yes. It would use the data in std::vector<Vector2f> ConvexShape::m_points; as-is and return the untransformed value.
- I personally would prefer getGeometricCenter() over Centroid
I'm perfectly fine with that too :)