How to Check If It Is a Straight Line in JavaScript?
Last Updated :
31 Jul, 2024
In this article, we are going to learn how we can check whether the given line is a Straight line or not using JavaScript. There are several methods of checking if the given line is straight or not in JavaScript. Below is an example to understand the problem clearly.
Example:
Input: points = [[1, 2], [2, 3], [3, 4], [4, 5]]
Output: true
Input: points = [[2,3], [7, 2] , [-1, 3]]
Output: false
Below are different approaches to Check If It Is a Straight Line in JavaScript:
Using Slope Calculation
Define a function that takes an array of coordinates as parameter. Get the x and y coordinates of the first two points from the coordinates array. Compute the slope between the first two points using the formula (y1 – y0) / (x1 – x0). Iterate through the remaining coordinates starting from index 2. For each point, calculate the slope between that point and the first point, and compare it with the initial slope. If slopes are not equal, return false else return true.
Example: To demonstrate checking If It Is a straight line in JavaScript using slope calculation approach.
JavaScript
function checkLine(coordinates) {
const [x0, y0] = coordinates[0];
const [x1, y1] = coordinates[1];
const slope = (y1 - y0) / (x1 - x0);
for (let i = 2; i < coordinates.length; i++) {
const [x, y] = coordinates[i];
if ((y - y0) / (x - x0) !== slope) {
return false;
}
}
return true;
}
const coordinates = [[1, 2], [2, 3], [3, 4], [4, 5]];
console.log(checkLine(coordinates));
Time complexity: O(n)
Space complexity: O(1)
Using Matrix Determinant Approach
Define a function that takes an array of coordinates as its parameter. Get the x and y coordinates of the first two points from the coordinates array. Calculate the coefficients A, B, and C by using formula:
A = y1 – y0
B = x0 – x1
C = x0 * (y0 – y1) + y0 * (x1 – x0)
Iterate through the remaining coordinates starting from index 2. For each point, calculate the expression Ax + By + C and check if it equals 0. If expression is not equal to 0, return false else return true.
Example: To demonstrate checking If It Is a straight line in JavaScript using matrix determinant approach.
JavaScript
function checkLine(coordinates) {
const [x0, y0] = coordinates[0];
const [x1, y1] = coordinates[1];
const A = y1 - y0;
const B = x0 - x1;
const C = x0 * (y0 - y1) + y0 * (x1 - x0);
for (let i = 2; i < coordinates.length; i++) {
const [x, y] = coordinates[i];
if (A * x + B * y + C !== 0)
{
return false;
}
}
return true;
}
const coordinates = [[1, 2], [2, 3], [3, 6], [4, 2]];
console.log(checkLine(coordinates));
Time complexity: O(n)
Space complexity: O(1)
Using Cross Product Method
The cross product method is another effective way to determine if a set of points lies on a straight line. This method involves calculating the cross product of vectors formed by consecutive points. If the cross product is zero for all consecutive vectors, the points lie on a straight line.
Example:
JavaScript
function isStraightLine(points) {
for (let i = 1; i < points.length - 1; i++) {
let x0 = points[i-1][0], y0 = points[i-1][1];
let x1 = points[i][0], y1 = points[i][1];
let x2 = points[i+1][0], y2 = points[i+1][1];
let crossProduct = (x1 - x0) * (y2 - y1) - (y1 - y0) * (x2 - x1);
if (crossProduct !== 0) {
return false;
}
}
return true;
}
let points1 = [[1, 2], [2, 3], [3, 4], [4, 5]];
console.log(isStraightLine(points1));
let points2 = [[2, 3], [7, 2], [-1, 3]];
console.log(isStraightLine(points2));
Time Complexity: O(n)
Space Complexity: O(1)
Similar Reads
How to Check if a Value is a Number in JavaScript ?
To check if a value is a number in JavaScript, use the typeof operator to ensure the value's type is 'number'. Additionally, functions like Number.isFinite() and !isNaN() can verify if a value is a valid, finite number. Methods to Check if a Value is a NumberThere are various ways to check if a valu
3 min read
How to Check if Object is JSON in JavaScript ?
JSON is used to store and exchange data in a structured format. To check if an object is JSON in JavaScript, you can use various approaches and methods. There are several possible approaches to check if the object is JSON in JavaScript which are as follows: Table of Content Using Constructor Type Ch
3 min read
How to check if a Variable Is Not Null in JavaScript ?
In JavaScript, checking if a variable is not null ensures that the variable has been assigned a value and is not empty or uninitialized. This check is important for avoiding errors when accessing or manipulating data, ensuring that the variable holds valid, usable content before proceeding with oper
3 min read
How to check object is an array in JavaScript ?
There are two different approaches to check an object is an array or not in JavaScript. 1. Using Array.isArray() MethodThe Array.isArray() method determines whether the value passed to this function is an array or not. This method returns true if the argument passed is an array else it returns false
1 min read
How to Create a New Line in JavaScript ?
A new line can be made in JavaScript using the below-discussed methods. Using Escape Sequence `\n`The \n escape sequence represents a newline character in JavaScript strings and it can used to render a new line in JavaScript. [GFGTABS] JavaScript console.log("GfG"); console.log("\n
3 min read
How to check if string contains only digits in JavaScript ?
A string with only digits means it consists solely of numeric characters (0-9) and contains no other characters or symbols. Here are the different methods to check if the string contains only digits. 1. Using Regular Expression (RegExp) with test() MethodThe most efficient way to check if a string c
3 min read
How to check if the value is primitive or not in JavaScript ?
To check if the value is primitive we have to compare the value data type. As Object is the non-primitive data type in JavaScript we can compare the value type to object and get the required results. Primitive data types are basic building blocks like numbers and characters, while non-primitive data
3 min read
JavaScript Program to Check if a Number is Float or Integer
In this article, we will see how to check whether a number is a float or an integer in JavaScript. A float is a number with a decimal point, while an integer is a whole number or a natural number without having a decimal point. Table of ContentUsing the Number.isInteger() MethodUsing the Modulus Ope
2 min read
How to check if an array includes an object in JavaScript ?
Check if an array includes an object in JavaScript, which refers to determining whether a specific object is present within an array. Since objects are compared by reference, various methods are used to identify if an object with matching properties exists in the array. These are the following appro
3 min read
How to check if one date is between two dates in JavaScript ?
The task is to determine if the given date is in between the given 2 dates or not? Here are a few of the most used techniques discussed with the help of JavaScript. In the first approach, we will use .split() method and the new Date() constructor. And in the second approach we will use the .getTime(
3 min read