Пример #1
0
 public function create($array)
 {
     // test conditions
     // TODO: test min/max
     // test area
     if ($this->minArea * ($this->minWidth * $this->minHeight) >= $this->maxArea * ($this->maxWidth * $this->maxHeight)) {
         throw new Exception('Min/max area overlap, please adjust settings');
     }
     // create matrix
     $this->matrix = Math_Matrix::makeMatrix($this->ysize, $this->xsize, -1);
     // variable to define whether filling is active
     $fill = false;
     // return array
     $positions = array();
     foreach ($array as $k => $v) {
         // get next empty space
         $pos = $this->searchNextPosition();
         // fill horizontal?
         if ($pos[1] == 0) {
             // are we at the top?
             if ($pos[0] != 0) {
                 // are we not at [0,0] ?
                 if (rand(1, $this->fillTopProbability) == 1) {
                     // roll a dice for top position fill
                     // calculate fill position
                     $fill = $this->searchXBoundFrom($pos[0]) + $this->minWidth;
                     if ($this->debug) {
                         echo 'fill from top x=' . $fill . '<br/>';
                     }
                 } else {
                     $fill = false;
                 }
             }
         } elseif ($fill === false) {
             // fill from halfway?
             if (rand(1, $this->fillHalfwayProbability) == 1) {
                 // roll a dice halfway position fill
                 // calculate fill position
                 $fill = $this->searchXBoundFrom($pos[0]) + $this->minWidth;
                 if ($this->debug) {
                     echo 'fill x=' . $fill . '<br/>';
                 }
             }
         }
         // get dimensions
         $rand = $this->randomDimensions();
         $randWidth = $rand['width'];
         $randHeight = $rand['height'];
         if ($fill !== false) {
             $randWidth = $fill - $pos[0];
         }
         // height available?
         $heightAvailable = $this->availableHeightFrom($pos[0], $pos[1]);
         if ($heightAvailable <= $this->minHeight) {
             $height = $heightAvailable;
         } else {
             if ($heightAvailable <= $this->minHeight * 2) {
                 $height = $heightAvailable;
             } else {
                 $height = min($randHeight, $heightAvailable - $this->minHeight);
             }
         }
         // random width
         $width = $randWidth;
         // debug
         if ($this->debug) {
             echo $k . ': ' . $height . '(' . $heightAvailable . ')x' . $width . ' @ [' . $pos[0] . ', ' . $pos[1] . ']<br/>';
         }
         // set ids
         for ($x = $pos[0]; $x < $width + $pos[0]; $x++) {
             for ($y = $pos[1]; $y < $height + $pos[1]; $y++) {
                 $this->matrix->setElement($y, $x, $k);
             }
         }
         // set item position
         $positions[$k] = array('x' => $pos[0] * $this->multiplier, 'y' => $pos[1] * $this->multiplier, 'w' => $width * $this->multiplier, 'h' => $height * $this->multiplier);
     }
     // debug: print matrix
     if ($this->debug) {
         echo '<pre>' . $this->matrix->toString('%3.0f') . '</pre>';
         die;
     }
     // return array
     return $positions;
 }