SFML community forums

General => SFML development => Topic started by: TedLyngmo on April 22, 2023, 01:42:13 pm

Title: virtual Vector2f sf::Shape::getCentroid() const = 0;
Post 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.
Title: Re: virtual Vector2f sf::Shape::getCentroid() const = 0;
Post by: Thrasher on April 22, 2023, 11:13:01 pm
How do you define this in the general case for all polygons?
Title: Re: virtual Vector2f sf::Shape::getCentroid() const = 0;
Post by: TedLyngmo on April 23, 2023, 10:46:01 am
> 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.
Title: Re: virtual Vector2f sf::Shape::getCentroid() const = 0;
Post by: eXpl0it3r on April 23, 2023, 09:52:35 pm
I think it could make sense to add something like this.

Some considerations:
Title: Re: virtual Vector2f sf::Shape::getCentroid() const = 0;
Post by: TedLyngmo on April 24, 2023, 07:41:34 am
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.


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.


Yes. It would use the data in std::vector<Vector2f> ConvexShape::m_points; as-is and return the untransformed value.


I'm perfectly fine with that too  :)