# Finding if three points are collinear - JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

## Collinear Points

Three or more points that lie on the same straight line are called collinear points.

And three points lie on the same if the slope of all three pairs of lines formed by them is equal.

Consider, for example, three arbitrary points A, B and C on a 2-D plane, they will be collinear if −

slope of AB = slope of BC = slope of accepts

## Slope of a line −

The slope of a line is generally given by the tangent of the angle it makes with the positive direction of x-axis.

Alternatively, if we have two points that lie on the line, say A(x1, y1) and B(x2, y2), then the slope of line can be calculated by −

Slope of AB = (y2-y1) / (x2-x1)

Let’s write the code for this function −

## Example

Following is the code −

const a = {x: 2, y: 4};
const b = {x: 4, y: 6};
const c = {x: 6, y: 8};
const slope = (coor1, coor2) => (coor2.y - coor1.y) / (coor2.x - coor1.x);
const areCollinear = (a, b, c) => {
return slope(a, b) === slope(b, c) && slope(b, c) === slope(c, a);
};
console.log(areCollinear(a, b, c));

## Output

Following is the output in the console −

true