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 output = document.getElementById('calc-output');
var resultBox = document.getElementById('equation-result-box');
if (isNaN(x1) || isNaN(y1) || isNaN(x2) || isNaN(y2)) {
output.innerHTML = "Error: Please enter valid numerical coordinates.";
resultBox.style.display = 'block';
return;
}
if (x1 === x2 && y1 === y2) {
output.innerHTML = "Error: Points must be distinct to define a line.";
resultBox.style.display = 'block';
return;
}
var resultHtml = "";
// Vertical Line Case
if (x1 === x2) {
resultHtml += "Slope (m): Undefined (Vertical Line)";
resultHtml += "Y-Intercept: None";
resultHtml += "Standard Form: x = " + x1 + "";
resultHtml += "Slope-Intercept Form: N/A (Infinite slope)";
} else {
var m = (y2 – y1) / (x2 – x1);
var b = y1 – (m * x1);
// Formatting values
var m_clean = Number(m.toFixed(4));
var b_clean = Number(b.toFixed(4));
var b_sign = b_clean >= 0 ? " + " : " – ";
var b_abs = Math.abs(b_clean);
// Slope-Intercept Form: y = mx + b
var si_eqn = "y = " + m_clean + "x" + (b_clean !== 0 ? b_sign + b_abs : "");
// Point-Slope Form: y – y1 = m(x – x1)
var x1_sign = x1 >= 0 ? " – " : " + ";
var y1_sign = y1 >= 0 ? " – " : " + ";
var ps_eqn = "y" + y1_sign + Math.abs(y1) + " = " + m_clean + "(x" + x1_sign + Math.abs(x1) + ")";
// Standard Form: Ax + By = C (Rough conversion)
var A = -m_clean;
var B = 1;
var C = b_clean;
resultHtml += "Slope (m): " + m_clean + "";
resultHtml += "Y-Intercept (b): " + b_clean + "";
resultHtml += "Slope-Intercept Form: " + si_eqn + "";
resultHtml += "Point-Slope Form: " + ps_eqn + "";
resultHtml += "Standard Form: " + Number(A.toFixed(4)) + "x + " + B + "y = " + Number(C.toFixed(4));
}
output.innerHTML = resultHtml;
resultBox.style.display = 'block';
}
Understanding the Equation of a Line
In coordinate geometry, a linear equation represents a straight line on a Cartesian plane. Determining the equation of a line is a fundamental skill in algebra, physics, and data science. This calculator helps you find the specific algebraic representation of a line given two known points.
Common Forms of Linear Equations
Depending on the information available, mathematicians use different formats to express a line:
Slope-Intercept Form (y = mx + b): This is the most popular form. m represents the slope (steepness), and b represents the y-intercept (where the line crosses the vertical axis).
Point-Slope Form (y – y₁ = m(x – x₁)): Useful when you know one point and the slope. It emphasizes the relationship between a specific point and the direction of the line.
Standard Form (Ax + By = C): Often used in systems of linear equations. In this form, A, B, and C are typically integers.
How to Calculate the Slope
The slope, often called "rise over run," measures how much the y value changes for every unit change in x. The formula is:
m = (y₂ – y₁) / (x₂ – x₁)
Step-by-Step Example
Let's find the equation for a line passing through (2, 3) and (4, 7).
Find Y-Intercept (b): Using y = mx + b, substitute point (2, 3): 3 = 2(2) + b → 3 = 4 + b → b = -1.
Final Equation: y = 2x – 1.
Special Cases
Vertical Lines: If the x-coordinates are the same (x₁ = x₂), the slope is undefined because you cannot divide by zero. The equation is simply x = [value].
Horizontal Lines: If the y-coordinates are the same (y₁ = y₂), the slope is zero. The equation is simply y = [value].