Last active
June 29, 2021 08:25
-
-
Save sagittaracc/9a4e81a9dbe57beb11a9903c6a72a7cc to your computer and use it in GitHub Desktop.
Calc all the rectangles by points on the plane (Google interview)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'Point.php'; | |
require 'PointArray.php'; | |
require 'Rectangle.php'; | |
$points = new PointArray([ | |
[0, 0], [0, 1], [0, 2], [0, 3], | |
[1, 0], [1, 1], [1, 2], [1, 3], | |
[2, 0], [2, 1], [2, 2], [2, 3], | |
[3, 0], [3, 1], [3, 2], [3, 3], | |
]); | |
$rectangles = []; | |
for ($i = 0; $i < $points->count(); $i++) { | |
for ($j = $i; $j < $points->count(); $j++) { | |
$point1 = $points->index($i); | |
$point2 = $points->index($j); | |
if ($point2->verticalTo($point1) || $point2->horizontalTo($point1)) continue; | |
$rectExtraPoint1 = new Point($point1->x(), $point2->y()); | |
$rectExtraPoint2 = new Point($point2->x(), $point1->y()); | |
$isExtraPoint1 = $points->search($rectExtraPoint1); | |
$isExtraPoint2 = $points->search($rectExtraPoint2); | |
if ($isExtraPoint1 && $isExtraPoint2) { | |
$rectangle = new Rectangle($point1, $point2, $rectExtraPoint1, $rectExtraPoint2); | |
$hash = $rectangle->getHash(); | |
if (isset($rectangles[$hash])) continue; | |
$rectangles[$hash] = $rectangle; | |
} | |
} | |
} | |
echo count($rectangles); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Point | |
{ | |
private $x; | |
private $y; | |
function __construct($x, $y) | |
{ | |
$this->x = $x; | |
$this->y = $y; | |
} | |
public function x() | |
{ | |
return $this->x; | |
} | |
public function y() | |
{ | |
return $this->y; | |
} | |
public function verticalTo($point) | |
{ | |
return $this->x() === $point->x(); | |
} | |
public function horizontalTo($point) | |
{ | |
return $this->y() === $point->y(); | |
} | |
public function equalTo($point) | |
{ | |
return $this->x() === $point->x() && $this->y() === $point->y(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PointArray | |
{ | |
private $pointArray; | |
function __construct($pointArray) | |
{ | |
foreach ($pointArray as $point) { | |
$this->pointArray[] = new Point($point[0], $point[1]); | |
} | |
} | |
public function getPoints() | |
{ | |
return $this->pointArray; | |
} | |
public function count() | |
{ | |
return count($this->pointArray); | |
} | |
public function index($i) | |
{ | |
return $this->pointArray[$i]; | |
} | |
public function search($needlePoint) | |
{ | |
foreach ($this->pointArray as $point) { | |
if ($point->equalTo($needlePoint)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Rectangle | |
{ | |
private $point1; | |
private $point2; | |
private $point3; | |
private $point4; | |
function __construct($point1, $point2, $point3, $point4) | |
{ | |
$this->point1 = $point1; | |
$this->point2 = $point2; | |
$this->point3 = $point3; | |
$this->point4 = $point4; | |
} | |
public function getHash() | |
{ | |
$xs = [$this->point1->x(), $this->point2->x(), $this->point3->x(), $this->point4->x()]; | |
$ys = [$this->point1->y(), $this->point2->y(), $this->point3->y(), $this->point4->y()]; | |
$xmin = min($xs); | |
$xmax = max($xs); | |
$ymin = min($ys); | |
$ymax = max($ys); | |
return md5($xmin.$ymin.$xmax.$ymax); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment