PHP phrase output in a circle in text mode

Lecture



  PHP phrase output in a circle in text mode 340: -80: 260
A: a = -80: r = -1.3962634015955: x = 15: y = 17
B: a = -69.032258064516: r = -1.2048401933122: x = 17: y = 19
B: a = -58.064516129032: r = -1.013416985029: x = 19: y = 22
G: a = -47.096774193548: r = -0.82199377674572: x = 22: y = 24
D: a = -36.129032258065: r = -0.63057056846247: x = 25: y = 25
E: a = -25.161290322581: r = -0.43914736017922: x = 29: y = 26
Y: a = -14.193548387097: r = -0.24772415189597: x = 34: y = 27
W: a = -3.2258064516129: r = -0.05630094361272: x = 39: y = 27
H: a = 7.741935483871: r = 0.13512226467053: x = 43: y = 27
And: a = 18.709677419355: r = 0.32654547295378: x = 48: y = 27
Y: a = 29.677419354839: r = 0.51796868123703: x = 52: y = 26
K: a = 40.645161290323: r = 0.70939188952028: x = 56: y = 24
L: a = 51.612903225806: r = 0.90081509780353: x = 60: y = 23
M: a = 62.58064516129: r = 1.0922383060868: x = 62: y = 21
H: a = 73.548387096774: r = 1.28366151437: x = 64: y = 19
O: a = 84.516129032258: r = 1.4750847226533: x = 65: y = 16
П: a = 95.483870967742: r = 1.6665079309365: x = 65: y = 14
Р: a = 106.45161290323: r = 1.8579311392198: x = 64: y = 11
C: a = 117.41935483871: r = 2.049354347503: x = 62: y = 9
T: a = 128.38709677419: r = 2.2407775557863: x = 60: y = 7
Y: a = 139.35483870968: r = 2.4322007640695: x = 56: y = 6
F: a = 150.32258064516: r = 2.6236239723528: x = 52: y = 4
X: a = 161.29032258065: r = 2.815047180636: x = 48: y = 3
C: a = 172.25806451613: r = 3.0064703889193: x = 43: y = 3
H: a = 183.22580645161: r = 3.1978935972025: x = 39: y = 3
Y: a = 194.1935483871: r = 3.3893168054858: x = 34: y = 3
B: a = 205.16129032258: r = 3.580740013769: x = 29: y = 4
Y: a = 216.12903225806: r = 3.7721632220523: x = 25: y = 5
B: a = 227.09677419355: r = 3.9635864303355: x = 22: y = 6
E: a = 238.06451612903: r = 4.1550096386188: x = 19: y = 8
Yu: a = 249.03225806452: r = 4.346432846902: x = 17: y = 11
Me: a = 260: r = 4.5378560551853: x = 15: y = 13



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 => '' ...);
  PHP phrase output in a circle in text mode
  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 />";
}

  PHP phrase output in a circle in text mode

Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Running server side scripts using PHP as an example (LAMP)

Terms: Running server side scripts using PHP as an example (LAMP)