PHP GD将坐标从Polar转换为笛卡尔坐标

时间:2015-12-07 17:18:57

标签: php gd cartesian

我想在图表的每个部分附近或上方显示百分比。

我是否必须将坐标从Polar转换为笛卡儿?

要将极坐标(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);

0 个答案:

没有答案