A line graph is a visual representation of the relationship between two variables, typically plotted on a Cartesian coordinate system (X and Y axes). This calculator helps you determine the exact mathematical relationship between any two points and visualizes the resulting straight line.
The Slope-Intercept Form
The most common way to express a linear relationship is the slope-intercept form: y = mx + b.
m (Slope): This represents the steepness of the line. It is the "rise over run"—how much y changes for every unit of x.
b (Y-Intercept): This is the point where the line crosses the vertical Y-axis (where x = 0).
(x, y): These represent any individual coordinate point on the line.
How to Calculate the Slope
To find the slope (m) manually, use the formula:
m = (y₂ – y₁) / (x₂ – x₁)
Once you have the slope, you can find the y-intercept (b) by plugging one of the points into the equation: b = y – mx.
Real-World Examples
Linear equations are used everywhere from physics to business:
Economics: Calculating total cost based on a fixed setup fee (intercept) plus a variable cost per unit (slope).
Physics: Calculating velocity when position is plotted against time.
Construction: Determining the pitch or grade of a roof or ramp.
Frequently Asked Questions
What if the slope is 0? If the slope is 0, you have a horizontal line. The equation will simply be y = b.
What is an "undefined" slope? An undefined slope occurs when the line is perfectly vertical (x₁ = x₂). In this case, the denominator in the slope formula becomes zero, which is mathematically impossible to divide by. The equation for a vertical line is x = [value].
function calculateLineGraph() {
var x1 = parseFloat(document.getElementById("x1_val").value);
var y1 = parseFloat(document.getElementById("y1_val").value);
var x2 = parseFloat(document.getElementById("x2_val").value);
var y2 = parseFloat(document.getElementById("y2_val").value);
var resSection = document.getElementById("results_section");
var resSlope = document.getElementById("res_slope");
var resIntercept = document.getElementById("res_intercept");
var resEquation = document.getElementById("res_equation");
var resXInt = document.getElementById("res_x_intercept");
var resAngle = document.getElementById("res_angle");
if (isNaN(x1) || isNaN(y1) || isNaN(x2) || isNaN(y2)) {
alert("Please enter valid numerical coordinates.");
return;
}
resSection.style.display = "block";
// Handle Vertical Line
if (x1 === x2) {
if (y1 === y2) {
resSlope.innerText = "Undefined (Point)";
resIntercept.innerText = "N/A";
resEquation.innerText = "Points are identical";
resXInt.innerText = x1;
resAngle.innerText = "N/A";
} else {
resSlope.innerText = "Undefined (Vertical)";
resIntercept.innerText = "None";
resEquation.innerText = "x = " + x1.toFixed(2);
resXInt.innerText = x1.toFixed(2);
resAngle.innerText = "90";
}
} else {
var m = (y2 – y1) / (x2 – x1);
var b = y1 – (m * x1);
var xInt = (m !== 0) ? (-b / m) : "None";
var angle = Math.atan(m) * (180 / Math.PI);
resSlope.innerText = m.toFixed(4);
resIntercept.innerText = b.toFixed(4);
var sign = b >= 0 ? " + " : " – ";
var absB = Math.abs(b);
resEquation.innerText = "y = " + m.toFixed(2) + "x" + sign + absB.toFixed(2);
resXInt.innerText = (typeof xInt === 'number') ? xInt.toFixed(4) : xInt;
resAngle.innerText = angle.toFixed(2);
}
drawLine(x1, y1, x2, y2);
}
function drawLine(x1, y1, x2, y2) {
var canvas = document.getElementById("lineCanvas");
var ctx = canvas.getContext("2d");
var w = canvas.width;
var h = canvas.height;
var padding = 40;
// Clear canvas
ctx.clearRect(0, 0, w, h);
// Draw Grid and Axis
ctx.strokeStyle = "#eee";
ctx.lineWidth = 1;
for(var i=0; i<=w; i+=40) {
ctx.beginPath(); ctx.moveTo(i, 0); ctx.lineTo(i, h); ctx.stroke();
}
for(var j=0; j<=h; j+=40) {
ctx.beginPath(); ctx.moveTo(0, j); ctx.lineTo(w, j); ctx.stroke();
}
// X and Y Axes
ctx.strokeStyle = "#333";
ctx.lineWidth = 2;
ctx.beginPath(); ctx.moveTo(w/2, 0); ctx.lineTo(w/2, h); ctx.stroke(); // Y
ctx.beginPath(); ctx.moveTo(0, h/2); ctx.lineTo(w, h/2); ctx.stroke(); // X
// Simple Mapping: Center is 0,0. Range is roughly -10 to 10.
var scale = 20;
var centerX = w/2;
var centerY = h/2;
function mapX(val) { return centerX + (val * scale); }
function mapY(val) { return centerY – (val * scale); }
// Points
ctx.fillStyle = "#ff4757";
ctx.beginPath();
ctx.arc(mapX(x1), mapY(y1), 5, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(mapX(x2), mapY(y2), 5, 0, Math.PI * 2);
ctx.fill();
// Draw Line
ctx.strokeStyle = "#0073aa";
ctx.lineWidth = 3;
if (x1 === x2) {
ctx.beginPath();
ctx.moveTo(mapX(x1), 0);
ctx.lineTo(mapX(x1), h);
ctx.stroke();
} else {
var m = (y2 – y1) / (x2 – x1);
var b = y1 – (m * x1);
// Find points at edge of canvas
var startX = -20;
var endX = 20;
ctx.beginPath();
ctx.moveTo(mapX(startX), mapY(m * startX + b));
ctx.lineTo(mapX(endX), mapY(m * endX + b));
ctx.stroke();
}
}
// Initial draw
window.onload = function() {
calculateLineGraph();
};