Write A Python Function To Check If The Two Lines Are Perpendicular

Hi Guys, welcome to my blog in this article you will learn about how to Write A Python Function To Check If The Two Lines Are Perpendicular

 

Title : 

Write A Python Function To Check If The Two Lines Are Perpendicular


Write A Python Function To Check If The Two Lines Are Perpendicular



 

Description : 

Hi guys, in this article you will learn a python program to check if two given lines are perpendicular or not. I will be writing a function that takes the x and y axis of two lines and checks if they are perpendicular or not. Define a function check_perpendicular() and pass x and y axis of the first line and second lines. There will be two values of x and y axis because each line contains starting and ending points denoted by x and y. Use the function to find the slope by dividing y values with x values of each line. Multiply slopes of given two lines and check if the product is equal to -1 if so you have found two lines that are perpendicular so return true. If the product of two lines is not equal to -1 then return Falls as we got to know that two lines are not perpendicular. This is the method to find if two lines are perpendicular or not.


    
def check_perpendicular(x1, y1, x2, y2, x3, y3, x4, y4):
   line1_slope = (y2 - y1) / (x2 - x1)
   line2_slope = (y4 - y3) / (x4 - x3)

   if line1_slope * line2_slope == -1:
       return True
   return False


    


Take four variables as the user to enter X and Y axis values of starting and ending points in the first line and store them as line1. Take another four variables as the user to enter x and y axis values of starting and ending points in the second line and store them as line2


    
x1_line1 = int(input("Enter x1 axis of Line1 : "))
y1_line1 = int(input("Enter y1 axis of Line1 : "))
x2_line1 = int(input("Enter x2 axis of Line1 : "))
y2_line1 = int(input("Enter y2 axis of Line1 : "))

x1_line2 = int(input("Enter x1 axis of Line2 : "))
y1_line2 = int(input("Enter y1 axis of Line2 : "))
x2_line2 = int(input("Enter x2 axis of Line2 : "))
y2_line2 = int(input("Enter y2 axis of Line2 : "))



    


After getting X and Y values of the two lines, call the function by passing all the 8 variables and print the result appropriately.


    
if check_perpendicular(x1_line1, y1_line1, x2_line1, y2_line1, x1_line2, y1_line2, x2_line2, y2_line2):
   print("Two Given Lines are Perpendicular")
else:
   print("Two Given Lines are Not Perpendicular")


    




Complete Python Code : 


    
def check_perpendicular(x1, y1, x2, y2, x3, y3, x4, y4):
   line1_slope = (y2 - y1) / (x2 - x1)
   line2_slope = (y4 - y3) / (x4 - x3)

   if line1_slope * line2_slope == -1:
       return True
   return False


x1_line1 = int(input("Enter x1 axis of Line1 : "))
y1_line1 = int(input("Enter y1 axis of Line1 : "))
x2_line1 = int(input("Enter x2 axis of Line1 : "))
y2_line1 = int(input("Enter y2 axis of Line1 : "))

x1_line2 = int(input("Enter x1 axis of Line2 : "))
y1_line2 = int(input("Enter y1 axis of Line2 : "))
x2_line2 = int(input("Enter x2 axis of Line2 : "))
y2_line2 = int(input("Enter y2 axis of Line2 : "))

if check_perpendicular(x1_line1, y1_line1, x2_line1, y2_line1, x1_line2, y1_line2, x2_line2, y2_line2):
   print("Two Given Lines are Perpendicular")
else:
   print("Two Given Lines are Not Perpendicular")  


    

 

 

Output : 


    
Enter x1 axis of Line1 : 1
Enter y1 axis of Line1 : 1
Enter x2 axis of Line1 : 0
Enter y2 axis of Line1 : 0
Enter x1 axis of Line2 : 1
Enter y1 axis of Line2 : -1
Enter x2 axis of Line2 : 0
Enter y2 axis of Line2 : 0
Two Given Lines are Perpendicular


    

 

Output : 


    
Enter x1 axis of Line1 : 1
Enter y1 axis of Line1 : 2
Enter x2 axis of Line1 : 3
Enter y2 axis of Line1 : 4
Enter x1 axis of Line2 : 1
Enter y1 axis of Line2 : 2
Enter x2 axis of Line2 : 3
Enter y2 axis of Line2 : 4
Two Given Lines are Not Perpendicular


    



Conclusion :

The above Python program accepts 8 variables as input, the first four variables are X and Y values of the starting and ending points of the first line. The next 4 variables are x and y values of the starting and ending points of the second line. After accepting the eight variables as input the program will call a function to check if the given lines are perpendicular or not and print the results appropriately. Comment it down below if you have any suggestions to improve the above Python program

 

Post a Comment

0 Comments