How to tell if points are collinear – Python Program to Check if Three Points are Collinear

How to tell if 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. There is a program to check if n points are collinear. And collinear functional, Show By Section Formula That The Points Are Collinear, Clockwise Orientation, collinear algorithm are discussed here.

Collinear Points python:

How to determine if points are collinear: 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

How to find collinear points: 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.

Recommended Reading On: Python Program for Section Formula

Answer these: 

  1. Ask The User To Enter Three Points And Find Whether They Are Collinear In Python
  2. Write A Function Which Takes Co-Ordinates Of Three Points As Input And Returns True If Points Are Collinear Otherwise Returns False.
  3. Determine If The Points Are Collinear
  4. How To Determine If Points Are Collinear
  5. How To Tell If Points Are Collinear
  6. How To Find If Points Are Collinear
  7. What Are Collinear Points
  8. How To Determine If Three Points Are Collinear
  9. If Three Points Are Collinear Then
  10. Given Three Points (X1, Y1), (X2, Y2) And (X3, Y3), Write A Program To Check If All The Three Points Fall On One Straight Line.
  11. Given Three Points (X1 Y1) (X2 Y2) And (X3 Y3)

Related Posts: