JavaScript Function to find all points between any two points

JavaScript Function to find all points between any two points

In our day today development some time we required a to find out all points between any two points. Let us discuss a scenario where I need to draw lines on a HTML5 Canvas but no two line will Cross each other. In this case to validate the new line that it is not overlapping the existing line first I need to find out all the points hold by the existing line. I have X1, Y1 & X2, Y2 as the starting & ending point of the existing line. Using the below function I am calculating the Slope & Intercept. Finally, using Console.log() I am printing all the points between X1, Y1 & X2, Y2.

Find all points between any two points

function getLinePoints(x1, y1, x2, y2) {
/*A & B are two Array which Holds x1, y1 & x2, y2 values*/
var A, B;
/*If both the lines are in same x-axis*/
if (x1 == x2) {
/*Checking for y1 greater than y2 else swapping the value*/
if (y1<y2) {
A=[x1,y1];
B=[x2,y2];
} else {
/*Swapping*/
B=[x1,y1];
A=[x2,y2];
}
/*Calculating Slope for the line*/
function getSlope(a, b) {
if (a[1] == b[1]) {
return null;
}
return (b[0] - a[0]) / (b[1] - a[1]);
}
function getIntercept(point, getSlope) {
if (getSlope === null) {
return point[0];
}
return point[1] - getSlope * point[0];
}
var m = getSlope(A, B);
var b = getIntercept(A, m);
for (var y = A[1]; y <= B[1]; y++){
/*Formula to Calculate x from y*/
var x = m * y + b;
/*Printing all line Points to Console window*/
console.log([x, y]);
}
} else {
/*Checking for x1 greater than x2 else swapping the value*/
if (x1<x2) {
A=[x1,y1];
B=[x2,y2];
} else {
/*Swapping*/
B=[x1,y1];
A=[x2,y2];
}
/*Calculating Slope for the line*/
function getSlope(a, b) {
if (a[0] == b[0]) {
return null;
}
return (b[1] - a[1]) / (b[0] - a[0]);
}
function getIntercept(point, getSlope) {
if (getSlope === null) {
return point[0];
}
return point[1] - getSlope * point[0];
}
var m = getSlope(A, B);
var b = getIntercept(A, m);
for (var x = A[0]; x <= B[0]; x++){
/*Formula to Calculate y from x*/
var y = m * x + b;
/*Printing all line Points to Console window*/
console.log([x, y]);
}
}
}