用PHP画一个有一定角度的矩形

发布于 2024-12-05 06:26:23 字数 587 浏览 1 评论 0原文

我想用PHP画一个一定角度的矩形。我知道你可以用 PHP 使用 imagefilledrectangle 绘制矩形,但如何绘制一个角度。

public function drawshelf($x1, $y1, $x2, $y2, $width, $angle = 'false'){
    imagesetthickness ( $this->canvas, 1 );
    for ($i=0; $i < $width; $i++){ //HORIZONTAL
        imageline( $this->canvas, $x1, $y1, $x2, $y2, $this->color );
        $y1++; $y2++;
        if( $angle == 'true' ){ $x1--; $x2--; }
    }
}

我编写了这个函数来使用线条和循环来绘制它,但它并不像红色框那样正确出现。

有人可以告诉我我做错了什么吗?你甚至可以这样画吗?

在此处输入图像描述

I would like to draw a rectangle at an angle with PHP. I know that you can draw rectangles with PHP using imagefilledrectangle but how to draw it an an angle.

public function drawshelf($x1, $y1, $x2, $y2, $width, $angle = 'false'){
    imagesetthickness ( $this->canvas, 1 );
    for ($i=0; $i < $width; $i++){ //HORIZONTAL
        imageline( $this->canvas, $x1, $y1, $x2, $y2, $this->color );
        $y1++; $y2++;
        if( $angle == 'true' ){ $x1--; $x2--; }
    }
}

I wrote this function to draw it using lines and a loop but its not coming up right like the red box.

Can someone please tell me what am i doing wrong? And can you even draw it like that?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画离情绘悲伤 2024-12-12 06:26:23

使用 imagepolygon()imagefilledpolygon() 使用 GD 绘制非矩形形状。您可能需要复习一些三角学知识来弄清楚如何定位点以获得直角。

Use imagepolygon() or imagefilledpolygon() to draw non-rectangular shapes using GD. You may need to review a bit of trigonometry to figure out how to position the points to get right-angle corners.

雪落纷纷 2024-12-12 06:26:23

我建议使用内置的 imagerotate 以及您的矩形使用imagefilledrectangle创建。

下面是一个示例,创建一个旋转 30 度的 20x100 红色矩形:

$width = 20;
$height = 100;
$angle = 30;
$im = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($im, 255, 255, 255);
$red = imagecolorallocate($im, 255, 0, 0);

// Draw a red rectangle
imagefilledrectangle($im, 0, 0, $width, $height, $red);
// Rotate and fill out background with white
$im = imagerotate($im, $angle, $white);

I'd suggest using the built in imagerotate along with a rectangle you've created with imagefilledrectangle.

Here's an example, creating a 20x100 red rectangle rotated 30 degrees:

$width = 20;
$height = 100;
$angle = 30;
$im = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($im, 255, 255, 255);
$red = imagecolorallocate($im, 255, 0, 0);

// Draw a red rectangle
imagefilledrectangle($im, 0, 0, $width, $height, $red);
// Rotate and fill out background with white
$im = imagerotate($im, $angle, $white);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文