围绕圆圈内部对齐矩形并旋转

Aligning rectangles around the inside of a circle and rotate

我有一个生成图像的程序。

它将 x 个图像围绕 x 个圆周放置。

请参阅以下当前实施的输出。

我需要所有的矩形都在圆内并且均匀放置。

请注意,在上图中,当前的实现中,一些矩形在内部,一些在外部。

我不确定我的计算有什么问题,请看下面放置矩形的代码。

 /**
 * Draw the points around a circle
 * @param  $count
 * @param  $circumference
 */
public function drawWheel($count, $circumference)
{
    /**
     * The starting angle
     */
    $angle = 0;

    /**
     * The starting step between rectangles
     */
    $step = (2 * pi()) / $count;

    /**
     * The center X of the canvas
     */
    $startX = ($this->canvas['width'] / 2);

    /**
     * The center Y of the canvas
     */
    $startY = ($this->canvas['height'] / 2);


    for ($i = 0; $i < $count; $i++) {
        /**
         * Width of rectangle
         */
        $width = 85;

        /**
         * Height of rectangle
         */
        $height = 41;

        /**
         * Rectangle X position
         */
        $x = ($startX + ($circumference / 2) * cos($angle)) - $width / 2;

        /**
         * Rectangle Y position
         */
        $y = ($startY + ($circumference / 2) * sin($angle)) - $height / 2;

        /**
         * Degrees to rotate the rectangle around the circle
         */
        $rotateAngle = atan2((($startX - ($width / 2)) - $x), (($startY - ($height)) - $y)) * 180 / pi();

        /**
         * The rectangle image
         */
        $watermark = Image::make(base_path('test.png'));

        $watermark->opacity(75);
        $watermark->resize($width, $height);
        $watermark->rotate($rotateAngle);

        $this->image->insert($watermark, 'top-left', ceil($x), ceil($y));

        /**
         * Increment the angle
         */
        $angle += $step;
    }
}

进行计算的函数部分如下。

$x = ($startX + ($circumference / 2) * cos($angle)) - $width / 2;

$y = ($startY + ($circumference / 2) * sin($angle)) - $height / 2;

$rotateAngle = atan2((($startX - ($width / 2)) - $x), (($startY - ($height)) - $y)) * 180 / pi();

旋转点是矩形的中心。

图像旋转使用:http://php.net/manual/en/function.imagerotate.php

使用以下方法绘制圆:http://php.net/manual/en/function.imagefilledarc.php

这些行是可疑的:

$x = ($startX + ($circumference / 2) * cos($angle)) - $width / 2;
$y = ($startY + ($circumference / 2) * sin($angle)) - $height / 2;

要将矩形中心放置在内半径的圆内,您必须使用这样的东西:

$x = ($startX + (($circumference - $height) / 2) * cos($angle));
$y = ($startY + (($circumference - $height) / 2) * sin($angle));

而旋转角度就是

$rotateAngle = $angle * 180 / Pi - 90; // probably $angle+90 depending on coordinate system

旋转水印的边界矩形尺寸

Fi = rotateAngle * Pi / 80 
New_Height = $width * Abs(Sin(Fi)) + $height * Abs(Cos(Fi))
New_Width = $width * Abs(Cos(Fi)) + $height * Abs(Sin(Fi))

为正确的输出更正 $x 和 $y:

$x = $x - New_Width/2
$y = $y - New_Height/2

旋转矩形后width/height会翻转,这是我没有考虑到的。

MBo 的回答对主要的 x、y 和旋转坐标有帮助。

请参阅下面的修改后的代码。

for ($i = 0; $i < $count; $i++) {
            $width = 85;
            $height = 41;

            $x = ($startX + (($circumference - $height) / 2) * cos($angle));
            $y = ($startY + (($circumference - $height) / 2) * sin($angle));

            $rotateAngle = 90 - $angle * 180 / pi(); 

            $watermark = Image::make(base_path('test.png'));

            $watermark->opacity(75);
            $watermark->resize($width, $height);
            $watermark->rotate($rotateAngle);

            $this->image->insert($watermark, 'top-left', ceil($x - ($watermark->width() / 2)), ceil($y - ($watermark->height() / 2)));

            $this->drawCircle($x, $y, 10);
            $this->drawCircle($x, $y, 10);


            $angle += $step;
        }