Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: virtual Vector2f sf::Shape::getCentroid() const = 0;  (Read 3773 times)

0 Members and 1 Guest are viewing this topic.

TedLyngmo

  • Newbie
  • *
  • Posts: 3
    • View Profile
virtual Vector2f sf::Shape::getCentroid() const = 0;
« 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.

Thrasher

  • Moderator
  • Jr. Member
  • *****
  • Posts: 57
    • View Profile
Re: virtual Vector2f sf::Shape::getCentroid() const = 0;
« Reply #1 on: April 22, 2023, 11:13:01 pm »
How do you define this in the general case for all polygons?

TedLyngmo

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: virtual Vector2f sf::Shape::getCentroid() const = 0;
« Reply #2 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: virtual Vector2f sf::Shape::getCentroid() const = 0;
« Reply #3 on: April 23, 2023, 09:52:35 pm »
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
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

TedLyngmo

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: virtual Vector2f sf::Shape::getCentroid() const = 0;
« Reply #4 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.

  • 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  :)
« Last Edit: April 24, 2023, 10:17:20 am by TedLyngmo »

 

anything