Last active
August 29, 2015 13:55
-
-
Save fintara/8774863 to your computer and use it in GitHub Desktop.
Very, very basic Circle object.
This file contains hidden or 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 Circle { | |
private $_center; | |
private $_radius; | |
public function __construct(Point $center, $radius) { | |
$this->_center = $center; | |
$this->_radius = $radius; | |
} | |
/** | |
* Checks if a point is inside this circle. | |
* Credit: http://stackoverflow.com/a/7227057/1365831 | |
* | |
* @param Point $point | |
* @return bool | |
*/ | |
public function isPointInside(Point $point) { | |
$dx = abs($point->x - $this->_center->x); | |
$dy = abs($point->y - $this->_center->y); | |
if($dx + $dy <= $this->_radius) | |
return true; | |
if($dx > $this->_radius) | |
return false; | |
if($dy > $this->_radius) | |
return false; | |
if($dx * $dx + $dy * $dy <= $this->_radius * $this->_radius) | |
return true; | |
return false; | |
} | |
} |
This file contains hidden or 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 { | |
public $x; | |
public $y; | |
public function __construct($x, $y) { | |
$this->x = $x; | |
$this->y = $y; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment