 # Python Program to Check if Three Points are Collinear

In the previous article, we have discussed Python Program to Find Sum of Series 1^1/1!+2^2/2!+3^3/3!…+n^n/n!
Given three points the task is to check whether the given three points are collinear or not in Python.

Collinear Points:

Collinear points are those that are located along the same straight line or in a single line. In Euclidean geometry, two or more points on a line that is close to or far from each other are said to be collinear.

Examples:

Example1:

Input:

Given First Point = ( 1 , 1 )
Given Second Point = ( 1 , 4 )
Given Third Point = ( 1, 5 )

Output:

The points ( 1 , 1 ) ( 1 , 4 ) ( 1 , 5 ) are collinear and lies on the straight line

Example2:

Input:

Given First Point = ( 7 , 0 )
Given Second Point = ( 8, 9 )
Given Third Point = ( 1, 2 )

Output:

The points ( 7 , 0 ) ( 8 , 9 ) ( 1 , 2 ) are not collinear

## Program to Check if Three Points are Collinear in Python

Below are the ways to check whether the given three points are collinear or not in Python:

### Method #1: Using Mathematical Formula (Static Input)

Approach:

• Give the first point as static input and store it in two variables.
• Give the second point as static input and store it in another two variables.
• Give the third point as static input and store it in another two variables.
• Calculate the area of these three points and store it in a variable.
• Check the above-calculated area is equal to 0 or not using the If conditional statement.
• If it is true then the given three points are collinear and lie in the straight line.
• Else the given three points are not collinear.
• The Exit of the Program.

Below is the implementation:

# Give the first point as static input and store it in two variables.
a1 = 1
b1 = 1
# Give the second point as static input and store it in another two variables.
a2 = 1
b2 = 4
# Give the third point as static input and store it in another two variables.
a3 = 1
b3 = 5
# Calculate the area of these three points(triangle) and store it in a variable.
# To avoid floating point computations, we avoided multiplication with 0.5.
areaofPts = a1 * (b2 - b3) + a2 * (b3 - b1) + a3 * (b1 - b2)
# Check the above-calculated area is equal to 0 or not using the If conditional statement.
if(areaofPts == 0):
# If it is true then the given three points are collinear and lie in the straight line.
print('The points (', a1, ',', b1, ') (', a2, ',', b2, ') (', a3,
',', b3, ')', 'are collinear and lies on the straight line ')
else:
# Else the given three points are not collinear.
print('The points (', a1, ',', b1, ') (', a2, ',', b2, ') (', a3,
',', b3, ')', 'are not collinear ')


Output:

The points ( 1 , 1 ) ( 1 , 4 ) ( 1 , 5 ) are collinear and lies on the straight line

### Method #2: Using Mathematical Formula (User Input)

Approach:

• Give the first point as user input using map(),int(),split() functions and store it in two variables.
• Give the second point as user input using map(),int(),split() functions and store it in two variables.
• Give the third point as user input using map(),int(),split() functions and store it in two variables.
• Calculate the area of these three points and store it in a variable.
• Check the above-calculated area is equal to 0 or not using the If conditional statement.
• If it is true then the given three points are collinear and lie in the straight line.
• Else the given three points are not collinear.
• The Exit of the Program.

Below is the implementation:

# Give the first point as user input using map(),int(),split() functions
# and store it in two variables.
a1, b1 = map(int, input(
'Enter some random first point values separated by spaces = ').split())
# Give the second point as user input using map(),int(),split() functions
# and store it in two variables.
a2, b2 = map(int, input(
'Enter some random second point values separated by spaces = ').split())
# Give the third point as user input using map(),int(),split() functions
# and store it in two variables.
a3, b3 = map(int, input(
'Enter some random third point values separated by spaces = ').split())
# Calculate the area of these three points(triangle) and store it in a variable.
# To avoid floating point computations, we avoided multiplication with 0.5.
areaofPts = a1 * (b2 - b3) + a2 * (b3 - b1) + a3 * (b1 - b2)
# Check the above-calculated area is equal to 0 or not using the If conditional statement.
if(areaofPts == 0):
# If it is true then the given three points are collinear and lie in the straight line.
print('The points (', a1, ',', b1, ') (', a2, ',', b2, ') (', a3,
',', b3, ')', 'are collinear and lies on the straight line ')
else:
# Else the given three points are not collinear.
print('The points (', a1, ',', b1, ') (', a2, ',', b2, ') (', a3,
',', b3, ')', 'are not collinear ')


Output:

Enter some random first point values separated by spaces = 7 0
Enter some random second point values separated by spaces = 8 9
Enter some random third point values separated by spaces = 1 2
The points ( 7 , 0 ) ( 8 , 9 ) ( 1 , 2 ) are not collinear

If you are new to the Python Programming Language then practice using our Python Programming Examples for Beginners as our expert team has designed them from scratch.