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 = 3Output:0.333333Input:m = -4, b1 = 11, b2 = 23Output:0.8

- 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++

`// C++ program find the distance ` `// between two parallel lines ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the distance ` `// between parallel lines ` `double` `dist(` `double` `m, ` `double` `b1, ` `double` `b2) ` `{ ` ` ` `double` `d = ` `fabs` `(b2 - b1) / ((m * m) - 1); ` ` ` `return` `d; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `double` `m = 2, b1 = 4, b2 = 3; ` ` ` `cout << dist(m, b1, b2); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program find the distance ` `// between two parallel lines ` `class` `GFG ` `{ ` ` ` `// Function to find the distance ` `// between parallel lines ` `static` `double` `dist(` `double` `m, ` ` ` `double` `b1, ` `double` `b2) ` `{ ` ` ` `double` `d = Math.abs(b2 - b1) / ` ` ` `((m * m) - ` `1` `); ` ` ` `return` `d; ` `} ` ` ` `// Driver Code ` `public` `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. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program find the distance ` `# between two parallel lines ` ` ` `# Function to find the distance ` `# between parallel lines ` `def` `dist(m, b1, b2): ` ` ` `d ` `=` `abs` `(b2 ` `-` `b1) ` `/` `((m ` `*` `m) ` `-` `1` `); ` ` ` `return` `d; ` ` ` `# Driver Code ` `def` `main(): ` ` ` `m, b1, b2 ` `=` `2` `,` `4` `, ` `3` `; ` ` ` `print` `(dist(m, b1, b2)); ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `main() ` ` ` `# This code contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## C#

`// C# program find the distance ` `// between two parallel lines ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the distance ` `// between parallel lines ` `static` `double` `dist(` `double` `m, ` ` ` `double` `b1, ` `double` `b2) ` `{ ` ` ` `double` `d = Math.Abs(b2 - b1) / ` ` ` `((m * m) - 1); ` ` ` `return` `d; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `double` `m = 2, b1 = 4, b2 = 3; ` ` ` `Console.Write(dist(m, b1, b2)); ` `} ` `} ` ` ` `// This code is contributed by Akanksha Rai ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program find the distance ` `// between two parallel lines ` ` ` `// Function to find the distance ` `// between parallel lines ` `function` `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 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

0.333333

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of lines from given N points not parallel to X or Y axis
- Find whether only two parallel lines contain all coordinates points or not
- Count of rectangles possible from N and M straight lines parallel to X and Y axis respectively
- Maximum number of region in which N non-parallel lines can divide a plane
- Number of parallelograms when n horizontal parallel lines intersect m vertical parallellines
- Distance between two parallel Planes in 3-D
- Count paths with distance equal to Manhattan distance
- Missing vertex among N axis-parallel rectangles
- Maximum Squares possible parallel to both axes from N distinct points
- Count of Squares that are parallel to the coordinate axis from the given set of N points
- Find Four points such that they form a square whose sides are parallel to x and y axes
- Find the equation of plane which passes through two points and parallel to a given axis
- Coplanarity of Two Lines in 3D Geometry
- Find the maximum value of Y for a given X from given set of lines
- Angle between a Pair of Lines in 3D
- Check whether two straight lines are orthogonal or not
- Maximum points of intersection n lines
- Check if given two straight lines are identical or not
- Check if three straight lines are concurrent or not
- Minimum lines to cover all points

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.