Python program to check if two lines are parallel or not
In this post, we will try to code a Python program to check if two lines are parallel or not. So what are parallel lines?
Two lines are said to be parallel if they remain the same distance apart over the entire length. They won’t meet how far you extend them. These lines are represented in the form of equations ax+by=c.
ax+by=c is the line equation where a is x coefficient and b is y coefficient. We say that two lines are parallel if the slopes are equal. So we have to find the slope which is “rise over run”.
y=mx+c is the straight-line equation where m is the slope. Let us take a1,b1,c1 and a2,b2,c2 from the user and check if they are parallel or not.
Check if two lines are parallel or not in Python
def are_parallel_lines(l1, l2): if(l1!=0 and l2!=0): if(l1/l1==l2/l2): return True else: return False else: if(l1==l2 and l1==l2): return True else: return False l1= l2= print("Enter the values of a1 b1 c1 :") for i in range(3): x=int(input()) l1.append(x) print("Enter the values of a2 b2 c2 :") for i in range(3): x=int(input()) l2.append(x) if(are_parallel_lines(l1,l2)==True): print("Yes") else: print("No")
The first thing to do is to find the slope that is a/b. If we consider our two lines then a1/b1 must be the slope of first-line and a2/b2 will be the second.
Both the slopes must be equal in order to become parallel. What if the b1 or b2 value is zero then there will be an error zero division error so that is what we are checking it in the first if condition.
we defined the function are_parallel_lines which takes two parameters l1,l2 which are lists and returns either true or false based on the conditions specified.
More Interesting programs