function calculateSlope() {
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 resultDiv = document.getElementById('slope-result');
if (isNaN(x1) || isNaN(y1) || isNaN(x2) || isNaN(y2)) {
alert("Please enter valid numerical values for all coordinates.");
return;
}
var deltaY = y2 – y1;
var deltaX = x2 – x1;
var stepHtml = "Step-by-step:";
if (deltaX === 0) {
document.getElementById('res-slope').innerText = "Undefined (Vertical Line)";
document.getElementById('res-eqn').innerText = "x = " + x1;
document.getElementById('res-intercept').innerText = "None (Does not cross Y-axis unless x=0)";
document.getElementById('res-angle').innerText = "90°";
document.getElementById('step-by-step').innerHTML = "Since x₁ = x₂ (" + x1 + " = " + x2 + "), the denominator is zero, making the slope undefined.";
} else {
var m = deltaY / deltaX;
var b = y1 – (m * x1);
var angle = Math.atan(m) * (180 / Math.PI);
document.getElementById('res-slope').innerText = m.toFixed(4).replace(/\.?0+$/, "");
var bDisplay = b >= 0 ? "+ " + b.toFixed(4).replace(/\.?0+$/, "") : "- " + Math.abs(b.toFixed(4).replace(/\.?0+$/, ""));
if (b === 0) bDisplay = "";
document.getElementById('res-eqn').innerText = "y = " + (m === 1 ? "" : (m === -1 ? "-" : m.toFixed(4).replace(/\.?0+$/, ""))) + "x " + bDisplay;
document.getElementById('res-intercept').innerText = b.toFixed(4).replace(/\.?0+$/, "");
document.getElementById('res-angle').innerText = angle.toFixed(2) + "°";
stepHtml += "1. m = (y₂ – y₁) / (x₂ – x₁)";
stepHtml += "2. m = (" + y2 + " – " + y1 + ") / (" + x2 + " – " + x1 + ")";
stepHtml += "3. m = " + deltaY + " / " + deltaX + " = " + m.toFixed(4).replace(/\.?0+$/, "");
document.getElementById('step-by-step').innerHTML = stepHtml;
}
resultDiv.style.display = 'block';
}
How to Calculate the Slope of a Line
In mathematics, the slope (also known as the gradient) describes both the direction and the steepness of a line. Whether you are working on algebra homework, analyzing a trend in physics, or designing a ramp, understanding the slope is fundamental to coordinate geometry.
The Slope Formula
To find the slope of a line passing through two points, $(x_1, y_1)$ and $(x_2, y_2)$, we use the following formula:
m = (y₂ – y₁) / (x₂ – x₁)
Where:
m is the slope.
y₂ – y₁ represents the "Rise" (the change in vertical distance).
x₂ – x₁ represents the "Run" (the change in horizontal distance).
Types of Slopes
Depending on the coordinates, the resulting slope will fall into one of four categories:
Positive Slope: The line rises from left to right.
Negative Slope: The line falls from left to right.
Zero Slope: The line is perfectly horizontal ($y_1 = y_2$).
Undefined Slope: The line is perfectly vertical ($x_1 = x_2$). Since division by zero is impossible in standard arithmetic, we call this slope "undefined."
Real-World Example
Imagine you are measuring a hill. If the base of the hill starts at point $(0, 0)$ and the peak is at $(50, 20)$, the calculation would be:
Rise: $20 – 0 = 20$
Run: $50 – 0 = 50$
Slope (m): $20 / 50 = 0.4$
A slope of 0.4 means for every 1 unit you move horizontally, the hill rises by 0.4 units. This can also be expressed as a 40% grade.
Finding the Equation of the Line
Once you have the slope ($m$), you can find the y-intercept ($b$) using the formula $y = mx + b$. By plugging in one of your points, you can solve for $b$ and find the full Slope-Intercept Form equation of the line, which our calculator provides automatically above.