+ -
当前位置:首页 → 问答吧 → [非原创]正弦函数图象

[非原创]正弦函数图象

时间:2006-12-19

来源:互联网

复制PHP内容到剪贴板
PHP代码:

<?php
Header("Content-type: image/png");
$width  = 800 ;   //图像的宽度
$height = 500 ;   //图像的高度
$pixel  = 720 ;   //画点的个数
$xOffset= 10;   //y 轴的偏移量
$yOffset= 250 ;   //x 轴的偏移量
$im     = imagecreate($width , $height);
$cream  = imagecolorallocate($im , 255 , 255 , 200);
$blue   = imagecolorallocate($im , 0 , 0 , 255);
$black  = imagecolorallocate($im , 0 , 0 , 0);
$red    = imagecolorallocate($im , 255 , 0 , 0);
$green  = imagecolorallocate($im , 0 , 255 , 0);
imageline($im,0,$yOffset,($width-15),$yOffset,$black); /* x 轴 */
imageline($im,$xOffset,0,$xOffset,$height,$black); /* y 轴 */
imagestring($im , 5 , ($xOffset + 2) , ($yOffset + 2) , "0" , $red);//设置坐标原点
imagestring($im , 5 , ($xOffset-3), 0, "^" , $red);
imagestring($im , 5 ,($width-16),($yOffset-7), ">" , $red);
imagestring($im , 5 ,($width-10),($yOffset-15), "x" , $red);
for ($i = 0 ; $i < $pixel ; $i++){
    $x  = $i+$xOffset;
    $y  = cos(deg2rad($i))*100+$yOffset;
    $z  = $yOffset-sin(deg2rad($i))*200;
    $k  = tan(deg2rad($i))*100+$yOffset;
    imagesetpixel($im , $x , $y , $blue);
    imagesetpixel($im , $x , $z , $red);
    imagesetpixel($im , $x , $k , $green);
    if($i%90==0&&$i!=0){
       imageline($im,$x,0,$x,$height,$black);
    }
}
imagepng($im);
imagedestroy($im);
?>

[ 本帖最后由 muqiao 于 2006-12-18 21:33 编辑 ]

作者: muqiao   发布时间: 2006-12-18

不错,不过曲线的连续性不够,看得出来楼主是用描点的方式画的

作者: cator   发布时间: 2006-12-18

这个我还不会

作者: leehui1983   发布时间: 2006-12-18

不用描点,那用什么,
请教,我不会撒,还没看gd2函数库,我只是改别人的,

作者: muqiao   发布时间: 2006-12-18

我增加了一个循环还是不够连续啊
复制PHP内容到剪贴板
PHP代码:

<?php
Header("Content-type: image/png");
$width  = 800 ;   //图像的宽度
$height = 500 ;   //图像的高度
$pixel  = 720 ;   //画点的个数
$xOffset= 10 ;    //y 轴的偏移量
$yOffset= 250 ;   //x 轴的偏移量
$avi    = 15 ;    //平均数
$im     = imagecreate($width , $height);
$cream  = imagecolorallocate($im , 255 , 255 , 200);
$blue   = imagecolorallocate($im , 0 , 0 , 255);
$black  = imagecolorallocate($im , 0 , 0 , 0);
$red    = imagecolorallocate($im , 255 , 0 , 0);
$green  = imagecolorallocate($im , 0 , 255 , 0);
imageline($im,0,$yOffset,($width-15),$yOffset,$black); /* x 轴 */
imageline($im,$xOffset,0,$xOffset,$height,$black); /* y 轴 */
imagestring($im , 5 , ($xOffset + 2) , ($yOffset + 2) , "0" , $red);//设置坐标原点
imagestring($im , 5 , ($xOffset - 3) , 0, "^" , $red);
imagestring($im , 5 , ($width - 16) , ($yOffset - 7 ), ">" , $red);
imagestring($im , 5 , ($width - 10) , ($yOffset - 15), "x" , $red);
for($j = 0;$j < $pixel ; $j++){
    for ($k = 0 ; $k < $avi ; $k++){
        $i  = $k/$avi+($j-1);
        $x  = $i+$xOffset;
        $y  = cos(deg2rad($i))*100+$yOffset;
        $z  = $yOffset-sin(deg2rad($i))*200;
        $k  = tan(deg2rad($i))*100+$yOffset;
        imagesetpixel($im , $x , $y , $blue);
        imagesetpixel($im , $x , $z , $red);
        imagesetpixel($im , $x , $k , $green);
        if($i%90==0&&$i!=0){
           imageline($im,$x,0,$x,$height,$black);
        }
    }
}
imagepng($im);
imagedestroy($im);
?>

