Geometry is a branch of mathematics concerned with properties of space such as distance, shape, size, and relative position of figures. This calculator helps you compute fundamental properties of common geometric shapes, including their area, perimeter, volume, and surface area.
Formulas Used:
Circle:
Radius (r)
Area: π * r²
Circumference (Perimeter): 2 * π * r
Rectangle:
Length (l), Width (w)
Area: l * w
Perimeter: 2 * (l + w)
Triangle:
Base (b), Height (h)
Area: 0.5 * b * h
Perimeter: a + b + c (requires side lengths, this calculator uses base and height for area only)
Square:
Side (s)
Area: s²
Perimeter: 4 * s
Sphere:
Radius (r)
Volume: (4/3) * π * r³
Surface Area: 4 * π * r²
Cube:
Side (s)
Volume: s³
Surface Area: 6 * s²
The value of π (Pi) is approximated as 3.14159 in these calculations.
Use Cases:
Understanding these geometric calculations is crucial in various fields:
Construction and Architecture: Calculating the area of rooms for flooring, the volume of concrete needed for foundations, or the surface area for painting.
Engineering: Designing components, analyzing structural integrity, and calculating material requirements.
Art and Design: Proportioning elements in visual compositions, understanding spatial relationships.
Everyday Life: Estimating paint needed for a wall, determining how much fabric to buy for a project, or calculating the space required for furniture.
This calculator provides a quick and accessible way to perform these common geometric computations accurately.
function updateInputs() {
var shapeType = document.getElementById("shapeType").value;
var inputFieldsDiv = document.getElementById("inputFields");
inputFieldsDiv.innerHTML = ""; // Clear previous inputs
var htmlContent = "";
switch(shapeType) {
case "circle":
htmlContent = `
`;
break;
case "rectangle":
htmlContent = `
`;
break;
case "triangle":
htmlContent = `
`;
break;
case "square":
htmlContent = `
`;
break;
case "sphere":
htmlContent = `
`;
break;
case "cube":
htmlContent = `
`;
break;
}
inputFieldsDiv.innerHTML = htmlContent;
}
function calculateGeometry() {
var shapeType = document.getElementById("shapeType").value;
var resultDiv = document.getElementById("result");
var area = null;
var perimeter = null;
var volume = null;
var surfaceArea = null;
var resultText = "";
var pi = 3.14159;
try {
if (shapeType === "circle") {
var radius = parseFloat(document.getElementById("radius").value);
if (isNaN(radius) || radius <= 0) throw new Error("Please enter a valid positive radius.");
area = pi * radius * radius;
perimeter = 2 * pi * radius;
resultText = "Area: " + area.toFixed(4) + ", Circumference: " + perimeter.toFixed(4);
} else if (shapeType === "rectangle") {
var length = parseFloat(document.getElementById("length").value);
var width = parseFloat(document.getElementById("width").value);
if (isNaN(length) || isNaN(width) || length <= 0 || width <= 0) throw new Error("Please enter valid positive length and width.");
area = length * width;
perimeter = 2 * (length + width);
resultText = "Area: " + area.toFixed(4) + ", Perimeter: " + perimeter.toFixed(4);
} else if (shapeType === "triangle") {
var base = parseFloat(document.getElementById("base").value);
var height = parseFloat(document.getElementById("height").value);
if (isNaN(base) || isNaN(height) || base <= 0 || height <= 0) throw new Error("Please enter valid positive base and height.");
area = 0.5 * base * height;
resultText = "Area: " + area.toFixed(4) + " (Perimeter requires side lengths)";
} else if (shapeType === "square") {
var side = parseFloat(document.getElementById("side").value);
if (isNaN(side) || side <= 0) throw new Error("Please enter a valid positive side length.");
area = side * side;
perimeter = 4 * side;
resultText = "Area: " + area.toFixed(4) + ", Perimeter: " + perimeter.toFixed(4);
} else if (shapeType === "sphere") {
var radiusSphere = parseFloat(document.getElementById("radiusSphere").value);
if (isNaN(radiusSphere) || radiusSphere <= 0) throw new Error("Please enter a valid positive radius.");
volume = (4/3) * pi * Math.pow(radiusSphere, 3);
surfaceArea = 4 * pi * Math.pow(radiusSphere, 2);
resultText = "Volume: " + volume.toFixed(4) + ", Surface Area: " + surfaceArea.toFixed(4);
} else if (shapeType === "cube") {
var sideCube = parseFloat(document.getElementById("sideCube").value);
if (isNaN(sideCube) || sideCube <= 0) throw new Error("Please enter a valid positive side length.");
volume = Math.pow(sideCube, 3);
surfaceArea = 6 * Math.pow(sideCube, 2);
resultText = "Volume: " + volume.toFixed(4) + ", Surface Area: " + surfaceArea.toFixed(4);
}
resultDiv.textContent = resultText;
resultDiv.style.backgroundColor = "#28a745"; // Success Green
} catch (error) {
resultDiv.textContent = error.message;
resultDiv.style.backgroundColor = "#dc3545"; // Error Red
}
}
// Initialize inputs when the page loads
document.addEventListener('DOMContentLoaded', updateInputs);