
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If Three Points Are Collinear in Java
In mathematical terms, three points are said to be collinear if all three points lie on a straight line. If the points do not lie on the same straight line, then they are not considered collinear points.
Those three points represent coordinates on a plane: (x1, y1), (x2, y2), and (x3, y3). They must lie on the same straight line to be considered collinear. Where, x1, y1, x2, y2, x3, y3 are the points on x and y axis and (x1, y1), (x2, y2), (x3, y3) are the coordinates.
Determining Colinearity
Mathematically, there are two ways to know whether the three points are collinear or not, which are:
1. By calculating the area of a triangle using these coordinates, if the area of the triangle is zero, then the three points are collinear. Following is the formula to calculate the area of a Triangle:
area or triangle: = 0.5 * [x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)]
2. By calculating the slopes of both points, if the slopes are equal, then the three points are collinear. Following is the formula to calculate the slope of the line:
Slope of (x1, y1), (x2, y2): m1 = (y2-y1) / (x2-x1) Slope of (x2, y2), (x3, y3): m2 = (y3-y2) / (x3-x2)
In this article, we will discuss how to check whether the three points are collinear or not by implementing the above mathematical formula in the program:
Input & Output Scenarios
Following are the mathematical Scenarios that give us a clear understandingof collinear and how to determine it:
Scenario 1
Suppose the given coordinates of the triangle are:
Input: points: (x1 = 1, y1 = 2) (x2 = 3, y2 = 4) (x3 = 5, y3 = 6) Output: area of triangle: = 0.5 * [1 * (4 - 6) + 3 * (6 - 2) + 5 * (2 - 4)] = 0.5 *[-2 + 12 -10] = 0.5 * 0 = 0
Since the area of a triangle is equal to 0, it indicates that all three points are collinear.
Scenario 2
Suppose the given coordinates of the triangle are:
Input: points: (x1 = 1, y1 = 1) (x2 = 2, y2 = 4) (x3 = 4, y3 = 6) Output: slope of points: m1 = (4-1) / (2-1) = 3/1 = 3 m2 = (6-4) / (4-2) = 2/2 = 1 m1 != m2
Since the slope of the points is not equal, it verifies that the points are not on the same line (not collinear).
Determining Colinearity Using Java
Following are the different ways to determine whether the three points are collinear or not:
By Comparing Triangle Area with Zero
To determine whether the given points are collinear, we calculate the area of the triangle formed by these points. If the area is zero, the points are collinear.
Example
In the following example, we calculate the area of the triangle formed by the given points (1, 2), (3, 4), and (5, 6) using the above formula and comparing it with zero to determine the collinearity:
public class checkCollinear{ public static void main(String args[]){ //initialized first point double x1 = 1; double y1 = 2; System.out.println("First point: (" + x1 + ", " + y1 + ")"); //initialized second point double x2 = 3; double y2 = 4; System.out.println("Second point: ("+ x2 + ", " + y2 +")"); //initialized third point double x3 = 5; double y3 = 6; System.out.println("Third point: (" + x3 + ", " + y3 + ")"); //find triangle area by using formula double triangleArea = 0.5 * (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)); System.out.println("Area of triangle formed by these points: "+ triangleArea); if (triangleArea == 0){ System.out.println("Yes! the given points are collinear."); } else{ System.out.println("No! the given points are not collinear."); } } }
The above program produces the following output:
First point: (1.0, 2.0) Second point: (3.0, 4.0) Third point: (5.0, 6.0) Area of triangle formed by these points: 0.0 Yes! the given points are collinear.
Comparing Slopes
The slope of a line is a number that describes the direction of the line on a plane. It is calculated as the ratio of the vertical change to the horizontal change between two distinct points on the line.
To check if the given coordinates are collinear, we calculate the slopes (m1 and m2) formed by the given points and compare them. If they are equal, it means the points are collinear.
Example
In the example below, we calculate the slope of these points (1, 2), (3, 4), and (5, 8) using the above formula. If the slopes are equal, then the points are collinear; if not, they are non-collinear:
public class checkCollinear{ public static void main(String args[]){ //initialized first point double x1 = 1; double y1 = 2; System.out.println("First point: (" + x1 + ", " + y1 + ")"); //initialized second point double x2 = 3; double y2 = 4; System.out.println("Second point: (" + x2 + ", " + y2 + ")"); //initialized third point double x3 = 5; double y3 = 8; System.out.println("Third point: (" + x3 + ", " + y3 + ")"); //find slope of (x1, y1) and (x2, y2) double m1 = (y2-y1) / (x2-x1); //find slope of (x2, y2) and (x3, y3) double m2 = (y3-y2) / (x3-x2); System.out.println("Slope of first pair = " + m1); System.out.println("Slope of second pair = " + m2); if (m1 == m2) System.out.println("Yes! the points are collinear."); else System.out.println("No! points are not collinear."); } }
Following is the output of the above program:
First point: (1.0, 2.0) Second point: (3.0, 4.0) Third point: (5.0, 8.0) Slope of first pair = 1.0 Slope of second pair = 2.0 No! points are not collinear.