Skip to content

Commit

Permalink
optimization - save map only after parsed all files instead of after …
Browse files Browse the repository at this point in the history
…each point
  • Loading branch information
sarven committed Jul 21, 2017
1 parent b808652 commit bb531cf
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 29 deletions.
57 changes: 30 additions & 27 deletions src/Map/Drawer.php
Expand Up @@ -27,60 +27,63 @@ final class Drawer implements DrawerInterface
*/
private $pointCalculator;

/**
* @var resource
*/
private $image;

/**
* Drawer constructor.
* @param PointCalculator $pointCalculator
*/
public function __construct(PointCalculator $pointCalculator)
{
$this->pointCalculator = $pointCalculator;
$this->image = $this->getImage();
}

/**
* @param Data $data
* {@inheritdoc}
*/
public function draw(Data $data): void
public function saveMap($map): void
{
$points = $this->pointCalculator->calc($data);
$this->drawPoints($points);
imagejpeg($map, self::MAP_FILE);
}

/**
* @param Point[] $points
* {@inheritdoc}
*/
private function drawPoints(array $points): void
public function createMap()
{
foreach ($points as $point) {
$this->drawPoint($point);
}
$image = imagecreatetruecolor(self::WIDTH, self::HEIGHT);
$white = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $white);
imagejpeg($image, self::MAP_FILE);

return $image;
}

/**
* @param Point $point
* {@inheritdoc}
*/
private function drawPoint(Point $point): void
public function draw($map, Data $data): void
{
$black = imagecolorallocate($this->image, self::COLOR['R'], self::COLOR['G'], self::COLOR['B']);
imagefilledarc($this->image, self::RATIO * $point->getX(), self::RATIO * $point->getY(), 1, 1, 0, 360, $black, IMG_ARC_PIE);
imagejpeg($this->image, self::MAP_FILE);
$points = $this->pointCalculator->calc($data);
$this->drawPoints($map, $points);
}

/**
* @return resource
* @param resource $map
* @param Point[] $points
*/
private function getImage()
private function drawPoints($map, array $points): void
{
$image = imagecreatetruecolor(self::WIDTH, self::HEIGHT);
$white = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $white);
imagejpeg($image, self::MAP_FILE);
foreach ($points as $point) {
$this->drawPoint($map, $point);
}
}

return $image;
/**
* @param resource $map
* @param Point $point
*/
private function drawPoint($map, Point $point): void
{
$black = imagecolorallocate($map, self::COLOR['R'], self::COLOR['G'], self::COLOR['B']);
imagefilledarc($map, self::RATIO * $point->getX(), self::RATIO * $point->getY(), 1, 1, 0, 360, $black, IMG_ARC_PIE);
}
}
13 changes: 12 additions & 1 deletion src/Map/DrawerInterface.php
Expand Up @@ -11,7 +11,18 @@
interface DrawerInterface
{
/**
* @return resource
*/
public function createMap();

/**
* @param resource $map
* @param Data $data
*/
public function draw(Data $data): void;
public function draw($map, Data $data): void;

/**
* @param resource $map
*/
public function saveMap($map);
}
8 changes: 7 additions & 1 deletion src/Solver/Solver.php
Expand Up @@ -62,16 +62,22 @@ public function solve(): void

private function processFiles(): void
{
$startTime = time();
$fileIterator = FileHandler::createIterator();
$count = FileHandler::getCount($fileIterator);
$map = $this->drawer->createMap();
$i = 1;

foreach ($fileIterator as $file) {
$data = $this->parseContent($file->getFileName());
$this->drawer->draw($data);
$this->drawer->draw($map, $data);
$this->output->writeLn($i.' of '.$count.' files processed.');
$i++;
}

$this->drawer->saveMap($map);
$duration = time() - $startTime;
$this->output->writeLn('Parsed '.($i-1).' files in '.$duration.' seconds');
}

/**
Expand Down

0 comments on commit bb531cf

Please sign in to comment.