Calculate the slope, y-intercept, and distance between two points on a 2D plane.
Point 1 (x₁, y₁)
Point 2 (x₂, y₂)
Slope (m):
Equation:
Y-Intercept (b):
Distance:
Angle:
How to Find the Slope of a Line
In mathematics, the slope (also called the gradient) represents the steepness and direction of a line. It is defined as the change in the vertical coordinate (y) divided by the change in the horizontal coordinate (x) between two distinct points.
m = (y₂ – y₁) / (x₂ – x₁)
Understanding the Components
Rise: The difference in height between two points (y₂ – y₁).
Run: The horizontal distance between two points (x₂ – x₁).
Positive Slope: The line rises from left to right.
Negative Slope: The line falls from left to right.
Zero Slope: A perfectly horizontal line (y₁ = y₂).
Undefined Slope: A perfectly vertical line (x₁ = x₂).
Step-by-Step Calculation Example
Suppose you have two points: Point A (2, 3) and Point B (6, 11).
Identify your coordinates: x₁=2, y₁=3, x₂=6, y₂=11.
Subtract the y-coordinates: 11 – 3 = 8 (Rise).
Subtract the x-coordinates: 6 – 2 = 4 (Run).
Divide the Rise by the Run: 8 / 4 = 2.
The slope (m) is 2.
The Equation of a Line
Once you have the slope, you can find the Slope-Intercept form equation: y = mx + b. By plugging in one of the points, you can solve for b (the y-intercept), which is the point where the line crosses the y-axis.
function calculateSlopeLogic() {
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 resDiv = document.getElementById('slopeResult');
if (isNaN(x1) || isNaN(y1) || isNaN(x2) || isNaN(y2)) {
alert("Please enter valid numeric coordinates for both points.");
return;
}
var run = x2 – x1;
var rise = y2 – y1;
var m, b, dist, angle, eq;
// Distance Calculation
dist = Math.sqrt(Math.pow(run, 2) + Math.pow(rise, 2));
if (run === 0) {
m = "Undefined (Vertical Line)";
b = "None";
eq = "x = " + x1;
angle = "90°";
} else {
m = rise / run;
// b = y – mx
b = y1 – (m * x1);
// Formatting Equation
var mDisp = Math.round(m * 1000) / 1000;
var bDisp = Math.round(b * 1000) / 1000;
var bSign = bDisp >= 0 ? "+ " : "- ";
var bAbs = Math.abs(bDisp);
if (bDisp === 0) {
eq = "y = " + mDisp + "x";
} else {
eq = "y = " + mDisp + "x " + bSign + bAbs;
}
// Angle Calculation
angle = Math.atan(m) * (180 / Math.PI);
angle = (Math.round(angle * 100) / 100) + "°";
// Rounding m and b for display
m = Math.round(m * 10000) / 10000;
b = Math.round(b * 10000) / 10000;
}
document.getElementById('resM').innerHTML = m;
document.getElementById('resEq').innerHTML = eq;
document.getElementById('resB').innerHTML = b;
document.getElementById('resDist').innerHTML = Math.round(dist * 10000) / 10000;
document.getElementById('resAngle').innerHTML = angle;
resDiv.style.display = 'block';
}