SFML community forums

Help => Graphics => Topic started by: Winplex on December 02, 2015, 05:50:00 pm

Title: Place text in the centre of a circle
Post by: Winplex 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:

(http://i66.tinypic.com/2i6ov2q.jpg)

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.
Title: Re: Place text in the centre of a circle
Post by: Laurent on December 02, 2015, 06:02:12 pm
Don't assume that numRect has a top-left coordinate of (0, 0).
Title: Re: Place text in the centre of a circle
Post by: Winplex 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!  :)