Last active
March 28, 2023 14:06
-
-
Save mahype/20a5424ff47aafc63b1b82877fe992a2 to your computer and use it in GitHub Desktop.
Revisions
-
mahype revised this gist
Mar 28, 2023 . 1 changed file with 7 additions and 7 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ class SurfaceCalculator { * @return float The area of the polygon. * @throws InvalidArgumentException If the number of points is less than 3. */ public static function calculatePolygonArea($points) { $num_points = count($points); if ($num_points < 3) { throw new InvalidArgumentException("A polygon must have at least 3 points."); @@ -37,7 +37,7 @@ public function calculatePolygonArea($points) { * @param float $height The length of the triangle's height. * @return float The area of the triangle. */ public static function calculateTriangleArea($base, $height) { return 0.5 * $base * $height; } @@ -49,7 +49,7 @@ public function calculateTriangleArea($base, $height) { * @param float $c The length of the third side of the triangle. * @return float The area of the triangle. */ public static function calculateHeronTriangleArea($a, $b, $c) { $s = ($a + $b + $c) / 2; return sqrt($s * ($s - $a) * ($s - $b) * ($s - $c)); } @@ -61,7 +61,7 @@ public function calculateHeronTriangleArea($a, $b, $c) { * @param float $width The width of the rectangle. * @return float The area of the rectangle. */ public static function calculateRectangleArea($length, $width) { return $length * $width; } @@ -71,7 +71,7 @@ public function calculateRectangleArea($length, $width) { * @param float $radius The radius of the circle. * @return float The area of the circle. */ public static function calculateCircleArea($radius) { return pi() * pow($radius, 2); } @@ -84,7 +84,7 @@ public function calculateCircleArea($radius) { * @param float $height The height between the parallel sides of the trapezoid. * @return float The area of the trapezoid. */ public static function calculateTrapezoidArea($a, $b, $height) { return 0.5 * ($a + $b) * $height; } @@ -95,7 +95,7 @@ public function calculateTrapezoidArea($a, $b, $height) { * @param float $b The length of the semi-minor axis of the ellipse. * @return float The area of the ellipse. */ public static function calculateEllipseArea($a, $b) { return pi() * $a * $b; } } -
mahype revised this gist
Mar 28, 2023 . 1 changed file with 57 additions and 19 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,19 @@ <?php /** * Class SurfaceCalculator * * This class provides methods to calculate the area of various shapes * like polygons, triangles, rectangles, circles, trapezoids, and ellipses. */ class SurfaceCalculator { /** * Calculate the area of a polygon using the Shoelace formula. * * @param array $points An array of associative arrays containing x and y coordinates. * @return float The area of the polygon. * @throws InvalidArgumentException If the number of points is less than 3. */ public function calculatePolygonArea($points) { $num_points = count($points); if ($num_points < 3) { @@ -17,47 +30,72 @@ public function calculatePolygonArea($points) { return abs($area) / 2; } /** * Calculate the area of a triangle given its base and height. * * @param float $base The length of the triangle's base. * @param float $height The length of the triangle's height. * @return float The area of the triangle. */ public function calculateTriangleArea($base, $height) { return 0.5 * $base * $height; } /** * Calculate the area of a triangle using Heron's formula. * * @param float $a The length of the first side of the triangle. * @param float $b The length of the second side of the triangle. * @param float $c The length of the third side of the triangle. * @return float The area of the triangle. */ public function calculateHeronTriangleArea($a, $b, $c) { $s = ($a + $b + $c) / 2; return sqrt($s * ($s - $a) * ($s - $b) * ($s - $c)); } /** * Calculate the area of a rectangle given its length and width. * * @param float $length The length of the rectangle. * @param float $width The width of the rectangle. * @return float The area of the rectangle. */ public function calculateRectangleArea($length, $width) { return $length * $width; } /** * Calculate the area of a circle given its radius. * * @param float $radius The radius of the circle. * @return float The area of the circle. */ public function calculateCircleArea($radius) { return pi() * pow($radius, 2); } /** * Calculate the area of a trapezoid given the lengths of its parallel sides and the height. * * @param float $a The length of the first parallel side of the trapezoid. * @param float $b The length of the second parallel side of the trapezoid. * @param float $height The height between the parallel sides of the trapezoid. * @return float The area of the trapezoid. */ public function calculateTrapezoidArea($a, $b, $height) { return 0.5 * ($a + $b) * $height; } /** * Calculate the area of an ellipse given the lengths of its semi-major and semi-minor axes. * * @param float $a The length of the semi-major axis of the ellipse. * @param float $b The length of the semi-minor axis of the ellipse. * @return float The area of the ellipse. */ public function calculateEllipseArea($a, $b) { return pi() * $a * $b; } } -
mahype created this gist
Mar 28, 2023 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,63 @@ <?php class SurfaceCalculator { public function calculatePolygonArea($points) { $num_points = count($points); if ($num_points < 3) { throw new InvalidArgumentException("A polygon must have at least 3 points."); } $area = 0; for ($i = 0; $i < $num_points; $i++) { $next_index = ($i + 1) % $num_points; $area += ($points[$i]['x'] * $points[$next_index]['y']) - ($points[$next_index]['x'] * $points[$i]['y']); } return abs($area) / 2; } public function calculateTriangleArea($base, $height) { return 0.5 * $base * $height; } public function calculateHeronTriangleArea($a, $b, $c) { $s = ($a + $b + $c) / 2; return sqrt($s * ($s - $a) * ($s - $b) * ($s - $c)); } public function calculateRectangleArea($length, $width) { return $length * $width; } public function calculateCircleArea($radius) { return pi() * pow($radius, 2); } public function calculateTrapezoidArea($a, $b, $height) { return 0.5 * ($a + $b) * $height; } public function calculateEllipseArea($a, $b) { return pi() * $a * $b; } } // Example usage: $surface_calculator = new SurfaceCalculator(); $polygon = [ ['x' => 0, 'y' => 0], ['x' => 4, 'y' => 0], ['x' => 4, 'y' => 4], ['x' => 0, 'y' => 4], ]; echo "The area of the polygon is: " . $surface_calculator->calculatePolygonArea($polygon) . "\n"; echo "The area of the triangle is: " . $surface_calculator->calculateTriangleArea(4, 3) . "\n"; echo "The area of the Heron's triangle is: " . $surface_calculator->calculateHeronTriangleArea(3, 4, 5) . "\n"; echo "The area of the rectangle is: " . $surface_calculator->calculateRectangleArea(4, 5) . "\n"; echo "The area of the circle is: " . $surface_calculator->calculateCircleArea(3) . "\n"; echo "The area of the trapezoid is: " . $surface_calculator->calculateTrapezoidArea(3, 5, 4) . "\n"; echo "The area of the ellipse is: " . $surface_calculator->calculateEllipseArea(3, 5) . "\n";