0

我想知道是否有一种简单的方法可以在 php 中使用 GD 绘制一个有角度的矩形?
我知道我可以使用该imagefilledpolygon功能,但考虑到您应该手动计算所有点,这有点棘手。

我认为改革后的版本imagefilledrectangle是这样的:

imagefilledrectangle($img,$centerX,$centerY,$angle,$color);

Where$centerX$centerY将是矩形中心点的坐标。
或任何类似的东西。

4

1 回答 1

2

“有角的矩形”是什么意思?你的意思是一个矩形,其边不垂直于图像的 x 和 y 方向?

要使用该imagefilledrectangle()函数,您必须提供定义矩形范围的两个点的坐标。我原以为如果你想绘制一个旋转了一个角度的矩形,那么你可能想要提供

  • 矩形的宽度和高度
  • 矩形的中心(或另一个指定点,如矩形的一个显着顶点)
  • 矩形要旋转的角度。
你提到了除了宽度和高度之外的每一个。

假设我想做一个函数imagefilledrotatedrectangle($img, $centerX, $centerY, $width, $height, $angle, $color)。我可能会让它计算矩形的 4 个顶点,然后调用imagefilledpolygon()传递这 4 个点。在伪代码中:

(假设我的顶点标记为 1、2、3 和 4,顺时针旋转。我可以将它们表示为整数对,得到整数、$x1$y1$x2$y2$x3$y3。)$x4$y4

function imagefilledrotatedrectangle( $img,
                                      $centerX, $centerY,
                                      $width, $height,
                                      $angle,
                                      $color
                                      ) {
    // First calculate $x1 and $y1. You may want to apply
    // round() to the results of the calculations.
    $x1 = (-$width  * cos($angle) / 2) + $centerX;
    $y1 = (-$height * sin($angle) / 2) + $centerY;
    // Then calculate $x2, $y2, $x4 and $y4 using similar formulae. (Not shown)
    // To calculate $x3 and $y3, you can use similar formulae again, *or*
    // if you are using round() to obtain integer points, you should probably
    // calculate the vectors ($x1, $y1) -> ($x2, $y2) and ($x1, $y1) -> ($x3, $y3)
    // and add them both to ($x1, $y1) (so that you do not occasionally obtain
    // a wonky rectangle as a result of rounding error). (Not shown)
    imagefilledpolygon( $img,
                        array($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4),
                        4,
                        $color
                        );
}
于 2012-01-14T09:53:47.417 回答