The x-coordinates cannot be the same (Vertical Line / Undefined Slope).
Understanding the Slope of a Line
In mathematics and coordinate geometry, the slope (also known as the gradient) represents the steepness and direction of a line. It is a fundamental concept used in everything from engineering and physics to economics and data science.
The Slope Formula
The slope is typically represented by the letter m. It is calculated as the "rise over run," which is the change in the vertical axis (y) divided by the change in the horizontal axis (x):
m = (y₂ – y₁) / (x₂ – x₁)
Slope-Intercept Form
Once you have the slope (m) and the y-intercept (b), you can express the line in the slope-intercept form equation:
y = mx + b
m: The slope of the line.
b: The point where the line crosses the Y-axis.
Example Calculation
Imagine you have two points on a graph: Point A (2, 3) and Point B (6, 11).
Identify coordinates: x₁=2, y₁=3, x₂=6, y₂=11.
Calculate change in y: 11 – 3 = 8.
Calculate change in x: 6 – 2 = 4.
Divide: 8 / 4 = 2. The slope (m) is 2.
Find Intercept: 3 = 2(2) + b => 3 = 4 + b => b = -1.
Final Equation: y = 2x – 1.
Types of Slopes
Positive Slope: The line rises from left to right.
Negative Slope: The line falls from left to right.
Zero Slope: A horizontal line where y₁ = y₂.
Undefined Slope: A vertical line where x₁ = x₂.
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("slopeResult");
var errorDiv = document.getElementById("slopeError");
if (isNaN(x1) || isNaN(y1) || isNaN(x2) || isNaN(y2)) {
alert("Please enter valid numerical coordinates.");
return;
}
if (x1 === x2) {
resultDiv.style.display = "none";
errorDiv.style.display = "block";
return;
} else {
errorDiv.style.display = "none";
}
var m = (y2 – y1) / (x2 – x1);
var b = y1 – (m * x1);
var distance = Math.sqrt(Math.pow((x2 – x1), 2) + Math.pow((y2 – y1), 2));
var angleRad = Math.atan2((y2 – y1), (x2 – x1));
var angleDeg = angleRad * (180 / Math.PI);
document.getElementById("resSlope").innerText = m.toFixed(4).replace(/\.?0+$/, "");
document.getElementById("resIntercept").innerText = b.toFixed(4).replace(/\.?0+$/, "");
document.getElementById("resDistance").innerText = distance.toFixed(4).replace(/\.?0+$/, "");
document.getElementById("resAngle").innerText = angleDeg.toFixed(2);
var equationStr = "y = " + (m === 0 ? "" : (m === 1 ? "x" : (m === -1 ? "-x" : m.toFixed(2).replace(/\.?0+$/, "") + "x")));
if (b > 0) {
equationStr += (m === 0 ? "" : " + ") + b.toFixed(2).replace(/\.?0+$/, "");
} else if (b < 0) {
equationStr += " – " + Math.abs(b).toFixed(2).replace(/\.?0+$/, "");
} else if (m === 0 && b === 0) {
equationStr = "y = 0";
}
document.getElementById("resEquation").innerText = equationStr;
resultDiv.style.display = "block";
}