Beispiel #1
0
 public function crawl($x, $y)
 {
     // Set the starting position to crawl;
     $this->x = $x;
     $this->y = $y;
     $turns = 0;
     $outline = new CrawlerOutline($this->matrix->pixel($x, $y));
     do {
         switch (true) {
             // L1
             case $this->look(1):
                 $turns = 0;
                 $this->walk()->turnLeft()->walk();
                 $outline->push($this->matrix->pixel($this->x, $this->y));
                 break;
                 // L2
             // L2
             case $this->look(2):
                 $turns = 0;
                 $this->walk();
                 $outline->push($this->matrix->pixel($this->x, $this->y));
                 break;
                 // L3
             // L3
             case $this->look(3):
                 $turns = 0;
                 $this->turnRight()->walk()->turnLeft()->walk();
                 $outline->push($this->matrix->pixel($this->x, $this->y));
                 break;
                 // Turn
             // Turn
             default:
                 $turns++;
                 $this->turnRight();
         }
         // Complete condition
         if ($turns == 4 || $outline->closed()) {
             break;
         }
     } while (true);
     return $outline;
 }
Beispiel #2
0
 public function sliceByOutline(CrawlerOutline $outline)
 {
     $target = imagecreatetruecolor($outline->boundary->width(), $outline->boundary->height());
     imagefill($target, 0, 0, 0xffffff);
     $topLeft = $outline->boundary->topLeft();
     $bottomRight = $outline->boundary->bottomRight();
     for ($x = $topLeft->x(); $x <= $bottomRight->x(); $x++) {
         for ($y = $topLeft->y(); $y <= $bottomRight->y(); $y++) {
             if ($outline->contains(new Point($x, $y))) {
                 imagesetpixel($target, $x - $topLeft->x(), $y - $topLeft->y(), imagecolorat($this->img, $x, $y));
             }
         }
     }
     return self::fromResource($target);
 }