The slope of a line represents its steepness and direction. In coordinate geometry, the slope (often designated by the letter m) tells us how much the y-value changes for every unit change in the x-value. This is commonly referred to as "rise over run."
The Slope Formula
To find the slope between two points (x₁, y₁) and (x₂, y₂), we use the following formula:
m = (y₂ – y₁) / (x₂ – x₁)
Slope-Intercept Form
Once you have the slope (m) and the y-intercept (b), you can write the equation of the line in slope-intercept form:
y = mx + b
The y-intercept (b) is the point where the line crosses the y-axis (where x = 0). It can be calculated using the formula: b = y₁ – (m * x₁).
Practical Example
Let's say you have two points: (2, 3) and (6, 11).
Step 1: Calculate the change in y (rise): 11 – 3 = 8.
Step 2: Calculate the change in x (run): 6 – 2 = 4.
Step 3: Divide rise by run: 8 / 4 = 2. The slope (m) is 2.
Step 4: Find the y-intercept: 3 = (2 * 2) + b -> 3 = 4 + b -> b = -1.
Equation: y = 2x – 1.
Types of Slopes
Positive Slope: The line goes up from left to right.
Negative Slope: The line goes down from left to right.
Zero Slope: A horizontal line (y₂ = y₁).
Undefined Slope: A vertical line (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('slope-result');
if (isNaN(x1) || isNaN(y1) || isNaN(x2) || isNaN(y2)) {
alert("Please enter valid numerical values for all coordinates.");
return;
}
var dx = x2 – x1;
var dy = y2 – y1;
var m, b, equation, angle, distance;
resultDiv.style.display = 'block';
if (dx === 0) {
// Vertical Line
m = "Undefined";
b = "None";
equation = "x = " + x1;
angle = "90°";
distance = Math.abs(dy).toFixed(4);
} else {
m = dy / dx;
b = y1 – (m * x1);
// Formatting m for display
var m_display = Number.isInteger(m) ? m : m.toFixed(4);
var b_display = Number.isInteger(b) ? b : b.toFixed(4);
// Building the equation string
var sign = b >= 0 ? " + " : " – ";
var absB = Math.abs(b_display);
if (m === 0) {
equation = "y = " + b_display;
} else {
var m_part = m === 1 ? "x" : (m === -1 ? "-x" : m_display + "x");
equation = "y = " + m_part + (b === 0 ? "" : sign + absB);
}
// Distance formula: sqrt((x2-x1)^2 + (y2-y1)^2)
distance = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2)).toFixed(4);
// Angle of inclination: atan(m) in degrees
angle = (Math.atan(m) * (180 / Math.PI)).toFixed(2) + "°";
m = m_display;
b = b_display;
}
document.getElementById('res-m').innerText = m;
document.getElementById('res-b').innerText = b;
document.getElementById('res-eq').innerText = equation;
document.getElementById('res-dist').innerText = distance;
document.getElementById('res-angle').innerText = angle;
}