How to easily calculate the size and position of text that is crooked without rotation?

83 Views Asked by At

I have created this picture for illustration.

enter image description here

I want to use it in a program, but this is a math problem, so I'm writing it here.

When I know the dimensions, I can calculate the height of the letters if all the spaces are 5.

textHeight = (height - ((numberOfLetters + 2) * 5)) / numberOfLetters

In the picture: textHeight = (800 - 10 * 5) / 9

Although the letters have a height higher than the width, the shape does not have to be square, so it is not possible to predict whether the width will exceed the maximum width. If so, I would still have to calculate the width.

textWidth = (width - ((numberOfLetters + 2) * 5)) / numberOfLetters

In the picture: textWidth = (800 - 10 * 5) / 9 (actually using monospaced text so it's possible)

However, the size of the letter is not defined by the width, and that is the problem. Is it possible to calculate the height so that it does not exceed the maximum width? In addition, if I know the ratio between width and height.

The second problem is positioning. It can be calculated using a diagonal, but by starting with the middle letter in the middle of the square. But it would be easier to start from the first letter. In addition, if the word does not have an odd number of letters, there will be nothing in the middle.

PS. The spaces between the edge of the square and the first and last letter may of course be larger, but there will always be 5 between the individual letters.

Please help.

Thank you

1

There are 1 best solutions below

4
On

Its not clear what conditions are required for you.

Consider this task: The text will appear so that the lower left hand corner of each letter will be aligned on a line slanted at an angle. The line will go through the letter and emerge either out of the left or top side and the lower left hand left hand corner of the next (or previous letter will begin five space latter on the line.

So our initial data is the angle of the line, the height and width of the paper, and the text we need to write and the dimensions of all letters and symbols.

First calculation, without scaling to fit the paper how long would the line need to be.

First notice if angle is $0$ to $90$ the line will go from the lower left hand corner of the current letter on a path that has an rightward horizontal change at least that of the width of the current letter and a upward height change of the height of the current letter. We'll call this method 1.

If the angle is $-90$ to $0$ the line will go from the lower left hand corner of the current letter on a path that has a rightward horizontal change at least that of the width of the current letter and a downward height change of the height of the next letter. Call this method 2.

If angle is $90$ to $180$ the line will go from the lower left hand corner of the current letter on a path that has a leftward horizontal change at least that of the width of the next letter and a upward height change of the height of the next letter. Call this method 3.

If angle is $-180$ to $-90$ the line will go from the lower left hand corner of the current letter on a path that has a leftward horizontal change at least that of the width of the next letter and a downward height change of the height of the next letter. Call this method 4.

The "methods" start and $(0,0)$. Now we will either go horizontally across the width of the appropriate letter (plus five for the buffer) and go vertically that width times the $\tan$ of the angle, or it will go vertically the height of the appropriate letter (plus five for the buffer) and go across that height times the $\cot $ of the angle. Choose the greater distance and let that be $(x_1, y_1)$. Whether to appropriate letter is the current or the next one, and whether you go up or down or left and right depend on the method.

Do that to get the coordinates of the base of all the letters. If you did method 3 or 4 you will need find the point before $(0,0)$ (by the same method) to make room for the first letter. If you did method 1 or 2 you will need to make the point after the lower left corner of the last letter to make room for the last letter.

Now adjust take the highest/lowest points the letters reach and the left and right most point the letters reach.

You will now have the coordinates of a rectangle and the coordinate of the bottom left corner of every letter. Scale it to fit the page.

==== old answer====

So lets say that you have a letter $X$ with width $w_x$ and and hieght $h_x$ and centered at $(a_x, b_x)$. then you want to place the next letter smack next to it but at an angle of $\theta$. The first point to clear then horizontal edge would be $(a_x + h_x, b_x + h*x\sin \theta)$, The first point to clear the vertical edge is $(a_x + w_x* \cos \theta, b_x + w_x)$. As you need to clear both edges you would choose $(a_x + \max(h_x, w_x* \cos \theta), b_x + \max(w_x, h*x\sin \theta)$.

Figure out the center of the second letter similarly.