Distance Formula Calculator
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
line-height: 1.6;
margin: 20px;
background-color: #f4f7f6;
color: #333;
}
.calculator-container {
max-width: 700px;
margin: 30px auto;
padding: 30px;
background-color: #ffffff;
border-radius: 8px;
box-shadow: 0 4px 15px rgba(0, 74, 153, 0.1);
border: 1px solid #e0e0e0;
}
h1 {
color: #004a99;
text-align: center;
margin-bottom: 25px;
}
.input-section, .result-section {
margin-bottom: 25px;
padding: 20px;
border: 1px solid #d0d0d0;
border-radius: 5px;
background-color: #f8f9fa;
}
.input-group {
margin-bottom: 15px;
display: flex;
align-items: center;
gap: 15px;
}
.input-group label {
display: inline-block;
min-width: 150px;
font-weight: 600;
color: #004a99;
}
.input-group input[type="number"] {
flex-grow: 1;
padding: 10px 12px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1rem;
box-sizing: border-box;
}
.input-group input[type="number"]:focus {
outline: none;
border-color: #004a99;
box-shadow: 0 0 0 2px rgba(0, 74, 153, 0.2);
}
button {
display: block;
width: 100%;
padding: 12px 20px;
background-color: #004a99;
color: white;
border: none;
border-radius: 5px;
font-size: 1.1rem;
cursor: pointer;
transition: background-color 0.3s ease;
margin-top: 10px;
}
button:hover {
background-color: #003f80;
}
.result-display {
text-align: center;
margin-top: 20px;
padding: 20px;
background-color: #28a745;
color: white;
border-radius: 5px;
font-size: 1.4rem;
font-weight: bold;
box-shadow: 0 2px 10px rgba(40, 167, 69, 0.3);
}
.result-display span {
font-size: 1.2rem;
font-weight: normal;
display: block;
margin-top: 5px;
}
.article-section {
margin-top: 40px;
padding: 30px;
background-color: #ffffff;
border-radius: 8px;
box-shadow: 0 4px 15px rgba(0, 74, 153, 0.1);
border: 1px solid #e0e0e0;
}
.article-section h2 {
color: #004a99;
border-bottom: 2px solid #004a99;
padding-bottom: 10px;
margin-bottom: 20px;
}
.article-section p {
margin-bottom: 15px;
color: #555;
}
.article-section code {
background-color: #eef;
padding: 2px 5px;
border-radius: 3px;
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
}
@media (max-width: 600px) {
.input-group {
flex-direction: column;
align-items: stretch;
}
.input-group label {
min-width: auto;
margin-bottom: 5px;
}
.calculator-container {
margin: 15px;
padding: 20px;
}
h1 {
font-size: 1.8rem;
}
}
Distance Formula Calculator
Distance: —
Enter coordinates to see the result
Understanding the Distance Formula
The distance formula is a fundamental concept in coordinate geometry, used to determine the straight-line distance between two points in a Cartesian coordinate system. It is derived directly from the Pythagorean theorem (a² + b² = c²).
The Formula
Given two points, P1 with coordinates (x1, y1) and P2 with coordinates (x2, y2), the distance 'd' between them is calculated using the following formula:
d = √[(x2 - x1)² + (y2 - y1)²]
How it Works
- Find the difference in x-coordinates: Calculate the horizontal change (Δx = x2 – x1).
- Find the difference in y-coordinates: Calculate the vertical change (Δy = y2 – y1).
- Square the differences: Square both Δx and Δy. This ensures that the result is always positive, regardless of the order of the points.
- Sum the squares: Add the squared differences together: (Δx)² + (Δy)². This sum represents the square of the hypotenuse of a right triangle formed by the two points and the horizontal/vertical lines connecting them.
- Take the square root: The square root of the sum gives you the actual distance 'd' between the two points, which is the length of the hypotenuse.
Use Cases
The distance formula has numerous applications across various fields:
- Geometry and Trigonometry: Calculating lengths of sides, diagonals, and perimeters of geometric shapes on a coordinate plane.
- Navigation and Mapping: Estimating distances between locations represented by coordinates.
- Physics: Determining displacement of objects moving in a 2D or 3D space (though the formula extends to 3D).
- Computer Graphics: Calculating distances between pixels, vertices, or objects for rendering and interaction.
- Data Analysis: Measuring similarity or dissimilarity between data points in a multi-dimensional space.
Example Calculation
Let's calculate the distance between Point A (1, 2) and Point B (4, 6).
- x1 = 1, y1 = 2
- x2 = 4, y2 = 6
Applying the formula:
d = √[(4 - 1)² + (6 - 2)²]
d = √[(3)² + (4)²]
d = √[9 + 16]
d = √[25]
d = 5
So, the distance between (1, 2) and (4, 6) is 5 units.
function calculateDistance() {
var x1 = parseFloat(document.getElementById("x1").value);
var y1 = parseFloat(document.getElementById("y1").value);
var x2 = parseFloat(document.getElementById("x2").value);
var y2 = parseFloat(document.getElementById("y2").value);
var resultElement = document.getElementById("result");
// Check if all inputs are valid numbers
if (isNaN(x1) || isNaN(y1) || isNaN(x2) || isNaN(y2)) {
resultElement.innerHTML = "Distance: Invalid Input
Please enter valid numbers for all coordinates.";
resultElement.style.backgroundColor = "#dc3545"; // Red for error
return;
}
var deltaX = x2 – x1;
var deltaY = y2 – y1;
var distanceSquared = Math.pow(deltaX, 2) + Math.pow(deltaY, 2);
var distance = Math.sqrt(distanceSquared);
resultElement.innerHTML = "Distance: " + distance.toFixed(4);
resultElement.innerHTML += "
The straight-line distance between the two points.";
resultElement.style.backgroundColor = "#28a745"; // Green for success
}