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

Author Topic: Place text in the centre of a circle  (Read 3294 times)

0 Members and 2 Guests are viewing this topic.

Winplex

  • Newbie
  • *
  • Posts: 2
    • View Profile
Place text in the centre of a circle
« on: December 02, 2015, 05:50:00 pm »
The thing is that I have a set of CircleShape objects and the one of Text objects. What I need is to simply place each of those "texts" (their "strings" are basically numbers) in the centre of each of the circles. Like this:



The code snippets I'm trying to succeed in it with:

Vector2f center(width/2.0f, height/2.0f);
float angle = 0.0f;
float step = M_PI*2.0f/n;
float vxShapeRadius = 20.0f

for (int i = 0; i < n; i++)
{
        vxShapes[i].setFillColor(Color::Blue);
        vxShapes[i].setOrigin(vxShapeRadius, vxShapeRadius);
        vxShapes[i].setPosition(center.x + 200.0f*cos(angle), center.y - 200.0f*sin(angle));
        angle += step;
}

for (int i = 0; i < n; i++)
{
        char buff[255] = {0};
        nums[i].setFont(font);
        nums[i].setColor(Color::Red);
        nums[i].setCharacterSize(32);
        nums[i].setString(_itoa(i+1,buff,10));
        FloatRect numRect = nums[i].getGlobalBounds();
        Vector2f numRectCenter(numRect.width/2.0, numRect.height/2.0);
        nums[i].setOrigin(numRectCenter);
        nums[i].setPosition(vxShapes[i].getPosition().x, vxShapes[i].getPosition().y);
}
 

Though I'm not getting what is required. In which line(s) did I do it wrong?

vxShapes is an array of CircleShape objects, nums is an array of Text objects.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Place text in the centre of a circle
« Reply #1 on: December 02, 2015, 06:02:12 pm »
Don't assume that numRect has a top-left coordinate of (0, 0).
Laurent Gomila - SFML developer

Winplex

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Place text in the centre of a circle
« Reply #2 on: December 02, 2015, 07:24:46 pm »
Allright! I think I've sorted it out. I just changed one of the lines above to this:

Vector2f numRectCenter(numRect.width / 2.0 + numRect.left, numRect.height / 2.0 + numRect.top);

Great thanks for the help provided!  :)