Perpendicular distance between a point and a Line in 2 D

Given a point (x1, y1) and a line (ax + by + c = 0). The task is to find the perpendicular distance between the given point and the line.

Examples :



Input: x1 = 5, y1 = 6, a = -2, b = 3, c = 4
Output:3.32820117735

Input: x1 = -1, y1 = 3, a = 4, b = -3, c = – 5
Output:3.6

Approach: The distance (i.e shortest distance) from a given point to a line is the perpendicular distance from that point to the given line. The equation of a line in the plane is given by the equation ax + by + c = 0, where a, b and c are real constants. the co-ordinate of the point is (x1, y1)

The formula for distance between a point and a line in 2-D is given by:



Distance = (| a*x1 + b*y1 + c |) / (sqrt( a*a + b*b))

Below is the implementation of the above formulae:

Program 1:

C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to find the distance 
// between a given point and a 
// given line in 2 D.
#include<stdio.h>
#include<math.h>
  
// Function to find distance
void shortest_distance(float x1, float y1,
                       float a, float b, 
                       float c)
{
    float d = fabs((a * x1 + b * y1 + c)) / 
             (sqrt(a * a + b * b));
    printf("Perpendicular distance is %f\n", d);
    return;
}
  
// Driver Code 
int main()
{
    float x1 = 5;
    float y1 = 6;
    float a = -2;
    float b = 3;
    float c = 4;
    shortest_distance(x1, y1, a, b, c);
    return 0;
}
  
// This code is contributed 
// by Amber_Saxena.

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find 
// the distance between 
// a given point and a 
// given line in 2 D.
import java.io.*;
  
class GFG 
{
      
    // Function to find distance
    static void shortest_distance(float x1, float y1,
                                  float a, float b,
                                  float c)
    {
        double d = Math.abs(((a * x1 + b * y1 + c)) / 
                  (Math.sqrt(a * a + b * b)));
        System.out.println("Perpendicular "
                         "distance is " + d);
        return;
    }
  
    // Driver code
    public static void main (String[] args)
    {
        float x1 = 5;
        float y1 = 6;
        float a = -2;
        float b = 3;
        float c = 4;
        shortest_distance(x1, y1, a, b, c);
    }
}
  
// This code is contributed
// by Mahadev.

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find the distance between
# a given point and a given line in 2 D.
  
import math
  
# Function to find distance
def shortest_distance(x1, y1, a, b, c): 
       
    d = abs((a * x1 + b * y1 + c)) / (math.sqrt(a * a + b * b))
    print("Perpendicular distance is"),d
      
  
# Driver Code 
x1 = 5
y1 = 6
a = -2
b = 3
c = 4
shortest_distance(x1, y1, a, b, c)  

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find 
// the distance between 
// a given point and a 
// given line in 2 D.
using System;
  
class GFG 
{
      
    // Function to find distance
    static void shortest_distance(float x1, float y1,
                                float a, float b,
                                float c)
    {
        double d = Math.Abs(((a * x1 + b * y1 + c)) / 
                (Math.Sqrt(a * a + b * b)));
        Console.WriteLine("Perpendicular "
                        "distance is " + d);
        return;
    }
  
    // Driver code
    public static void Main ()
    {
        float x1 = 5;
        float y1 = 6;
        float a = -2;
        float b = 3;
        float c = 4;
        shortest_distance(x1, y1, a, b, c);
    }
}
  
// This code is contributed
// by inder_verma..

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the distance 
// between a given point and a 
// given line in 2 D.
  
// Function to find distance
function shortest_distance($x1, $y1, $a, $b, $c)
{
    $d = abs(($a * $x1 + $b * $y1 + $c)) / 
               (sqrt($a * $a + $b * $b));
    echo"Perpendicular distance is ", $d;
}
  
// Driver Code 
$x1 = 5;
$y1 = 6;
$a = -2;
$b = 3;
$c = 4;
shortest_distance($x1, $y1, $a, $b, $c);
  
// This code is contributed 
// by inder_verma..
?>

chevron_right


Output:

Perpendicular distance is 3.32820117735

Program 2:

C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to find the distance 
// between a given point and a 
// given line in 2 D.
#include<stdio.h>
#include<math.h>
  
// Function to find distance
void shortest_distance(float x1, float y1,
                       float a, float b, 
                       float c)
{
    float d = fabs((a * x1 + b * y1 + c)) / 
              (sqrt(a * a + b * b));
    printf("Perpendicular distance is %f\n", d);
    return;
}
  
// Driver Code 
int main()
{
    float x1 = -1;
    float y1 = 3;
    float a = 4;
    float b = -3;
    float c = - 5;
    shortest_distance(x1, y1, a, b, c);
    return 0;
}
  
// This code is contributed 
// by Amber_Saxena.

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the distance 
// between a given point and a 
// given line in 2 D.
class GFG
{
// Function to find distance
static void shortest_distance(double x1, double y1,
                              double a, double b, 
                              double c)
{
    double d = Math.abs((a * x1 + b * y1 + c)) / 
              (Math.sqrt(a * a + b * b));
    System.out.println("Perpendicular distance is " + d);
    return;
}
  
// Driver Code 
public static void main(String[] args)
{
    double x1 = -1;
    double y1 = 3;
    double a = 4;
    double b = -3;
    double c = - 5;
    shortest_distance(x1, y1, a, b, c);
}
}
  
// This code is contributed 
// by mits

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find the distance between
# a given point and a given line in 2 D.
  
import math
  
# Function to find distance
def shortest_distance(x1, y1, a, b, c): 
       
    d = abs((a * x1 + b * y1 + c)) / (math.sqrt(a * a + b * b))
    print("Perpendicular distance is"),d
     
  
# Driver Code 
x1 = -1
y1 = 3
a = 4
b = -3
c = - 5 
shortest_distance(x1, y1, a, b, c)  

chevron_right


C#

// C# program to find the distance
// between a given point and a
// given line in 2 D.
using System;

class GFG
{
// Function to find distance
static void shortest_distance(double x1, double y1,
double a, double b,
double c)
{
double d = Math.Abs((a * x1 + b * y1 + c)) /
(Math.Sqrt(a * a + b * b));
Console.WriteLine(“Perpendicular distance is ” + d);
return;
}

// Driver Code
public static void Main()
{
double x1 = -1;
double y1 = 3;
double a = 4;
double b = -3;
double c = – 5;
shortest_distance(x1, y1, a, b, c);
}
}

// This code is contributed
// by Akanksha Rai

PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the distance 
// between a given point and a 
// given line in 2 D.
  
// Function to find distance
function shortest_distance($x1, $y1, $a,
                           $b, $c)
{
    $d = abs((int)($a * $x1 + $b * $y1 + $c) / 
              sqrt($a * $a + $b * $b));
    echo"Perpendicular distance is ", $d;
}
  
// Driver Code 
$x1 = -1;
$y1 = 3;
$a = 4;
$b = -3;
$c = -5;
shortest_distance($x1, $y1, $a, $b, $c);
  
// This code is contributed 
// by inder_verma..
?>

chevron_right


Output:

Perpendicular distance is 3.6


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.