Distance between two parallel lines - GeeksforGeeks

18 Mar, 2021
Given are two parallel straight lines with slope m, and different y-intercepts b1 & b2.The task is to find the distance between these two parallel lines.Examples:  
Input: m = 2, b1 = 4, b2 = 3
Output: 0.333333

Input: m = -4, b1 = 11, b2 = 23
Output: 0.8
 
Approach: 
Let PQ and RS be the parallel lines, with equations y = mx + b1 y = mx + b2 The distance between these two lines is the distance between the two intersection points of these lines with the perpendicular line.Let that distance be d.  So, equation of the line perpendicular to PQ and RS can be y = -x/m  Now, solving the perpendicular line with PQ and RS separately to get the intersecting points (x1, y1) & (x2, y2), we get,  From PQ, y = mx + b1 y = -x/m (x1, y1) = ( -b1*m/(m^2 + 1), b1/(m^2 + 1))  From RS, y = mx + b2 y = -x/m (x2, y2) = ( -b2*m/(m^2 + 1), b2/(m^2 + 1))  So, d = distance between (x1, y1) and (x2, y2)  
Let PQ and RS be the parallel lines, with equations y = mx + b1 y = mx + b2 
The distance between these two lines is the distance between the two intersection points of these lines with the perpendicular line.Let that distance be d.  
So, equation of the line perpendicular to PQ and RS can be y = -x/m  
Now, solving the perpendicular line with PQ and RS separately to get the intersecting points (x1, y1) & (x2, y2), we get,  
From PQ, y = mx + b1 y = -x/m (x1, y1) = ( -b1*m/(m^2 + 1), b1/(m^2 + 1))  
From RS, y = mx + b2 y = -x/m (x2, y2) = ( -b2*m/(m^2 + 1), b2/(m^2 + 1))  
So, d = distance between (x1, y1) and (x2, y2)  
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program find the distance// between two parallel lines #include <bits/stdc++.h>using namespace std; // Function to find the distance// between parallel linesdouble dist(double m, double b1, double b2){    double d = fabs(b2 - b1) / ((m * m) - 1);    return d;} // Driver Codeint main(){    double m = 2, b1 = 4, b2 = 3;    cout << dist(m, b1, b2);    return 0;}
// Java program find the distance// between two parallel linesclass GFG{     // Function to find the distance// between parallel linesstatic double dist(double m,                double b1, double b2){    double d = Math.abs(b2 - b1) /                    ((m * m) - 1);    return d;} // Driver Codepublic static void main(String[] args){    double m = 2, b1 = 4, b2 = 3;     System.out.println(dist(m, b1, b2));}} // This code is contributed by Code_Mech.
# Python3 program find the distance# between two parallel lines # Function to find the distance# between parallel linesdef dist(m, b1, b2):    d = abs(b2 - b1) / ((m * m) - 1);    return d; # Driver Codedef main():    m, b1, b2 =2,4, 3;    print(dist(m, b1, b2));if __name__ == '__main__':    main() # This code contributed by PrinciRaj1992
// C# program find the distance// between two parallel linesusing System; class GFG{     // Function to find the distance// between parallel linesstatic double dist(double m,                   double b1, double b2){    double d = Math.Abs(b2 - b1) /                      ((m * m) - 1);    return d;} // Driver Codepublic static void Main(){    double m = 2, b1 = 4, b2 = 3;    Console.Write(dist(m, b1, b2));}} // This code is contributed by Akanksha Rai
<?php// PHP program find the distance// between two parallel lines // Function to find the distance// between parallel linesfunction dist($m, $b1, $b2){    $d = abs($b2 - $b1) / (($m * $m) - 1);    return $d;} // Driver Code$m = 2;$b1 = 4;$b2 = 3; echo dist($m, $b1, $b2); // This code is contributed by Ryuga?>
<script> // javascript program find the distance// between two parallel lines      // Function to find the distance// between parallel linesfunction dist(m, b1 , b2){    var d = Math.abs(b2 - b1) /                    ((m * m) - 1);    return d;} // Driver Codevar m = 2, b1 = 4, b2 = 3;document.write(dist(m, b1, b2).toFixed(5));  // This code contributed by Princi Singh </script>
0.333333
 
ankthon
Akanksha_Rai
Code_Mech
princiraj1992
princi singh
school-programming
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convex Hull using Divide and Conquer Algorithm
Equation of circle when three points on the circle are given
Circle and Lattice Points
Orientation of 3 ordered points
Program to find slope of a line
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7