要绘制正弦图,可以使用PHP的GD库来进行绘图操作。以下是一个简单的示例代码:
<?php
// 创建一个 800x400 的画布
$width = 800;
$height = 400;
$image = imagecreatetruecolor($width, $height);
// 设置背景色为白色
$bgColor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgColor);
// 设置坐标轴颜色为黑色
$axisColor = imagecolorallocate($image, 0, 0, 0);
// 绘制坐标轴
imageline($image, 0, $height/2, $width, $height/2, $axisColor); // x轴
imageline($image, $width/2, 0, $width/2, $height, $axisColor); // y轴
// 绘制正弦曲线
$sinColor = imagecolorallocate($image, 255, 0, 0);
$amplitude = 100; // 振幅
$frequency = 0.02; // 频率
$phase = 0; // 相位
for ($x = 0; $x < $width; $x++) {
$y = $height/2 - $amplitude * sin($frequency * ($x - $width/2) + $phase);
imagesetpixel($image, $x, $y, $sinColor);
}
// 输出图像
header('Content-type: image/png');
imagepng($image);
// 释放内存
imagedestroy($image);
?>
你可以将上述代码保存为一个php文件,并在浏览器中访问该文件,就能看到绘制的正弦图。
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站