Skip to content

Instantly share code, notes, and snippets.

@fintara
Last active August 29, 2015 13:55
Show Gist options
  • Save fintara/8774863 to your computer and use it in GitHub Desktop.
Save fintara/8774863 to your computer and use it in GitHub Desktop.
Very, very basic Circle object.
<?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;
}
}
<?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