Write A Python Function To Check And Return Whether The Points Are Collinear Or Not

Hi Guys, welcome to my blog in this article you will learn about how to Write A Python Function To Check And Return Whether The Points Are Collinear Or Not

 

Title : 

Write A Python Function To Check And Return Whether The Points Are Collinear Or Not



Write A Python Function To Check And Return Whether The Points Are Collinear Or Not


 

Description : 

Hi guys, in this article you will learn a python program that checks if three given points are collinear or not. The Python function returns true if three given points are colinear else it returns false. Three or more points are said to be collinear if they lie on a common straight line. If points do not lie on a straight line, they are said to be non-collinear. Define a function check_collinear() that takes three points as input, these points are tuple data types and contain x and y values. Unpack x and y values and check if (y3 - y2) * (x2 - x1) is equal to (y2 - y1) * (x3 - x2) if so return true else return false.


    
def check_collinear(point1, point2, point3):
   x1, y1 = point1
   x2, y2 = point2
   x3, y3 = point3
   return (y3 - y2) * (x2 - x1) == (y2 - y1) * (x3 - x2)


    


In the main program take three tuples as input and call the check_collinear() function defined above. If the function returns true print points are collinear else print points are not collinear.


    
p1 = (1, 2)
p2 = (3, 4)
p3 = (5, 6)

if collinear(p1, p2, p3):
   print("The Three Given Points are Collinear")
else:
   print("The Three Given Points are Not collinear")


    




Complete Python Code : 


    
def collinear(point1, point2, point3):
   x1, y1 = point1
   x2, y2 = point2
   x3, y3 = point3
   return (y3 - y2) * (x2 - x1) == (y2 - y1) * (x3 - x2)


p1 = (1, 2)
p2 = (3, 4)
p3 = (5, 6)

if collinear(p1, p2, p3):
   print("The Three Given Points are Collinear")
else:
   print("The Three Given Points are Not collinear")


    

 

 

Output : 


    
The Three Given Points are Collinear


    

 

Output : 


    
Change input values to 
p1 = (1, 2)
p2 = (3, 4)
p3 = (5, 9)

The Three Given Points are Not collinear


    



Conclusion :

The above Python program uses the Python function to check if three given point are collinear or not by comparing (y3 - y2) * (x2 - x1) == (y2 - y1) * (x3 - x2) If the values are equal the function returns true else false. Comment it down below if you have any suggestions to improve the above Python program

 

Post a Comment

0 Comments