How to check if two lines are parallel – Python Program to Check if two Lines are Parallel or Not

How to check if two lines are parallel: In the previous article, we have discussed Python Program to Find the Greatest Digit in a Number.
Parallel Lines :

How to check if two lines are parallel: If two lines remain the same distance apart along their entire length, they are said to be parallel. They will not meet no matter how far you stretch them. These lines are denoted by the equations ax+by=c.

The line equation is ax+by=c, where an is the x coefficient and b is the y coefficient. If the slopes of two lines are equal, we say they are parallel. As a result, we must determine the slope, which is “rise over run.”

The straight-line equation is y=mx+c, where m is the slope. Take a1,b1,c1 and a2,b2,c2 from the user and see if they are parallel.

Given the values of equations of two lines, and the task is to check if the given two lines are parallel or not.

Examples:

Example 1:

Input:

a1=4, b1=8, c1=13
a2=2, b2=4, c2=7

Output:

The given lines are parallel to each other

Example 2:

Input:

a1=8, b1=0, c1=9
a2=8, b2=0, c2=11

Output:

The given lines are parallel to each other

Program to Check if two Lines are Parallel or Not

How to know if two lines are parallel: Below are the ways to check if the given two lines are parallel or not.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the values of a1,b1,c1 as static input and store it in three separate variables.
  • Give the values of a2,b2,c2 as static input and store it in three separate variables.
  • We can Check if the slopes of the given two lines are equal by formula (a1/b1 == a2/b2).
  • Create a function checkParallel() which accepts the 6 parameters (First and second line x,y,z coordinates)
  • Inside the checkParallel() function.
  • Check if both lines y coordinates are not equal to 0 using the If conditional Statement.
  • If it is true then check a1/b1 is equal to a2/b2 using another Nested If conditional Statement.
  • If it is true then return True
  • Else return False.
  • If parent If conditional statement is false then inside the else statement check if first line and second line x and y coordinates are equal or not using the nested If conditional statement.
  • If it is true then return True else return False.
  • Pass the a1,b1,c1,a2,b2,c2 as the arguments to checkParallel() function inside the if Conditional statement.
  • If it is true then print those lines are parallel.
  • Else they are not parallel.
  • The Exit of the Program.

Below is the implementation:

#Create a function checkParallel() which accepts the 6 parameters
#(First and second line x,y,z coordinates)
#We can Check if the slopes of the given two lines are equal by formula (a1/b1 == a2/b2).
def checkParallel(a1,b1,c1,a2,b2,c2):
    #Inside the checkParallel() function.
    #Check if both lines y coordinates are not equal to 0 
    #using the If conditional Statement.
    if(b1!=0 and b2!=0):
        #If it is true then check a1/b1 is equal to a2/b2 
        #using another Nested If conditional Statement.
        if(a1/b1==a2/b2):
            #If it is true then return True
            return True
        else:
            #Else return False.
            return False
    #If parent If conditional statement is false then inside the else statement 
    
    else:
      #check if first line and second line x and y coordinates are equal
      #or not using the nested If conditional statement.
      if(a1==a2 and b1==b2):
     	 #If it is true then return True else return False.
          return True
      else:
          return False
#Give the values of a1,b1,c1  as static input 
#and store it in three separate variables.
a1,b1,c1=4,8,13
#Give the values of a2,b2,c2  as static input 
#and store it in three separate variables.
a2,b2,c2=2,4,7
#Pass the a1,b1,c1,a2,b2,c2 as the arguments to checkParallel() function
#inside the if Conditional statement.
if(checkParallel(a1,b1,c1,a2,b2,c2)):
  #If it is true then print those lines are parallel.
  print('The given lines are parallel to each other')
else:
  #Else they are not parallel.
  print('The given lines are not parallel to each other')

Output:

The given lines are parallel to each other

Method #2: Using For Loop (User Input)

Approach:

  • Give the values of a1,b1,c1 as user input using map(),int(),split() functions and store it in three separate variables.
  • Give the values of a2,b2,c2 as user input using map(),int(),split() functions and store it in three separate variables.
  • We can Check if the slopes of the given two lines are equal by formula (a1/b1 == a2/b2).
  • Create a function checkParallel() which accepts the 6 parameters (First and second line x,y,z coordinates)
  • Inside the checkParallel() function.
  • Check if both lines y coordinates are not equal to 0 using the If conditional Statement.
  • If it is true then check a1/b1 is equal to a2/b2 using another Nested If conditional Statement.
  • If it is true then return True
  • Else return False.
  • If parent If conditional statement is false then inside the else statement check if first line and second line x and y coordinates are equal or not using the nested If conditional statement.
  • If it is true then return True else return False.
  • Pass the a1,b1,c1,a2,b2,c2 as the arguments to checkParallel() function inside the if Conditional statement.
  • If it is true then print those lines are parallel.
  • Else they are not parallel.
  • The Exit of the Program.

Below is the implementation:

#Create a function checkParallel() which accepts the 6 parameters
#(First and second line x,y,z coordinates)
#We can Check if the slopes of the given two lines are equal by formula (a1/b1 == a2/b2).
def checkParallel(a1,b1,c1,a2,b2,c2):
    #Inside the checkParallel() function.
    #Check if both lines y coordinates are not equal to 0 
    #using the If conditional Statement.
    if(b1!=0 and b2!=0):
        #If it is true then check a1/b1 is equal to a2/b2 
        #using another Nested If conditional Statement.
        if(a1/b1==a2/b2):
            #If it is true then return True
            return True
        else:
            #Else return False.
            return False
    #If parent If conditional statement is false then inside the else statement 
    
    else:
      #check if first line and second line x and y coordinates are equal
      #or not using the nested If conditional statement.
      if(a1==a2 and b1==b2):
          #If it is true then return True else return False.
          return True
      else:
          return False
#Give the values of a1,b1,c1 as user input using map(),int(),split() functions 
#and store it in three separate variables.
a1,b1,c1=map(int,input('Enter some random x y z coordinates separated by spaces = ').split())                    
#Give the values of a2,b2,c2 as user input using map(),int(),split() functions 
#and store it in three separate variables.
a2,b2,c2=map(int,input('Enter some random x y z coordinates separated by spaces = ').split())
#Pass the a1,b1,c1,a2,b2,c2 as the arguments to checkParallel() function
#inside the if Conditional statement.
if(checkParallel(a1,b1,c1,a2,b2,c2)):
  #If it is true then print those lines are parallel.
  print('The given lines are parallel to each other')
else:
  #Else they are not parallel.
  print('The given lines are not parallel to each other')

Output:

Enter some random x y z coordinates separated by spaces = 8 0 9
Enter some random x y z coordinates separated by spaces = 8 0 11
The given lines are parallel to each other

They are parallel to each other

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.