「角のある四角形」とはどういう意味ですか? 辺が画像の 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
);
}