PHP GD конвертировать координаты из полярных в декартовы

Я хочу показать проценты около или над каждой частью графика.

Нужно ли преобразовывать координаты из полярных в декартовы?

Для преобразования полярных координат (r, θ) в прямоугольные координаты (x, y) Я использовал следующее преобразование:

x = r * cos( θ )
y = r * sin( θ )

на этом графике

$x = $radius * cos( $Arc ); $y = $radius * sin( $Arc );

Но результаты не совпадают (значения процентов не близки к каждой отдельной части диаграммы), где я ошибаюсь?

Как я могу это исправить?

<?php
$W = 500; $H = 500;
$Image = imagecreate($W,$H);
$colors = array();
imagecolorallocate($Image,150,150,150); // grey background
$colors[0] = imagecolorallocate($Image,255,0,0); // red
$colors[1] = imagecolorallocate($Image,100,255,10); // green
$colors[2] = imagecolorallocate($Image,0,0,255); // bleu
$colors[3] = imagecolorallocate($Image,255,255,0); // yellow

$Country = array('England', 'Northern Ireland', 'Scotland', 'Wales');
$Population = array(53012456,1810863,5295403,3063456);
$TotPopulation = array_sum($Population);

$radius = 300; $StarAngle = 0;

foreach($Population  as $index => $V )
{
$Arc = (360 * $V) / $TotPopulation;
$Percentage = (100 * $V) / $TotPopulation;

imagefilledarc(
$Image,
$W / 2, $H / 2, //  center x,y
$radius,
$radius,
$StarAngle,
($StarAngle + $Arc), // end arc
$colors[$index],
IMG_ARC_PIE
);

$x = $radius * cos($Arc);
$y = $radius * sin($Arc);
imagestring($Image, 15, 5, 30 + $index*15 , 'x= '.number_format($x, 2, '.', '').' y='.number_format($y, 2, '.', '').' Country='.$Country[$index].' %='. number_format($Percentage, 2, '.', '').' ' , $colors[$index]);

$StarAngle += $Arc;
imagestring($Image, 15, 5, 430 + $index*15 , 'Country='.$Country[$index].' Population= $V %='.number_format($Percentage, 2, '.', '').' ' , $colors[$index]);
}
imagestring($Image, 5, 35, 10 , 'The population of the United Kingdom year 2011' , $colors[3]);
imagepng($Image);

2

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …