How to find geometric center of juce::Path?

Hello,
Is there any way to find geometric center of juce::Path? Or how to find biggest possible `Rectangle that could be drawn inside the closed Path?

I have in my project many buttons with various shapes defined by Paths. And I want to draw text in the center of each button, so at the moment I need to define various Button::resized() to define teaxt area Rectangle<float>. But it would be much easier if I can just find the geometric center of Path. Then only one resized() definition would be enaugh.

For any advice great thanks in advance.
Best Regards

Hi, @pajczur.
Your question reminds me of the Centroid (Centroid - Wikipedia) analogous to the centre of mass of physical objects. Maybe this is what you could be looking for. But: Will you consider the points coordinates as individual masses or the various shapes that can be part of a path? Results will be different in each case.
On the other hand, juce::Path class (JUCE: Path Class Reference) has a method, JUCE: Path Class Reference, which (I cite) “Returns the smallest rectangle that contains all the points within the past”. The average position in X and Y of the rectangle points would give you the centre of that rectangle enclosing the path.
I wish this is useful to you.

Gus

Thanks for your try.
Actually I have shapes with rounded edges, so just points coordinates are not enaugh. Path::getBounds() is also not for me, because in most of my cases it gives me just bounds of button which I already know. So for me the best would be to get biggest rectangle which can be drawn inside the closed path.

Of course I know there is math formula to find what I want, but I just thought it would be much easier if in Juce library I could already find such methods for Path :slight_smile:

Best Regards

“Or how to find biggest possible `Rectangle that could be drawn inside the closed Path?”

Do you mean that the rectangle has to be contained inside the path and can’t intersect it?
Do your shapes can be concave?
The biggest rectangle inside a shape may not represent the center of the shape at all, would it be the desired effect?
Are the shapes in your button offset?
Are the shapes considerably smaller than the button?
Do you also need the button’s hitbox to reflect the shape?

Hello Nfect,
thanks for your reply.

  • Yes I that the rectangle has to be contained inside the path and can’t intersect it.
  • I know that biggest rectangle inside a shape may not represent the center of the shape, but in all of my cases it would be perfect.
  • I am not sure what do you mean by “Are the shapes in your button offset?”. I would say not. I mean they don’t ofset when clicking, they are static.
  • The shapes are smaller in that meaning they have smaller area, because they are not rectangle, but they are fit perfectly in the button bounds. I mean that maPath.getBounds() give me exactly the same rectangle as myButton.getBounds()
  • The button hitbox is doesn’t matter for me in that case.

Best Regards