From afb84e091c2b7e7e85e4d99e1bc7df946682c1dc Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 25 Dec 2015 13:52:06 +0100 Subject: [PATCH] Implementing basic shape functionalities. --- Math/Shape/D2/Circle.php | 80 +++++++++++++ Math/Shape/D2/Ellipse.php | 62 ++++++++++ Math/Shape/D2/Quadrilateral.php | 7 -- Math/Shape/D2/Rectangle.php | 68 +++++++++++ Math/Shape/D2/Shape2DInterface.php | 155 ------------------------ Math/Shape/D2/Square.php | 0 Math/Shape/D2/Trapezoid.php | 184 +++++++++++++++++++++++++++++ Math/Shape/D2/Triangle.php | 75 ++++++++++++ Math/Shape/D3/Cone.php | 84 +++++++++++++ Math/Shape/D3/Cube.php | 0 Math/Shape/D3/Cylinder.php | 68 +++++++++++ Math/Shape/D3/Pyramid.php | 0 Math/Shape/D3/Sphere.php | 66 ++++++++++- Math/Shape/D3/SquarePyramid.php | 0 Math/Shape/D3/Tetrahedron.php | 65 ++++++++++ 15 files changed, 749 insertions(+), 165 deletions(-) delete mode 100644 Math/Shape/D2/Shape2DInterface.php delete mode 100644 Math/Shape/D2/Square.php delete mode 100644 Math/Shape/D3/Cube.php delete mode 100644 Math/Shape/D3/Pyramid.php delete mode 100644 Math/Shape/D3/SquarePyramid.php diff --git a/Math/Shape/D2/Circle.php b/Math/Shape/D2/Circle.php index e69de29bb..abd933b3f 100644 --- a/Math/Shape/D2/Circle.php +++ b/Math/Shape/D2/Circle.php @@ -0,0 +1,80 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Math\Algebra; + +class Circle +{ + + /** + * Area + * + * @param float $r Radius + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getArea(\float $r) + { + return pi() * $r ** 2; + } + + /** + * Circumference + * + * @param float $r Radius + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getCircumference(\float $r) + { + return 2 * pi() * $r; + } + + /** + * Radius + * + * @param float $area Area + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getRadiusByArea(\float $area) + { + return sqrt($area / pi()); + } + + /** + * Radius + * + * @param float $C Circumference + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getRadiusByCircumference(\float $C) + { + return $C / (2 * pi()); + } +} diff --git a/Math/Shape/D2/Ellipse.php b/Math/Shape/D2/Ellipse.php index e69de29bb..8810b4387 100644 --- a/Math/Shape/D2/Ellipse.php +++ b/Math/Shape/D2/Ellipse.php @@ -0,0 +1,62 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Math\Algebra; + +class Ellipse +{ + + /** + * Area + * + * | + * b + * -------a-|---- + * | + * + * @param float $a Axis + * @param float $b Axis + * + * @return float Distance between points in meter + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getArea(\float $a, \float $b) + { + return pi() * $a * $b; + } + + /** + * Circumference + * + * | + * b + * -------a-|---- + * | + * + * @param float $a Axis + * @param float $b Axis + * + * @return float Distance between points in meter + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getCircumference(\float $a, \float $b) + { + return pi() * ($a + $b) * (3 * ($a - $b) ** 2 / (($a + $b) ** 2 * (sqrt(-3 * ($a - $b) ** 2 / (($a + $b) ** 2) + 4) + 10)) + 1); + } +} diff --git a/Math/Shape/D2/Quadrilateral.php b/Math/Shape/D2/Quadrilateral.php index 0c82ec0d4..e69de29bb 100644 --- a/Math/Shape/D2/Quadrilateral.php +++ b/Math/Shape/D2/Quadrilateral.php @@ -1,7 +0,0 @@ -abstract class Quadrilateral extends Polygon { -public function __construct() { - -} - - -} \ No newline at end of file diff --git a/Math/Shape/D2/Rectangle.php b/Math/Shape/D2/Rectangle.php index e69de29bb..684b42707 100644 --- a/Math/Shape/D2/Rectangle.php +++ b/Math/Shape/D2/Rectangle.php @@ -0,0 +1,68 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Math\Algebra; + +class Rectangle +{ + + /** + * Area + * + * @param float $a Edge + * @param float $b Edge + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getArea(\float $a, \float $b) + { + return $a * $b; + } + + /** + * Perimeter + * + * @param float $a Edge + * @param float $b Edge + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPerimeter(\float $a, \float $b) + { + return 2 * ($a + $b); + } + + /** + * Diagonal + * + * @param float $a Edge + * @param float $b Edge + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getDiagonal(\float $a, \float $b) + { + return sqrt($a * $a + $b * $b); + } +} diff --git a/Math/Shape/D2/Shape2DInterface.php b/Math/Shape/D2/Shape2DInterface.php deleted file mode 100644 index a80e6502b..000000000 --- a/Math/Shape/D2/Shape2DInterface.php +++ /dev/null @@ -1,155 +0,0 @@ - - * @author Dennis Eichhorn - * @copyright 2013 Dennis Eichhorn - * @license OMS License 1.0 - * @version 1.0.0 - * @link http://orange-management.com - */ -namespace phpOMS\Math\Shape\D2; - -use phpOMS\Math\Shape\ShapeInterface; - -/** - * 2D Shape interface. - * - * @category Framework - * @package phpOMS\Math - * @author OMS Development Team - * @author Dennis Eichhorn - * @license OMS License 1.0 - * @link http://orange-management.com - * @since 1.0.0 - */ -interface Shape2DInterface extends ShapeInterface -{ - - /** - * Get the polygon perimeter. - * - * @return \float - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function getPerimeter(); - - /** - * Set the polygon perimeter. - * - * @param \float $perimeter Polygon perimeter - * - * @return void - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function setPerimeter($perimeter); - - /** - * Get the polygon perimeter formula. - * - * @return \string - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function getPerimeterFormula(); - - /** - * Get the polygon surface. - * - * @return \float - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function getSurface(); - - /** - * Set the polygon surface. - * - * @param \float $surface Polygon surface - * - * @return void - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function setSurface($surface); - - /** - * Get the polygon surface formula. - * - * @return \string - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function getSurfaceFormula(); - - /** - * Get the interior angle sum. - * - * @return \int|float - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function getInteriorAngleSum(); - - /** - * Get the interior angle sum formula. - * - * @return \string - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function getInteriorAngleSumFormula(); - - /** - * Get the exterior angle sum. - * - * @return \int|float - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function getExteriorAngleSum(); - - /** - * Get the exterior angle sum formula. - * - * @return \string - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function getExteriorAngleSumFormula(); - - /** - * Get the barycenter of the polygon. - * - * @return \float[] - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function getBarycenter(); - - /** - * Reset all values. - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function reset(); -} diff --git a/Math/Shape/D2/Square.php b/Math/Shape/D2/Square.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/Math/Shape/D2/Trapezoid.php b/Math/Shape/D2/Trapezoid.php index e69de29bb..f1ff06005 100644 --- a/Math/Shape/D2/Trapezoid.php +++ b/Math/Shape/D2/Trapezoid.php @@ -0,0 +1,184 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Math\Algebra; + +class Rectangle +{ + + /** + * Area + * + * --- a ---- + * / | \ + * c h d + * / | \ + * -------- b --------- + * + * @param float $a Edge + * @param float $b Edge + * @param float $h Height + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getArea(\float $a, \float $b, \float $h) + { + return ($a + $b) / 2 * $h; + } + + /** + * Perimeter + * + * --- a ---- + * / | \ + * c h d + * / | \ + * -------- b --------- + * + * @param float $a Edge + * @param float $b Edge + * @param float $c Edge + * @param float $d Edge + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPerimeter(\float $a, \float $b, \float $c, \float $d) + { + return $a + $b + $c + $d; + } + + /** + * Height + * + * --- a ---- + * / | \ + * c h d + * / | \ + * -------- b --------- + * + * @param float $area Area + * @param float $a Edge + * @param float $b Edge + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getHeight(\float $area, \float $a, \float $b) + { + return 2 * $area / ($a + $b); + } + + /** + * A + * + * --- a ---- + * / | \ + * c h d + * / | \ + * -------- b --------- + * + * @param float $area Area + * @param float $h Height + * @param float $b Edge + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getA(\float $area, \float $h, \float $b) + { + return 2 * $area / $h - $b; + } + + /** + * B + * + * --- a ---- + * / | \ + * c h d + * / | \ + * -------- b --------- + * + * @param float $area Area + * @param float $h Height + * @param float $a Edge + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getB(\float $area, \float $h, \float $a) + { + return 2 * $area / $h - $a; + } + + /** + * C + * + * --- a ---- + * / | \ + * c h d + * / | \ + * -------- b --------- + * + * @param float $perimeter Perimeter + * @param float $a Edge + * @param float $b Edge + * @param float $d Edge + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getC(\float $perimeter, \float $a, \float $b, \float $d) + { + return $perimeter - $a - $b - $d; + } + + /** + * D + * + * --- a ---- + * / | \ + * c h d + * / | \ + * -------- b --------- + * + * @param float $perimeter Perimeter + * @param float $a Edge + * @param float $b Edge + * @param float $c Edge + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getD(\float $perimeter, \float $a, \float $b, \float $c) + { + return $perimeter - $a - $b - $c; + } +} diff --git a/Math/Shape/D2/Triangle.php b/Math/Shape/D2/Triangle.php index e69de29bb..351bf5596 100644 --- a/Math/Shape/D2/Triangle.php +++ b/Math/Shape/D2/Triangle.php @@ -0,0 +1,75 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Math\Algebra; + +class Triangle +{ + + /** + * Area + * + * . + * /|\ + * a h c + * / | \ + * ----b--- + * + * @param float $b Edge + * @param float $h Height + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getArea(\float $b, \float $h) + { + return $h * $b / 2; + } + + /** + * Perimeter + * + * @param float $a Edge + * @param float $b Edge + * @param float $c Edge + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPerimeter(\float $a, \float $b, \float $c) + { + return $a + $b + $c; + } + + /** + * Diagonal + * + * @param float $area Area + * @param float $b Edge + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getHeight(\float $area, \float $b) + { + return 2 * $area / $b; + } +} diff --git a/Math/Shape/D3/Cone.php b/Math/Shape/D3/Cone.php index e69de29bb..f6f155db1 100644 --- a/Math/Shape/D3/Cone.php +++ b/Math/Shape/D3/Cone.php @@ -0,0 +1,84 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Math\Algebra; + +class Cone +{ + + /** + * Volume + * + * @param float $r Radius + * @param float $h Height + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getVolume(\float $r, \float $h) + { + return pi() * $r ** 2 * $h / 3; + } + + /** + * Surface area + * + * @param float $r Radius + * @param float $h Height + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getSurface(\float $r, \float $h) + { + return pi() * $r * ($r + sqrt($h ** 2 + $r ** 2)); + } + + /** + * Slant height + * + * @param float $r Radius + * @param float $h Height + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getSlantHeight(\float $r, \float $h) + { + return sqrt($h ** 2 + $r ** 2); + } + + /** + * Height + * + * @param float $V Volume + * @param float $r Radius + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getHeight(\float $V, \float $r) + { + return 4 * $V / (pi() * $r ** 2); + } +} diff --git a/Math/Shape/D3/Cube.php b/Math/Shape/D3/Cube.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/Math/Shape/D3/Cylinder.php b/Math/Shape/D3/Cylinder.php index e69de29bb..2101bf04d 100644 --- a/Math/Shape/D3/Cylinder.php +++ b/Math/Shape/D3/Cylinder.php @@ -0,0 +1,68 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Math\Algebra; + +class Cylinder +{ + + /** + * Volume + * + * @param float $r Radius + * @param float $h Height + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getVolume(\float $r, \float $h) + { + return pi() * $r ** 2 * $h; + } + + /** + * Surface area + * + * @param float $r Radius + * @param float $h Height + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getSurface(\float $r, \float $h) + { + return 2 * pi() * ($r * $h + $r ** 2); + } + + /** + * Lateral surface area + * + * @param float $r Radius + * @param float $h Height + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getLateralSurface(\float $r, \float $h) + { + return 2 * pi() * $r * $h; + } +} diff --git a/Math/Shape/D3/Pyramid.php b/Math/Shape/D3/Pyramid.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/Math/Shape/D3/Sphere.php b/Math/Shape/D3/Sphere.php index 50417f92d..cfa5602e9 100644 --- a/Math/Shape/D3/Sphere.php +++ b/Math/Shape/D3/Sphere.php @@ -13,7 +13,7 @@ * @version 1.0.0 * @link http://orange-management.com */ -namespace phpOMS\Math\Algebra; +namespace phpOMS\Math\Shape\D3; class Sphere { @@ -42,8 +42,8 @@ class Sphere $latDelta = $latTo - $latFrom; $lonDelta = $lonTo - $lonFrom; - $a = pow(cos($latTo) * sin($lonDelta), 2) + pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2); - $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta); + $a = pow(cos($latTo) * sin($lonDelta), 2) + pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2); + $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta); $angle = atan2(sqrt($a), $b); // Approximation (very good for short distances) @@ -51,4 +51,64 @@ class Sphere return $angle * $radius; } + + /** + * Volume + * + * @param float $r Radius + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getVolume(\float $r) + { + return 4 / 3 * pi() * $r ** 3; + } + + /** + * Radius + * + * @param float $V Volume + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getRadiusByVolume(\float $V) + { + return pow($V * 3 / (4 * pi()), 1 / 3); + } + + /** + * Surface area + * + * @param float $r Radius + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getSurface(\float $r) + { + return 4 * pi() * $r ** 2; + } + + /** + * Radius + * + * @param float $S Surface + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getRadiusBySurface(\float $S) + { + return sqrt($S / (4 * pi())); + } } diff --git a/Math/Shape/D3/SquarePyramid.php b/Math/Shape/D3/SquarePyramid.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/Math/Shape/D3/Tetrahedron.php b/Math/Shape/D3/Tetrahedron.php index e69de29bb..7ebd50d96 100644 --- a/Math/Shape/D3/Tetrahedron.php +++ b/Math/Shape/D3/Tetrahedron.php @@ -0,0 +1,65 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Math\Algebra; + +class Tetrahedron +{ + + /** + * Volume + * + * @param float $a Edge + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getVolume(\float $a) + { + return $a ** 3 / (6 * sqrt(2)); + } + + /** + * Surface area + * + * @param float $a Edge + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getSurface(\float $a) + { + return sqrt(3) * $a ** 2; + } + + /** + * Lateral surface area + * + * @param float $a Edge + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getFaceArea(\float $a) + { + return sqrt(3) / 4 * $a ** 2; + } +}