Last active
March 28, 2023 14:06
-
-
Save mahype/20a5424ff47aafc63b1b82877fe992a2 to your computer and use it in GitHub Desktop.
Surface Calculator
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 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 static 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; | |
} | |
/** | |
* 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 static 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 static 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 static 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 static 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 static 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 static function calculateEllipseArea($a, $b) { | |
return pi() * $a * $b; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment