Math Quickie: Centering

Someone was reading the circular text layout math installment and asked if I had "... any thoughts on how to draw the text so that it is centered?". I'm going to be blatantly honest. The math installments are mainly to bring math to professionals and draw tangible links between theory and implementation. I've seen far too many hacks end up in code where the math isn't understood, but the end result was good enough to get by. With that in mind, I don't have thoughts on how text is centered, I instead go back to the mathematical rules and equations to discover the answer. Hopefully given the small basis on math that I've been focusing on others are now able to do this as well.

Since the original article discussed linearly rotated text as well as circular text, let's center that first. Remember that the transformation pipeline allows us to offset, rotate, and then offset again. We'll use this to make the entire process much easier.

textLocationX = ??
textLocationY = ??
textSizeX = size(text).x
textSizeY = size(text).y
textOffsetX = textSizeX / 2

// Transformation pipeline in non GDI+ order
moveTo(-textOffsetX, -(textSizeY/2))
rotate(angle)
moveTo(textLocationX, textLocationY)

Easy enough. We simply offset or center the text with respect to the origin. Then rotate about the origin. Then move to the final destination. The center of the text will now be deposited on the point given to the equation.

Centered circular text isn't much more difficult. I tried to quickly explain the process in a comment, but it deserves a bit more. Our objective is to convert linear string distance into an angular offset. Our algorithm already takes an angular offset for rendering text, so our new centering code simply needs to exist at the front of the pipeline to modify this angle before we start rendering the characters out. If you recall that Carcd is the distance around the circumference per each degree we can find a degree offset by dividing the linear text length by the Carcd. This is how far the text will travel around the edge of the circle in degrees, cut this in half (aka center), and then add or subtract it from the initial angle and you have your angle centered circular text.

textSizeX = size(text).x // Upgrade to character wise because of GDI+ measurement crap
sweepAngle = textSizeX / Carcd
offsetAngle = sweepAngle / 2
angle = initial angle +/- offsetAngle // Depending on direction of rotation

I would hope that the understanding of these processes can flow from the previous articles. I've had a number of contacts based on the series where people are putting together additional concepts to create some cool things, but they needed the intial grounding. I've created some great new controls, a couple of which are powered by Whidbey so I'll hold onto them for a while, that really make use of all the math. I also want to discuss some three dimensional layout opportunities using the same math and GDI+. Stay tuned!

Published Saturday, August 28, 2004 9:11 AM by Justin Rogers
Filed under: ,

Comments

Thursday, April 17, 2008 2:34 AM by aa

# re: Math Quickie: Centering

please mention the code clearly so that it could be understand easily otherwise its not possible to grab it.

Saturday, May 16, 2009 10:04 PM by nick_paslic

# re: Math Quickie: Centering

Monday, February 14, 2011 8:21 PM by Mary Stepman

# re: Math Quickie: Centering

It was rather interesting for me to read this blog. Thanx for it. I like such themes and everything that is connected to them. I definitely want to read more soon.    

Mary  Stepman  

<a href="indianescortmodels.com/">indian escort agency London</a>

Monday, February 21, 2011 5:49 PM by Sara Swift

# re: Math Quickie: Centering

It was extremely interesting for me to read this article. Thank author for it. I like such themes and everything connected to this matter. I would like to read a bit more soon.    

Sara  Swift    

<a href="www.latinescortlondon.com/">spanish escort London</a>

Wednesday, March 02, 2011 4:01 PM by Sara Flouee

# re: Math Quickie: Centering

It was certainly interesting for me to read that blog. Thanks for it. I like such topics and everything connected to this matter. I definitely want to read more on this blog soon.      

Sara  Flouee    

<a href="www.jammer-store.com/">phone jammer kit</a>

Wednesday, March 09, 2011 2:14 PM by Kate Swingfield

# re: Math Quickie: Centering

It was rather interesting for me to read this blog. Thanx for it. I like such themes and anything connected to them. I definitely want to read a bit more on this site soon.      

Kate  Swingfield      

<a href="www.london-escort-agency.com/">paradise girls</a>

Tuesday, March 15, 2011 2:34 PM by Brandy Trider

# re: Math Quickie: Centering

It is extremely interesting for me to read that post. Thanx for it. I like such topics and anything connected to them. I would like to read more soon.        

Brandy  Trider      

<a href="rome-escort.info/">escorts a roma</a>

Tuesday, March 22, 2011 6:19 PM by Kate Swift

# re: Math Quickie: Centering

It is rather interesting for me to read this post. Thanks for it. I like such themes and everything connected to them. I definitely want to read more soon.        

Kate  Swift        

<a href="www.phone-blocker.com/">cell phone jammer buy</a>

Thursday, March 31, 2011 4:07 PM by Brandy Karver

# re: Math Quickie: Centering

It was extremely interesting for me to read this post. Thanx for it. I like such themes and everything that is connected to this matter. I definitely want to read a bit more soon.          

Brandy  Karver          

<a href="milanescorts.com/">independent escort milan</a>

Monday, January 09, 2012 7:01 AM by icorssusanna

# re: Math Quickie: Centering

you love this?  <a href=chanelbag2011.livejournal.com/>chanel bag 2011</a>  , just clicks away   , for special offer

Sunday, January 15, 2012 7:49 PM by Moogevictor

# re: Math Quickie: Centering

to buy <a href=www.hermesbirking.net/cheap-hermes-birkin-bags-ezp-5.html>hermes birkin bags</a>  online shopping   online shopping

Monday, January 16, 2012 8:07 AM by DierEdeandra

# re: Math Quickie: Centering

buy <a href=imtoo-dvd-to-iphone-converter.weebly.com/>imtoo dvd to iphone converter</a>  online    to get new coupon

Sunday, January 22, 2012 9:22 PM by puntyakiko

# re: Math Quickie: Centering

purchase <a href=dvd-to-mov.weebly.com/>dvd to mov</a>  online shopping   suprisely

Tuesday, January 31, 2012 12:11 PM by cluchdip

# re: Math Quickie: Centering

buy <a href=chanel-vintage-bags.weebly.com/>chanel vintage bags</a>  online shopping

Leave a Comment

(required) 
(required) 
(optional)
(required)