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 ofInstance-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 ofInstance-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!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment