矩形自动Blit

  • 本文关键字:Blit php algorithm
  • 更新时间 :
  • 英文 :


我有一个容器矩形和元素矩形,它们都只指定了高度和宽度。使用PHP,我需要弄清楚什么位置和方向将允许元素的最有可能的位在容器内绝对没有重叠。

示例:

$container = array(5000,2000);
$element = array(300,200);

输出应该是一个"blit"数组(或对象)的数组,如下所示

$blit_object = array($x,$y,$rotation_degrees);

好吧,既然没有人回答我,我就展示一下我的"试错"解决方案,以防有人需要它。

它计算两个基本的布局选项,看哪一个有最多的,并使用它。

function MakeBlits($container,$element)
{
    $container_x = $container[0];
    $container_y = $container[1];
    $options = array();
    for($i = 0;$i<=1;$i++)
    {
        if($i == 0)
        {
            $element_x = $element[0];
            $element_y = $element[1];
        }
        else

{
        $element_x = $element[1];
        $element_y = $element[0];
    }
    $test_x = floor($container_x/$element_x);
    $test_y = floor($container_y/$element_y);
    $test_remainder_x = $container_x - $test_x*$element_x;
    $test_remainder_y = $container_y - $test_y*$element_y;
    $test_x2 = 0;
    $test_x3 = 0;
    $test_y2 = 0;
    $test_y3 = 0;
    if($test_remainder_x > $element_y)
    {
        $test_x2 = floor($test_remainder_x/$element_y);
        $test_y2 = floor($container_y/$element_x);
    }
    if($test_remainder_y > $element_x)
    {
        $test_x3 = floor($container_x/$element_y);
        $test_y3 = floor($test_remainder_y/$element_x);
    }
    $options[] = array(
        'total'=>($test_x*$test_y)+($test_x2*$test_y2)+($test_x3*$test_y3),
        'x'=>$test_x,
        'y'=>$test_y,
        'x2'=>$test_x2,
        'y2'=>$test_y2,
        'x3'=>$test_x3,
        'y3'=>$test_y3
    );
}
if($options[0]['total']>=$options[1]['total'])
{
    $option = $options[0];
    $rotate = 0;
    $width = $element[0];
    $height = $element[1];
}
else
{
    $option = $options[1];
    $rotate = -90;
    $width = $element[1];
    $height = $element[0];
}
$blit_objects = array();
for($i=0;$i<$option['x'];$i++)
{
    for($j=0;$j<$option['y'];$j++)
    {
        $blit_objects[] = array(
            'x'=>$i*$width,
            'y'=>$j*$height,
            'rotation'=>$rotate);
    }
}
for($k = 0;$k < $option['x2'];$k++)
{
    for($l = 0;$l < $option['y2'];$l++)
    {
        $blit_objects[] = array(
            'x'=>$i*$width + $k*$height,
            'y'=>$l*$width,
            'rotation'=>$rotate+90);
    }
}
for($k = 0;$k < $option['x3'];$k++)
{
    for($l = 0;$l < $option['y3'];$l++)
    {
        $blit_objects[] = array(
            'x'=>$k*$height,
            'y'=>$j*$height+$l*$width,
            'rotation'=>$rotate+90);
    }
}
return $blit_objects;

}

最新更新