Created
June 6, 2011 22:43
-
-
Save croensch/1011279 to your computer and use it in GitHub Desktop.
PHP Geometry
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 | |
// config | |
error_reporting(-1); | |
ini_set('display_errors', 1); | |
header('Content-Type: text/plain; charset=utf-8'); | |
// library | |
class Edge { | |
protected $_length; | |
public function __construct($length){ | |
if( $length > 0 ){ | |
$this->_length = $length; | |
} else { | |
throw new Exception("Edge length must be positive"); | |
} | |
} | |
public function getLength() | |
{ | |
return $this->_length; | |
} | |
} | |
abstract class Polygon { | |
protected $_edges = array(); | |
public function __construct(array $edges) { | |
foreach( $edges as $edge ){ | |
if( $edge instanceof Edge ){ | |
$this->_edges[] = $edge; | |
} else { | |
throw new Exception("Edge must be an instance of 'Edge'"); | |
} | |
} | |
} | |
public abstract function getArea(); | |
} | |
class Triangle extends Polygon { | |
public function __construct(array $edges){ | |
if( count($edges) === 3 ){ | |
parent::__construct($edges); | |
} else { | |
throw new Exception('Triangle must have three edges'); | |
} | |
} | |
public function getArea(){ | |
list($a, $b, $c) = $this->_edges; | |
$a = $a->getLength(); | |
$b = $b->getLength(); | |
$c = $c->getLength(); | |
$s = $a + $b + $c; | |
return sqrt( ($s-$a)*($s-$b)*($s-$c) ); | |
} | |
} | |
class Quadrilateral extends Polygon { | |
public function __construct(array $edges) { | |
if( count($edges) === 4 ){ | |
parent::__construct($edges); | |
} else { | |
throw new Exception('Quadliteral must have four edges'); | |
} | |
} | |
public function getArea(){ | |
return "Can't quite figure that out"; | |
} | |
} | |
class Rectangle extends Quadrilateral { | |
public function __construct(array $edges) { | |
if( count($edges) === 2 ){ | |
foreach( $edges as $edge ){ | |
$edges[] = $edge; | |
} | |
parent::__construct($edges); | |
} else { | |
throw new Exception('Rectangle must have two edges'); | |
} | |
} | |
public function getArea(){ | |
list($a, $b) = $this->_edges; | |
$a = $a->getLength(); | |
$b = $b->getLength(); | |
return ($a*$b); | |
} | |
} | |
class Square extends Quadrilateral { | |
public function __construct(array $edges) { | |
if( count($edges) === 1 ){ | |
foreach( $edges as $edge ){ | |
$edges[] = $edge; | |
$edges[] = $edge; | |
$edges[] = $edge; | |
} | |
parent::__construct($edges); | |
} else { | |
throw new Exception('Square must have one edge'); | |
} | |
} | |
public function getArea(){ | |
list($a) = $this->_edges; | |
$a = $a->getLength(); | |
return ($a*$a); | |
} | |
} | |
// tests | |
try { | |
$edge = new Edge(0); | |
echo "Impossible edge with 0 length\n"; | |
} catch( Exception $exception ){ | |
} | |
$edge = new Edge(1); | |
try{ | |
$triangle = new Triangle(array( | |
new Edge(1), | |
new Edge(2) | |
)); | |
echo "Impossible triangle with 2 edges\n"; | |
} catch( Exception $exception ){ | |
} | |
try{ | |
$triangle = new Triangle(array( | |
new Edge(1), | |
new Edge(2), | |
new Edge(3), | |
new Edge(4) | |
)); | |
echo "Impossible triangle with 4 edges\n"; | |
} catch( Exception $exception ){ | |
} | |
$triangle = new Triangle(array( | |
new Edge(1), | |
new Edge(2), | |
new Edge(3) | |
)); | |
echo "Triangle (1,2,3) "; | |
echo $triangle->getArea(); | |
echo "\n"; | |
try{ | |
$quadrilateral = new Quadrilateral(array( | |
new Edge(1), | |
new Edge(2), | |
new Edge(3) | |
)); | |
echo "Impossible quadrilateral with 3 edges\n"; | |
} catch( Exception $exception ){ | |
} | |
try{ | |
$quadrilateral = new Quadrilateral(array( | |
new Edge(1), | |
new Edge(2), | |
new Edge(3), | |
new Edge(4), | |
new Edge(5) | |
)); | |
echo "Impossible quadrilateral with 5 edges\n"; | |
} catch( Exception $exception ){ | |
} | |
$quadrilateral = new Quadrilateral(array( | |
new Edge(1), | |
new Edge(2), | |
new Edge(3), | |
new Edge(4) | |
)); | |
echo "Quadrilateral (1,2,3,4) "; | |
echo $quadrilateral->getArea(); | |
echo "\n"; | |
try { | |
$rectangle = new Rectangle(array( | |
new Edge(1) | |
)); | |
echo "Impossible rectangle with 1 edge\n"; | |
} catch( Exception $e ){ | |
} | |
try { | |
$rectangle = new Rectangle(array( | |
new Edge(1), | |
new Edge(2), | |
new Edge(3) | |
)); | |
echo "Impossible rectangle with 3 edges\n"; | |
} catch( Exception $e ){ | |
} | |
$rectangle = new Rectangle(array( | |
new Edge(4), | |
new Edge(5) | |
)); | |
echo "Rectangle (4,5) "; | |
echo $rectangle->getArea(); | |
echo "\n"; | |
try { | |
$square = new Square(array( | |
)); | |
echo "Impossible square with 0 edges\n"; | |
} catch( Exception $e ){ | |
} | |
try { | |
$square = new Square(array( | |
new Edge(1), | |
new Edge(2) | |
)); | |
echo "Impossible square with 2 edges\n"; | |
} catch( Exception $e ){ | |
} | |
$square = new Square(array( | |
new Edge(3) | |
)); | |
echo "Square (3) "; | |
echo $rectangle->getArea(); | |
echo "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment