「斜め矩形」とはどういう意味ですか?あなたは、辺が画像のx方向とy方向に垂直ではない長方形を意味しますか?
imagefilledrectangle()
関数を使用するには、長方形の範囲を定義する2つの点の座標を指定する必要があります。私はあなたが角度を回転している矩形を描きたい場合、おそらく提供したいと思っていたでしょう
- 矩形の幅と高さ
- 矩形の中心(または識別された頂点
- 矩形を回転させる角度。
幅と高さ以外のこれらのそれぞれについて言及します。
私が関数を作りたかったとします。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
);
}
はああ、私は幅と高さを残したんだろう。答えに感謝します。 –