Source for file Math.class.php
Documentation is available at Math.class.php
* This work is hereby released into the Public Domain.
* To view a copy of the public domain dedication,
* visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
* Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
require_once dirname(__FILE__ ). "/../Graph.class.php";
* @param int $style Line style
$this->style = (int) $style;
* @param int $thickness Shape thickness in pixels
$this->hide = (bool) $hide;
$this->hide = (bool) !$shape;
* @package linea21.externals
function awPoint($x, $y) {
* @param array Point location
return array($this->x, $this->y);
* Get distance to another point
return sqrt(pow($p->x - $this->x, 2) + pow($p->y - $this->y, 2));
* Move the point to another location
* @param Point A Point with the new awlocation
* @package linea21.externals
* The line slope (the m in y = mx + p)
* The y-intercept value of the line (the p in y = mx + p)
* @param $p2 Second point
* @param int $type Style of line (default to solid)
* @param int $thickness Line thickness (default to 1)
function awLine($p1 = NULL, $p2 = NULL, $type = LINE_SOLID, $thickness = 1) {
* Build a line from 4 coords
* @param int $x1 Left position
* @param int $y1 Top position
* @param int $x2 Right position
* @param int $y2 Bottom position
function build($x1, $y1, $x2, $y2) {
* Change X values of the line
* @param int $x1 Begin value
* @param int $x2 End value
function setX($x1, $x2) {
// Resets slope and origin values so they are
// recalculated when and if needed.
* Change Y values of the line
* @param int $y1 Begin value
* @param int $y2 End value
function setY($y1, $y2) {
// Resets slope and origin values so they are
// recalculated when and if needed.
* @param $p2 Second point
// Resets slope and origin values so they are
// recalculated when and if needed.
* @param array Line location
return array($this->p1, $this->p2);
$square = pow($this->p2->x - $this->p1->x, 2) + pow($this->p2->y - $this->p1->y, 2);
* Calculate the line slope
function calculateSlope() {
$slope = ($this->p1->y - $this->p2->y) / ($this->p1->x - $this->p2->x);
* Calculate the y-intercept value of the line
function calculateOrigin() {
$this->origin = $this->p1->y; // Or p2->y
$origin = ($y2 * $x1 - $y1 * $x2) / ($x1 - $x2);
* Calculate the slope and y-intercept value of the line
function calculateEquation() {
$this->calculateOrigin();
* Get the line slope value
} elseif($this->slope !== NULL) {
* Get the line y-intercept value
} elseif($this->origin !== NULL) {
$this->calculateOrigin();
* @return array An array containing the slope and y-intercept value of the line
return array($slope, $origin);
* Return the x coordinate of a point on the line
* given its y coordinate.
* @param float $y The y coordinate of the Point
* @return float $x The corresponding x coordinate
$x = ($y - $origin) / $slope;
* Return the y coordinate of a point on the line
* given its x coordinate.
* @param float $x The x coordinate of the Point
* @return float $y The corresponding y coordinate
$y = $slope * $x + $origin;
* Test if the line can be considered as a point
return ($this->p1->x === $this->p2->x and $this->p1->y === $this->p2->y);
* Test if the line is a vertical line
return ($this->p1->x === $this->p2->x);
* Test if the line is an horizontal line
return ($this->p1->y === $this->p2->y);
* Returns TRUE if the line is going all the way from the top
* to the bottom of the polygon surrounding box.
* @param $polygon Polygon A Polygon object
list ($xMin, $xMax) = $polygon->getBoxXRange();
list ($yMin, $yMax) = $polygon->getBoxYRange();
if($this->p1->y < $this->p2->y) {
$this->isOnBoxTopSide($top, $xMin, $xMax, $yMin)
$this->isOnBoxBottomSide($bottom, $xMin, $xMax, $yMax)
* Returns TRUE if the line is going all the way from the left side
* to the right side of the polygon surrounding box.
* @param $polygon Polygon A Polygon object
list ($xMin, $xMax) = $polygon->getBoxXRange();
list ($yMin, $yMax) = $polygon->getBoxYRange();
if($this->p1->x < $this->p2->x) {
$this->isOnBoxLeftSide($left, $yMin, $yMax, $xMin)
$this->isOnBoxRightSide($right, $yMin, $yMax, $xMax)
function isOnBoxTopSide($point, $xMin, $xMax, $yMin) {
function isOnBoxBottomSide($point, $xMin, $xMax, $yMax) {
function isOnBoxLeftSide($point, $yMin, $yMax, $xMin) {
function isOnBoxRightSide($point, $yMin, $yMax, $xMax) {
* A vector is a type of line
* The sense of the vector goes from $p1 to $p2.
* @package linea21.externals
* Get vector angle in radians
$width = ($this->p2->x - $this->p1->x);
$height = ($this->p2->y - $this->p1->y) * - 1;
if($width >= 0 and $height >= 0) {
return acos($width / $size);
} else if($width <= 0 and $height >= 0) {
return acos($width / $size);
if($width >= 0 and $height >= 0) {
return 2 * M_PI - acos($width / $size);
} else if($width <= 0 and $height >= 0) {
return 2 * M_PI - acos($width / $size);
* @package linea21.externals
* Set a point in the polygon
* @param int $pos Point position
function set($pos, $point) {
* Add a point at the end of the polygon
* Get a point at a position in the polygon
* @param int $pos Point position
* Count number of points in the polygon
* Returns all points in the polygon
* Returns the different lines formed by the polygon vertices
for($i = 0; $i < $count - 1; $i++ ) {
$lines[] = new Line($this->get($i), $this->get($i + 1));
$lines[] = new Line($this->get($count - 1), $this->get(0));
* Get the upper-left and lower-right points
* of the bounding box around the polygon
* @return array An array of two Point objects
for($i = 0; $i < $count; $i++ ) {
list ($x[], $y[]) = $point->getLocation();
$upperLeft = new Point(min($x), min($y));
$lowerRight = new Point(max($x), max($y));
return array($upperLeft, $lowerRight);
* Return the range of the polygon on the y axis,
* i.e. the minimum and maximum y value of any point in the polygon
list (, $yMin) = $p1->getLocation();
list (, $yMax) = $p2->getLocation();
return array($yMin, $yMax);
* Return the range of the polygon on the x axis,
* i.e. the minimum and maximum x value of any point in the polygon
list ($xMin, ) = $p1->getLocation();
list ($xMax, ) = $p2->getLocation();
return array($xMin, $xMax);
|