Lecture
Sch W C X
B f
S
B y
T
Uh
WITH
Yu R
I
P
ABOUT
BUT
BN
M
AT
L
GK
D
HER
YO F ZI
Tips for silly Let us analyze the problem by points. First, how to find where one letter or another should be? How to arrange them in a circle? For this it is necessary to recall trigonometry. If the point is on a circle, then there is a certain angle a between, for example, the axis X and the line drawn to the point. The angle is measured in degrees and can take values from 0 to 360 *. Accordingly, the letters can be set so that the angle a to them uniformly varies from 0 to 360 *. For example, if we have a phrase of 36 characters, then we can arrange letters every 10 * - 360 * will be typed in the total, or full circle. That is, we divide 360 * by the number of letters and get the angle between 2 adjacent letters. So, suppose that the center of the circle coincides with the origin. We know the radius of the circle and the angle on which is the letter. How to find her coordinates now? Again, we have to remember trigonometry. We can take a point with a letter and drop a perpendicular from it. (cut LK). We get a right triangle (OLK), and in a right triangle, as we know, the sides are interconnected through sine, cosines and tangents and angle a (read about sine in Wikipedia). In order not to torment you with formulas and mathematics, so be it, I will immediately write a formula for solving. So, sin (a) = LK / LO, and cos (a) = CO / LO. LO is the radius of the circle (we set it ourselves and we know it), and we also have a specific letter known, because rearranging a couple of items, we get (x coordinate) KO = LO * sin (a) (coordinate at the letter) LK = LO * cos (a) So, to get the coordinates of a letter, it’s enough to substitute the radius of the circle (LO) and the angle a into the formula. So, we can calculate the coordinates of any letter. The last question remains: how to place the letter at the desired point? After all, echo displays the text from left to right, and on in no particular order. So, how to display letters in given positions? To do this, we will create an array, each element of which is a line. Each line, in turn, is an array consisting of individual letters. Initially, we fill all elements of these arrays with spaces, that is, we get something like: $ screen = array (); $ screen [0] - array (0 => '', 1 => '', 2 => '', ...); $ screen [l] = array (0 => '', 1 => '', 2 => '', ...); • •• • $ screen [29] - array (0 => '', 1 => '', 2 => '' ...);
So, $ screen [0] is the first line, and $ screen [0] [0] is the first letter of the first line. We can write a letter in the element Sscreen [$ y] [$ x] to bring it in the right place. After we have found the coordinates of each letter and placed them in the Sscreen array, it remains to output it. To do this, we simply loop through this array and derive characters from it. You can output an entire line at a time if you use the implode () function wisely. Actually, this information is more than enough, I’ll warn you only about dirty tricks. Here they are: - in PHP (as well as in programming) functions sln () and cos () take an angle in radians. Radian is a unit of measure in which 180 * correspond to the number n (3.1415 ...). How to convert from degrees to radians - think for yourself or google. - Perhaps, when calculating the coordinates, you will have to change the sign to a minus: in front of the x or y coordinates. - In the array $ screen, you can write letters only under integer coordinates (and the sine gives the fractional result) - it means you need not forget about rounding and round () - Since the letters are not square, but rectangular, the x coordinate should be multiplied by the matched empirically coefficient, otherwise the circle will be flattened - If you change not only the angle, but also the radius, you can make a spiral instead of a circle!
<? php
error_reporting (-1);
mb_internal_encoding ('utf-8');
function mod ($ a, $ n) {
return ($ a% $ n) + ($ a <0? $ n: 0);
}
function dif ($ argument0, $ argument1)
{
$ d = rad2deg (deg2rad ($ argument1) - deg2rad ($ argument0));
if ($ d <0) $ d = 360 + $ d;
return $ d;
}
$ phrase = "ABCGDODEZHZIYKLMNOPRSTUFHTSCHSCHYYEYUYA";
$ phraselength = mb_strlen ($ phrase);
$ fromAngle = - 80;
$ toAngle = 260;
$ radius = 12.5;
$ height = 30;
$ centerX = 40;
$ centerY = 15;
$ screen = array ();
for ($ y = 0; $ y <$ height; $ y ++) {
$ screen [$ y] = array_fill (0, 80, '');
}
// if ($ toAngle <0) $ toAngle = 360 + $ toAngle;
// if ($ fromAngle <0) $ fromAngle = 180 + $ fromAngle;
/ * $ fromAngle = $ fromAngle-270;
$ toAngle = $ toAngle-270;
* /
echo dif ($ fromAngle, $ toAngle), ':', $ fromAngle, ':', $ toAngle, "<br />";
$ n = 0;
$ a = $ fromAngle; //
for ($ n = 0; $ n <$ phraselength; $ n ++)
{
$ x = round ($ centerX + 2.0 * $ radius * sin (deg2rad ($ a)));
$ y = round ($ centerY + $ radius * cos (deg2rad ($ a)));
echo mb_substr ($ phrase, $ n, 1), ': a =', $ a, ': r =', deg2rad ($ a), ': x =', $ x, ': y =', $ y , "<br />";
$ screen [$ y] [$ x] = mb_substr ($ phrase, $ n, 1);
$ a = $ a + dif ($ fromAngle, $ toAngle) / ($ phraselength-1);
if ($ a> 360) $ a = 0;
}
echo "<pre>";
// var_dump ($ screen);
foreach ($ screen AS $ st)
{$ st = implode ('', $ st);
echo $ st. "<br />";
}
Comments
To leave a comment
Running server side scripts using PHP as an example (LAMP)
Terms: Running server side scripts using PHP as an example (LAMP)