Home >Java >javaTutorial >How to check if three points are collinear in Java?

How to check if three points are collinear in Java?

WBOY
WBOYforward
2023-09-05 18:41:051145browse

How to check if three points are collinear in Java?

If three points are located on a straight line, they are said to be collinear. If the points are not on the same straight line, they are not collinear.

This means that if three points (x1, y1), (x2, y2), (x3, y3) are on the same straight line, they are collinear.

Among them, x1, y1, x2, y2, x3, y3 are points on the x-axis and y-axis, (x1, y1), (x2, y2), (x3, y3) are the coordinates.

Mathematically, there are two ways to determine whether three points are collinear.

Find the area of ​​a triangle by using the points. If the area of ​​the triangle is zero, then the three points are collinear.

Formula to find area of triangle = 0。5 * [x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)]

By finding that the slopes of two points are equal, you can determine that the three points are collinear.

Formula to find slope =
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 learn how to check if three points are collinear using Java programming language.

Show you some examples

Instance-1

is translated as:

Instance-1

Assume that the given coordinates are (1,2), (3,4), (5,6)

All three points are collinear because they lie on the same straight line.

The Chinese translation of

Instance-2

is:

Instance-2

Assume that the given coordinates are (1,1), (1,4), (1,6)

All three points are collinear because they lie on the same straight line.

The Chinese translation of

Instance-3

is:

Instance-3

Assume that the given coordinates are (1,1), (2,4), (4,6)

All three points are not collinear because they are not on the same straight line.

algorithm

  • Step 1 - Get three points via user input or initialization.

  • Step 2 - By using any one of the above formulas, check if the area of ​​the triangle is zero or if the slope is the same and then print the three points are collinear otherwise the three points are not collinear .

  • Step 3 − Print the results.

Multiple methods

We provide solutions in different ways.

  • Find the area of ​​a triangle.

  • By finding the slope.

Let’s look at the program and its output one by one

Method 1: By finding the area of ​​a triangle

In this method, the program will initialize three points. Then use the formula to calculate the area of ​​the triangle. If the area is zero, then three points are printed collinear.

Example

public class Main{
   //main method
   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 using three points ="+triangleArea);
      if (triangleArea == 0)
         System。out。println("Three points are collinear。");
      else
         System。out。println("Three points are not collinear。");
   }
}

Output

First point: 1。0, 2。0
Second pointe: 3。0, 4。0
Third pointe: 5。0, 6。0
Area of triangle using three points = 0。0
Three points are collinear。
.

Method 2: Find the slope

In this approach, three points will be initialized in the program. Then calculate the slope of any pair of points and check if slope is equal with slope of other pair of points by using the slope formula. If both slopes are equal then print three points are collinear.

Example

public class Main{
   //main method
   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 slope of (x1, y1) , (x2, y2)
      double m1 = (y2-y1) / (x2-x1);

      //find slope of (x2, y2) , (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("Three points are collinear。");
      else
         System。out。println("Three points are not collinear。");
   }
}

Output

First point: 1。0, 2。0
Second point: 3。0, 4。0
Third point: 5。0, 6。0
Slope of first pair= 1。0
Slope of second pair= 1。0
Three points are collinear。

In this article, we explored how to check if three points are collinear or not in Java by using different approaches.

The above is the detailed content of How to check if three points are collinear in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete