PHP Geometry Toolkit
Select a shape and provide dimensions to calculate the area and perimeter.
Select a shape and provide dimensions to calculate the area and perimeter.
<?php
include('includes/header2.php');
echo "<h1>PHP Geometry Toolkit</h1>";
echo '<p>Select a shape and provide dimensions to calculate the area and perimeter.</p>';
// Function to calculate the area and perimeter of a circle
function circle(float $radius): array
{
$area = pi() * pow($radius, 2);
$perimeter = 2 * pi() * $radius;
return ['area' => $area, 'perimeter' => $perimeter];
}
// Function to calculate the area and perimeter of a square
function square(float $side): array
{
$area = pow($side, 2);
$perimeter = 4 * $side;
return ['area' => $area, 'perimeter' => $perimeter];
}
// Function to calculate the area and perimeter of a rectangle
function rectangle(float $length, float $width): array
{
$area = $length * $width;
$perimeter = 2 * ($length + $width);
return ['area' => $area, 'perimeter' => $perimeter];
}
// Function to calculate the area and perimeter of a triangle (given all sides)
function triangle(float $a, float $b, float $c): array
{
// Check if the sides satisfy the triangle inequality
if ($a + $b > $c && $a + $c > $b && $b + $c > $a)
{
$s = ($a + $b + $c) / 2; // Semi-perimeter
$area = sqrt($s * ($s - $a) * ($s - $b) * ($s - $c)); // Heron's formula
$perimeter = $a + $b + $c;
return ['area' => $area, 'perimeter' => $perimeter];
}
else
{
return ['error' => 'Invalid triangle sides. They do not satisfy the triangle inequality.'];
}
}
// Handle form submission and validation
$result = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
$shape = $_POST['shape'];
switch ($shape)
{
case 'circle':
$radius = floatval($_POST['radius']);
if ($radius > 0)
{
$result = circle($radius);
}
else
{
$result = ['error' => 'Invalid radius. Must be greater than zero.'];
}
break;
case 'square':
$side = floatval($_POST['side']);
if ($side > 0)
{
$result = square($side);
}
else
{
$result = ['error' => 'Invalid side length. Must be greater than zero.'];
}
break;
case 'rectangle':
$length = floatval($_POST['length']);
$width = floatval($_POST['width']);
if ($length > 0 && $width > 0)
{
$result = rectangle($length, $width);
}
else
{
$result = ['error' => 'Invalid dimensions. Length and width must be greater than zero.'];
}
break;
case 'triangle':
$a = floatval($_POST['side_a']);
$b = floatval($_POST['side_b']);
$c = floatval($_POST['side_c']);
if ($a > 0 && $b > 0 && $c > 0)
{
$result = triangle($a, $b, $c);
}
else
{
$result = ['error' => 'Invalid triangle sides. All sides must be greater than zero.'];
}
break;
}
}
?>
<!-- HTML Form for User Input -->
<form method="post" action="mathfun2.php">
<label for="shape">Choose a shape:</label>
<select name="shape" id="shape" onchange="showFields(this.value)">
<option value="">-- Select a Shape --</option>
<option value="circle">Circle</option>
<option value="square">Square</option>
<option value="rectangle">Rectangle</option>
<option value="triangle">Triangle</option>
</select>
<!-- Fields for Circle -->
<div id="circle-fields" style="display: none;">
<label for="radius">Radius:</label>
<input type="number" step="0.01" name="radius" id="radius" placeholder="Enter radius">
</div>
<!-- Fields for Square -->
<div id="square-fields" style="display: none;">
<label for="side">Side Length:</label>
<input type="number" step="0.01" name="side" id="side" placeholder="Enter side length">
</div>
<!-- Fields for Rectangle -->
<div id="rectangle-fields" style="display: none;">
<label for="length">Length:</label>
<input type="number" step="0.01" name="length" id="length" placeholder="Enter length">
<label for="width">Width:</label>
<input type="number" step="0.01" name="width" id="width" placeholder="Enter width">
</div>
<!-- Fields for Triangle -->
<div id="triangle-fields" style="display: none;">
<label for="side_a">Side A:</label>
<input type="number" step="0.01" name="side_a" id="side_a" placeholder="Enter side A">
<label for="side_b">Side B:</label>
<input type="number" step="0.01" name="side_b" id="side_b" placeholder="Enter side B">
<label for="side_c">Side C:</label>
<input type="number" step="0.01" name="side_c" id="side_c" placeholder="Enter side C">
</div>
<button type="submit">Calculate</button>
</form>
<!-- Display Result -->
<?php
if ($result) {
if (isset($result['error'])) {
echo "<p style='color: red;'>{$result['error']}</p>";
} else {
echo "<h2>Results</h2>";
echo "<p>Area: " . round($result['area'], 2) . " square units</p>";
echo "<p>Perimeter: " . round($result['perimeter'], 2) . " units</p>";
}
}
?>
<script>
// Initially hide all fields
document.getElementById('circle-fields').style.display = 'none';
document.getElementById('square-fields').style.display = 'none';
document.getElementById('rectangle-fields').style.display = 'none';
document.getElementById('triangle-fields').style.display = 'none';
// Function to show/hide fields based on selected shape
function showFields(shape) {
// Hide all fields initially
document.getElementById('circle-fields').style.display = 'none';
document.getElementById('square-fields').style.display = 'none';
document.getElementById('rectangle-fields').style.display = 'none';
document.getElementById('triangle-fields').style.display = 'none';
// Show relevant fields based on the selected shape
if (shape === 'circle') {
document.getElementById('circle-fields').style.display = 'block';
} else if (shape === 'square') {
document.getElementById('square-fields').style.display = 'block';
} else if (shape === 'rectangle') {
document.getElementById('rectangle-fields').style.display = 'block';
} else if (shape === 'triangle') {
document.getElementById('triangle-fields').style.display = 'block';
}
}
</script>
<?php
include('includes/footer2.php');
?>