作者: muqiao   发布时间: 2006-12-19

学习一下图象函数了!

作者: Nickboy   发布时间: 2006-12-19

���牲c
那��好�c��?

作者: w992   发布时间: 2006-12-19

输两点也会在同一个位置,不是一样的效果吗?

作者: muqiao   发布时间: 2006-12-19

用描点画类 sin 函数曲线,如果振幅不是太大的话,图象的连续性还是很好的.

如果要画振幅比较大的类 sin 函数曲线,就得考虑用另外的一种算法了,用画小段的线代替点.

作者: hy0kl   发布时间: 2006-12-19

呵呵,不错

作者: xiaojia   发布时间: 2006-12-22

有没有办法让图像看起来是连续的?
不要看起来断断续续的。

作者: jefsun   发布时间: 2006-12-28

要想让点连续不是很简单么,只要把当前点和上一点用线连接起来就可以了。虽然有些锯齿,但是总比点好多了。
复制内容到剪贴板
代码:
<?php
Header("Content-type: image/png");
$width  = 800 ;   //图像的宽度
$height = 500 ;   //图像的高度
$pixel  = 720 ;   //画点的个数
$xOffset= 10;   //y 轴的偏移量
$yOffset= 250 ;   //x 轴的偏移量
$im     = imagecreate($width , $height);
$cream  = imagecolorallocate($im , 255 , 255 , 200);
$blue   = imagecolorallocate($im , 0 , 0 , 255);
$black  = imagecolorallocate($im , 0 , 0 , 0);
$red    = imagecolorallocate($im , 255 , 0 , 0);
$green  = imagecolorallocate($im , 0 , 255 , 0);
imageline($im,0,$yOffset,($width-15),$yOffset,$black); /* x 轴 */
imageline($im,$xOffset,0,$xOffset,$height,$black); /* y 轴 */
imagestring($im , 5 , ($xOffset + 2) , ($yOffset + 2) , "0" , $red);//设置坐标原点
imagestring($im , 5 , ($xOffset-3), 0, "^" , $red);
imagestring($im , 5 ,($width-16),($yOffset-7), ">" , $red);
imagestring($im , 5 ,($width-10),($yOffset-15), "x" , $red);
for ($i = 0 ; $i < $pixel ; $i++){
        if ($i != 0) $lastX = $x;
        if ($i != 0) $lastY = $y;
        if ($i != 0) $lastZ = $z;
        if ($i != 0) $lastK = $k;
    $x  = $i+$xOffset;
    $y  = cos(deg2rad($i))*100+$yOffset;
    $z  = $yOffset-sin(deg2rad($i))*200;
    $k  = tan(deg2rad($i))*100+$yOffset;
    imagesetpixel($im , $x , $y , $blue);
        if ($i != 0) imageline($im, $x, $y, $lastX, $lastY, $blue);
    imagesetpixel($im , $x , $z , $red);
        if ($i != 0) imageline($im, $x, $z, $lastX, $lastZ, $red);
    imagesetpixel($im , $x , $k , $green);
        if ($i != 0) imageline($im, $x, $k, $lastX, $lastK, $green);
    if($i%90==0&&$i!=0){
       imageline($im,$x,0,$x,$height,$black);
    }
}
imagepng($im);
imagedestroy($im);
?>

作者: dgwxx   发布时间: 2007-01-01

楼上高见,很好,

作者: muqiao   发布时间: 2007-01-01

只要把:
$im     = imagecreate($width , $height);
$cream  = imagecolorallocate($im , 255 , 255 , 200);

替换成:
$im        = imagecreatetruecolor($width , $height);
imageantialias($im,true);
$cream  = imagecolorallocate($im , 255 , 255 , 200);
imagefill($im, 0, 0, $cream);

即可实现抗锯齿,做出来的图好看多了。

作者: dgwxx   发布时间: 2007-01-01

thank you very much

作者: muqiao   发布时间: 2007-01-01

不错了

作者: dzjzmj   发布时间: 2007-01-02