Rectangle
Square
Triangle (Scalene)
Circle
Regular Polygon
Your perimeter will be displayed here.
Understanding and Calculating Perimeter
Perimeter is a fundamental concept in geometry, referring to the total distance around the outside of a two-dimensional shape. Imagine walking along the edges of a shape; the total distance you cover is its perimeter. It's a crucial measurement used in various practical applications, from construction and gardening to designing layouts and even in everyday tasks like framing a picture.
The method for calculating perimeter varies depending on the shape's properties. Our calculator is designed to handle several common geometric figures.
Formulas for Common Shapes:
Rectangle: A rectangle has four sides, with opposite sides being equal in length. The perimeter (P) is calculated by adding up the lengths of all four sides, or more efficiently, using the formula:
P = 2 * (Length + Width) Example: For a rectangle with a length of 10 units and a width of 5 units, the perimeter is 2 * (10 + 5) = 2 * 15 = 30 units.
Square: A square is a special type of rectangle where all four sides are equal. If 's' represents the length of one side, the perimeter is:
P = 4 * s Example: For a square with a side length of 8 units, the perimeter is 4 * 8 = 32 units.
Triangle: The perimeter of any triangle is found by summing the lengths of its three sides (a, b, c).
P = a + b + c Example: For a triangle with sides measuring 7, 8, and 9 units, the perimeter is 7 + 8 + 9 = 24 units.
Circle: The perimeter of a circle is called its circumference. It's calculated using the formula:
C = 2 * π * r
where 'r' is the radius of the circle and 'π' (pi) is a mathematical constant approximately equal to 3.14159.
Example: For a circle with a radius of 6 units, the circumference is 2 * π * 6 ≈ 37.70 units.
Regular Polygon: A regular polygon has all sides equal in length and all interior angles equal. If 'n' is the number of sides and 's' is the length of one side, the perimeter is:
P = n * s Example: For a regular hexagon (6 sides) with each side measuring 4 units, the perimeter is 6 * 4 = 24 units.
Using this calculator simplifies the process, allowing you to quickly find the perimeter for various shapes by inputting the relevant dimensions.
function updateInputs() {
var shapeType = document.getElementById("shapeType").value;
var shapeInputsDiv = document.getElementById("shapeInputs");
var html = ";
if (shapeType === "rectangle") {
html += '
';
html += '';
html += '';
html += '
';
} else if (shapeType === "square") {
html += '
';
html += '';
html += '
';
} else if (shapeType === "triangle") {
html += '
';
html += '';
html += '';
html += '';
html += '
';
} else if (shapeType === "circle") {
html += '
';
html += '';
html += '
';
} else if (shapeType === "regular_polygon") {
html += '
';
html += '';
html += '';
html += '
';
}
shapeInputsDiv.innerHTML = html;
}
function calculatePerimeter() {
var shapeType = document.getElementById("shapeType").value;
var perimeter = 0;
var resultDiv = document.getElementById("result");
var unit = "units"; // Default unit
try {
if (shapeType === "rectangle") {
var length = parseFloat(document.getElementById("rectLength").value);
var width = parseFloat(document.getElementById("rectWidth").value);
if (isNaN(length) || isNaN(width) || length <= 0 || width <= 0) {
resultDiv.innerHTML = "Please enter valid positive numbers for length and width.";
return;
}
perimeter = 2 * (length + width);
} else if (shapeType === "square") {
var side = parseFloat(document.getElementById("squareSide").value);
if (isNaN(side) || side <= 0) {
resultDiv.innerHTML = "Please enter a valid positive number for side length.";
return;
}
perimeter = 4 * side;
} else if (shapeType === "triangle") {
var sideA = parseFloat(document.getElementById("triSideA").value);
var sideB = parseFloat(document.getElementById("triSideB").value);
var sideC = parseFloat(document.getElementById("triSideC").value);
if (isNaN(sideA) || isNaN(sideB) || isNaN(sideC) || sideA <= 0 || sideB <= 0 || sideC <= 0) {
resultDiv.innerHTML = "Please enter valid positive numbers for all three side lengths.";
return;
}
// Triangle inequality theorem check (optional but good practice)
if (sideA + sideB <= sideC || sideA + sideC <= sideB || sideB + sideC <= sideA) {
resultDiv.innerHTML = "These side lengths do not form a valid triangle.";
return;
}
perimeter = sideA + sideB + sideC;
} else if (shapeType === "circle") {
var radius = parseFloat(document.getElementById("circleRadius").value);
if (isNaN(radius) || radius <= 0) {
resultDiv.innerHTML = "Please enter a valid positive number for the radius.";
return;
}
perimeter = 2 * Math.PI * radius;
unit = "units (Circumference)"; // Specific unit for circle
} else if (shapeType === "regular_polygon") {
var numSides = parseInt(document.getElementById("polygonSides").value);
var sideLength = parseFloat(document.getElementById("polygonSideLength").value);
if (isNaN(numSides) || isNaN(sideLength) || numSides < 3 || sideLength <= 0) {
resultDiv.innerHTML = "Please enter a valid number of sides (3 or more) and a positive side length.";
return;
}
perimeter = numSides * sideLength;
}
resultDiv.innerHTML = "The perimeter is: " + perimeter.toFixed(2) + " " + unit;
} catch (e) {
resultDiv.innerHTML = "An error occurred. Please check your inputs.";
console.error(e);
}
}
// Initialize inputs for the default shape on page load
document.addEventListener('DOMContentLoaded', updateInputs);