Get x intercept given two points

Viewed 2970

This might be a somewhat simple question but I can not seem to get it working.

I want to find the x intercept given two points.

Lets say I have these two points: (5,3) and (3,4) I would like to find the x intercept. Currently this is what I have. Which finds the y intercept correctly. In this case 5.5.

var A = [5, 3];
var B = [3, 4];

function slope(a, b) {
    if (a[0] == b[0]) {
        return null;
    }

    return (b[1] - a[1]) / (b[0] - a[0]);
}

function intercept(point, slope) {
    if (slope === null) {
        // vertical line
        return point[0];
    }

    return point[1] - slope * point[0];
}

var m = slope(A, B);
console.log(m);

var b = intercept(A, m);
console.log('intercept: ' + b);
3 Answers
Related