0

i have this php program which takes in an angle as a get parameter and prints a segment of a circle with that angle:

<?php
$size = 600;
$img = imagecreatetruecolor($size, $size);

$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);

imagefilledrectangle($img,0,0,$size,$size,$white);

function Vector($palette,$startx,$starty,$angle,$length,$colour){
    $angle = deg2rad($angle);
    $endx = $startx+cos($angle)*$length;
    $endy = $starty-sin($angle)*$length;
    return(imageline($palette,$startx,$starty,$endx,$endy,$colour));
}

$ang = 0;
while($ang <= $_GET['angle']){
Vector($img,$size/2,$size/2,$ang,200,$black);
$ang += 1;
}


header("Content-type: image/png");
imagepng($img);

?>

The function vector basically draws a line with the given parameters. So i loop through a number of times and then each time i loop through i increase the angle by 1. And then i call the vector function which basically draws a sort of circle segment with the specified angle.

But when i wish to draw another sector of the circle starting at the end point of the previous circle, it overlaps! By the way, here's the code:

<?php
$size = 600;
$img = imagecreatetruecolor($size, $size);

$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
$blue = imagecolorallocate($img, 0, 0, 255);

imagefilledrectangle($img,0,0,$size,$size,$white);

function Vector($palette,$startx,$starty,$angle,$length,$colour){
    $angle = deg2rad($angle);
    $endx = $startx+cos($angle)*$length;
    $endy = $starty-sin($angle)*$length;
    return(imageline($palette,$startx,$starty,$endx,$endy,$colour));
}

$int = 0;
while($int <= $_GET['angle']){
Vector($img,$size/2,$size/2,$int,200,$black);
$int += 0.01;
}

$int = 0;
while($int <= $_GET['angle']){
Vector($img,$size/2,$size/2,$int,200,$blue);
$int += 0.01;
}


header("Content-type: image/png");
imagepng($img);

?>

In the above code, i expect to draw a circle sector with an angle and then draw another sector with the same angle but in blue color.

I want the 2nd sector to start where the first sector ended, but it overlaps?

So how do i make it start where the previous one stopped?

4

2 回答 2

1

你说,你有一个功能来绘制一个一定角度的矢量,对吧?然后你可以循环 360 次,每个循环将角度增加 1 度并绘制矢量。你会得到一个圆圈。

对于饼图,您所要做的就是根据段的角度以特定间隔更改颜色...

如果您正在使用投票系统,这里是 PHP 图像生成器的完整源代码,它接受n多个 QueryString 参数并制作一个包含所有这些 QueryString 参数的饼图,并为它们添加一个图例:

<?php
$size = 600;
$img = imagecreatetruecolor($size, $size);

$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);

imagefilledrectangle($img,0,0,$size,$size,$white);

function Vector($palette,$startx,$starty,$angle,$length,$colour){
    $angle = deg2rad($angle);
    $endx = $startx+cos($angle)*$length;
    $endy = $starty-sin($angle)*$length;
    return(imageline($palette,$startx,$starty,$endx,$endy,$colour));
}

$sum__ = array_sum($_GET);
$items = array();
foreach($_GET as $key => $value){
$items[$key] = ($value/$sum__)*360;
}

$items2 = array();

$cur = 0;
foreach($items as $key => $value){
$cur += $value;
$items2[$key]=$cur;
}

$colors = array();
foreach($items as $key => $value){
while(array_key_exists($key,$colors) == False){
$tempcolor = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255));
if(in_array($tempcolor,$colors) == False){
$colors[$key] = $tempcolor;
}
}
}

$int = 0;
foreach($items2 as $key => $value){
while($int <= $value){
Vector($img,$size/2,$size/2,$int,200,$colors[$key]);
$int += 0.01;
}
}

$container = 10;
foreach($items2 as $key => $value){
imagefilledrectangle($img, 4, $container, 50, $container+15, $colors[$key]);
imagestring($img,5,4+60,$container,$key,$black);
$container += 20;
}

header("Content-type: image/png");
imagepng($img);

?>

希望有帮助...

于 2013-08-14T01:24:52.403 回答
0

仅供参考,您只需更改两行原始代码即可执行您想要的操作。删除第二$int = 0;行并更改下一行,以便:

$int = 0;
while($int <= $_GET['angle']){
Vector($img,$size/2,$size/2,$int,200,$blue);
$int += 0.01;
}

变成:

//delete '$int = 0;'
while($int <= ($_GET['angle']) * 2){
Vector($img,$size/2,$size/2,$int,200,$blue);
$int += 0.01;
}

这不是一个通用的解决方案,但希望能让你看到你最初做错了什么。

于 2013-08-14T01:43:25.690 回答