Line Equation Calculator

Line Equation Calculator

Enter coordinates for two points:

Results

Slope-Intercept Form:
Standard Form:

Slope (m):
Y-Intercept (b):
X-Intercept:
Distance:

Understanding the Equation of a Line

A linear equation represents a straight line on a Cartesian coordinate plane. Whether you are solving for physics trajectories, financial forecasting, or geometric proofs, understanding how to derive a line from two points is a fundamental mathematical skill.

The Slope-Intercept Form (y = mx + b)

The most common way to express a line is the slope-intercept form:

  • m (Slope): Represents the steepness of the line (Rise over Run).
  • b (Y-intercept): The point where the line crosses the Y-axis (when x = 0).

How the Calculation Works

To find the equation of a line passing through (x₁, y₁) and (x₂, y₂), follow these steps:

  1. Calculate Slope (m): Use the formula m = (y₂ - y₁) / (x₂ - x₁).
  2. Find Y-intercept (b): Rearrange the formula to b = y₁ - m(x₁).
  3. Assemble the Equation: Plug m and b into y = mx + b.

Real-World Example

Imagine a car traveling at a constant speed. At 1 hour (x₁=1), the car has traveled 60 miles (y₁=60). At 3 hours (x₂=3), it has traveled 180 miles (y₂=180).

Step 1: m = (180 – 60) / (3 – 1) = 120 / 2 = 60.
Step 2: b = 60 – (60 * 1) = 0.
Equation: y = 60x. (This means the speed is 60 mph).

function calculateLineEquation() { 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 resultBox = document.getElementById('line-result-box'); var slopeInterceptText = document.getElementById('slopeInterceptResult'); var standardFormText = document.getElementById('standardFormResult'); var valSlope = document.getElementById('valSlope'); var valIntercept = document.getElementById('valIntercept'); var valXIntercept = document.getElementById('valXIntercept'); var valDistance = document.getElementById('valDistance'); if (isNaN(x1) || isNaN(y1) || isNaN(x2) || isNaN(y2)) { alert("Please enter valid numerical coordinates."); return; } if (x1 === x2 && y1 === y2) { alert("The two points must be different to define a unique line."); return; } resultBox.style.display = "block"; // Distance Calculation var dist = Math.sqrt(Math.pow(x2 – x1, 2) + Math.pow(y2 – y1, 2)); valDistance.innerText = dist.toFixed(4); // Vertical Line Case if (x1 === x2) { slopeInterceptText.innerText = "x = " + x1; standardFormText.innerText = "1x + 0y = " + x1; valSlope.innerText = "Undefined (Vertical)"; valIntercept.innerText = "None"; valXIntercept.innerText = x1; return; } // Slope calculation var m = (y2 – y1) / (x2 – x1); // Y-intercept calculation (b = y – mx) var b = y1 – (m * x1); valSlope.innerText = m.toFixed(4); valIntercept.innerText = b.toFixed(4); // X-intercept (0 = mx + b -> x = -b/m) if (m !== 0) { valXIntercept.innerText = (-b / m).toFixed(4); } else { valXIntercept.innerText = "None (Horizontal)"; } // Formulate Slope-Intercept String var slopeStr = m === 0 ? "" : (m === 1 ? "x" : (m === -1 ? "-x" : m.toFixed(2) + "x")); var signStr = b >= 0 ? (slopeStr === "" ? "" : " + ") : " – "; var interceptStr = b === 0 ? (slopeStr === "" ? "0" : "") : Math.abs(b).toFixed(2); var finalSlopeIntercept = "y = " + slopeStr + signStr + interceptStr; slopeInterceptText.innerText = finalSlopeIntercept; // Formulate Standard Form: Ax + By = C // m = (y2-y1)/(x2-x1) -> (y-y1) = m(x-x1) -> (y-y1)(x2-x1) = (y2-y1)(x-x1) // (y1-y2)x + (x2-x1)y = x2y1 – y2x1 var A = y1 – y2; var B = x2 – x1; var C = (x2 * y1) – (y2 * x1); // Simplify A, B, C if A is negative if (A < 0) { A = -A; B = -B; C = -C; } standardFormText.innerText = A.toFixed(2) + "x + (" + B.toFixed(2) + ")y = " + C.toFixed(2); }

Leave a Comment