0

PHPでGDを使用して斜めの長方形を描く簡単な方法があるのだろうか?
関数を使用できることはわかっていimagefilledpolygonますが、すべてのポイントを手動で計算する必要があることを考えると、少し注意が必要です。

の改良版は次のimagefilledrectangleようになると思います。

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

$centerXおよびは、長方形$centerYの中心点の座標です。
またはそれに類似したもの。

4

1 に答える 1

2

「角のある四角形」とはどういう意味ですか? 辺が画像の x 方向と y 方向に垂直ではない長方形を意味しますか?

この関数を使用するimagefilledrectangle()には、長方形の範囲を定義する 2 点の座標を指定する必要があります。ある角度で回転した長方形を描きたい場合は、おそらく提供したいと思うでしょう

  • 長方形の幅と高さ
  • 四角形の中心 (または四角形の明確な頂点のような別の指定された点)
  • 四角形を回転させる角度。
幅と高さを除いて、これらのそれぞれについて言及しています。

関数を作りたいとしますimagefilledrotatedrectangle($img, $centerX, $centerY, $width, $height, $angle, $color)imagefilledpolygon()おそらく、四角形の 4 つの頂点を計算してから、それらの 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 に答える