Center string at point in a "round" drawing area

This forum is read only. No new submissions are accepted.

Questions about motif? Contact us

3 posts / 0 new
Last post
Center string at point in a "round" drawing area

 

Here is some psuedo-code that draws something like a clock:
// Draw a full circleXDrawArc(display,drawingArea,gc,100,RADIUS+100,RADIUS,RADIUS,0,360);
// Draw the numbers on the facefor (i - 1; i <= 12; ++i){angle = i * 30;x = convertAngleIntoX(angle, RADIUS);y = convertAngleIntoY(angle, RADIUS);char numString[5];sprintf(numString,"%d", i);

XDrawString(display,drawingArea,gc,x,y,numString,strlen(numString));}
But the XDrawString always puts the lower left corner of the string at the coordinates specified, so as it goes around the circle, the numbers 12 & 3 appear outside the circle, and the numbers 6 & 9 appear inside the circle.

Can someone suggest a formula to adjust X and Y offsets, so all 12 numbers appear equally aligned with the radius? (i.e., the string's centerpoint is at the specified coordinates)?

Load the font by

 

Load the font by XLoadQueryFont() and set it to your gc.
Examine XFontStruct structure of your font, get ascent and descent, in order to calculate font height.
Use XTextWidth() of your string to get the width of your text in current font.
Do the math for centering.

 

Thank you for your input.

 

Thank you for your input. Getting the height and width of the font isn't the hard part ... doing the math to calculate the offset for centering is. I was trying to figure out if I could use a formula with SIN() and COS() in order to adjust the X and Y offsets, and wondering if someone has already burnt the brain cells to figure this out.