Recently here asked this question , the answer to which neither the author nor I have found. The essence of the question is to display the text inscribed in a circle. And I decided to be confused, and realize it.
I can not come up with an algorithm to write the text in a circle, knowing the height of the line.
Nothing comes to mind, except for trying to find the best option through brute force.
That's what I want to achieve:
- enter the text in a circle, knowing the height of the line - and what is the height? Is the font size any given or can it be changed? And an extreme case - there is a text like I, the text is a little triangular , i.e. text that can normally fill the upper part of the circle, but which is no longer suitable for the lower part of the circle - what to do? A normal circle without cutting the words, we will not succeed. And surely at the top of each line should be more than the previous one? Or is it important that the borders of the text create the outline of a circle? those. text can be a stair-round - BOPOH
- @BOPOH, yes, there are a lot of nuances ... I think that the triangular text to fill the upper part, the font size is a constant, and the radius of the circle must change. - Vladyslav Matviienko
- 2But if you score on the circle itself? we shift the text in your example to the right - it turns out a triangle, i.e. if you make such a triangle - we get about a circle. "Approximately", because having done the same with a rhombus, we will get the same triangle in the same way, but with a stretch you can call the “rhombus” a “circle”. Although inserting additional space in a “diamond” between words, we can get our “circle”. Will this approach work? Then the algorithm is simple: from the beginning of the line we move the text up, and from the end - down so that our "triangle" is saved. When it’s impossible to save - we got our “circle” - BOPOH
- @BOPOH I remember the army, that in wartime Pi can reach 4. So you, with the "folding" of the circle, you get a triangle / rhombus ;-) - Kromster
1 answer
In my opinion everything is simple. Knowing the height of the line and its sequence number, we can calculate the required width (chord of the circle): ширина = корень из (высота*номер)*(2*радиус -высота*номер) . (I may be mistaken, but I think you are able to calculate the chord length yourself)
The task then comes down to the text you enter this width.
If you do not know the radius in advance, you can calculate it based on the fact that you need to enter the text area in the area of the circle: радиус = корень из (общая ширина * высота строки / пи)
Add tolerances, taking into account that part of the words will have to be transferred to the next line so that everything fits. Text alignment is needed on both sides.
- .. and repeat until everything fits with a large tolerance if it does not fit (due to the characteristics of the transfer for example). - Kromster
- @Krom Stern why? You can simply take a large enough tolerance, selected by the method of scientific tyke. Alignment on both sides eliminates the effect of too much tolerance. You can also search for the longest word in the text and take an admission based on it, but IMHO is unnecessary - Darth
- 3As a person who worked in the layout of magazines, I will say that one extra letter in a strip can lengthen the text by several lines, and all because of unsuccessful hyphenation. No one will pick up at random every time when the customer again something does not fit. Just add recursion with a large margin to the end - it is not difficult. And if you just take a large tolerance, then there can be too much empty space at the bottom of the circle. I do not criticize you in anything, just complement the answer. - Kromster
- And you need to write a hyphenation module taking into account the desired language, since Whole words will fit beautifully only with a very small font. And alignment a la Word, making spaces between words of different widths and everything will fit in as needed - Isaev