-
Scaling problem
I have recently come across a rather annoying mathematical problem. I am currently making a computer program that will scale text to fit inside a 5 pixels wide by 7 pixels high box.
At first it seemed easy since I was able to get the width and height of the character. For example if the characters size was 7 by 8 I could simply do this
5/7 = x (0.71428571428571428571428571428571)
7/8 = y (0.875)
Then I just say I want the image scaled by x and y which should give the character back as 5 by 7
When that wasn’t working I saw that there was an invisible box round the character that was used instead by the computer so its perceived width and height was wrong.
For example if the width counted by me is 7
And the computer thinks its 9 (No way to change this)
And I want the character to fit inside 5
I can no longer do 5/7 because it will be multiplied by 9 and I can’t do 5/9 because that will make the invisible box 5 and the actual width of the character counted by me to be smaller than 5
What maths can I use so that I can fit the character inside the 5 by 7 grid?
Thanks
-
Re: Scaling problem
I think that this is more of an implementation problem than a mathematical problem. The function you use to count the height of the character doesn't return what it's supposed to: it returns the height of the character + whitespace. I *think* that if you remake that function to find only the height of the character, you'll be able to have an image of the proper dimensions.
Two things I would do to write this function/make this work:
1. Loop through the printed character's pixels and find the first row (and then column) with a black pixel in it, and then find the last such row (and column). The distance between those two columns [inclusive] is the true height (and then width) of the character.
2. You'll still have to deal with the leftover whitespace somehow. I see two ways of doing this:
a. Make a function "CullWhitespace" that removes the whitespace on the borders of your character image. Then you can scale it as you feel like.
b. Keep the character with whitespace that you have now, but scale the image by a value such that the actual character part of the image is of the proper dimensions. Then, whenever you print out this character, you'll have to offset the image to the upper left, so that all of the whitespace surrounding the character is cut off.
Good luck!