Equation of parabola from its focus and directrix

We are given focus(x, y) and directrix(ax + by + c) of a parabola and we have to find the equation of parabola using its focus and directrix.

Examples :

Input: x1 = 0, y1 = 0, a = 2, b = 1, c = 2
Output: equation of parabola is 16.0 x^2 + 9.0 y^2 + -12.0 x + 16.0 y + 24.0 xy + -4.0 = 0.



Input: x1 = -1, y1 = -2, a = 1, b = -2, c = 3
Output:equation of parabola is 4.0 x^2 + 1.0 y^2 + 4.0 x + 32.0 y + 4.0 xy + 16.0 = 0.

Let P(x, y) be any point on the parabola whose focus S(x1, y1) and the directrix is the straight line ax + by + c =0.
Draw PM perpendicular from P on the directrix. then by definition pf parabola distance SP = PM
SP^2 = PM^2

(x - x1)^2 + (y - y1)^2 = ( ( a*x + b*y + c ) / (sqrt( a*a + b*b )) ) ^ 2

// let ( a*a + b*b ) = t

x^2 + x1^2 - 2*x1*x + y^2 + y1^2 - 2*y1*y  = ( ( a*x + b*y + c ) ^ 2 )/ t

on cross multiplying above we get

t*x^2 + t*x1^2 - 2*t*x1*x + t*y^2 + t*y1^2 - 2*t*y1*y  = ( ( a*x + b*y + c ) ^ 2 )  
t*x^2 + t*x1^2 - 2*t*x1*x + t*y^2 + t*y1^2 - 2*t*y1*y  = a^2*x^2 + b^2*y^2 + 2*a*x*b*y + c^2 + 2*c*(a*x + b*y)
t*x^2 + t*x1^2 - 2*t*x1*x + t*y^2 + t*y1^2 - 2*t*y1*y  = a^2*x^2 + b^2*y^2 + 2*a*x*b*y + c^2 + 2*c*a*x + 2*c*b*y
t*x^2 - a^2*x^2 +  t*y^2 - b^2*y^2 - 2*t*x1*x - 2*c*a*x - 2*t*y1*y - 2*c*b*y - 2*a*x*b*y - c^2  + t*x1^2 + t*y1^2 =0.

This can be compared with general form that is

a*x^2 + 2*h*x*y + b*y^2 + 2*g*x + 2*f*y + c = 0.

Below is the implementation of the above :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find equation of a parbola
// using focus and directrix.
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <math.h>
  
using namespace std;
  
// Function to find equation of parabola.
void equation_parabola(float x1, float y1,
                       float a, float b, float c)
{
    float t = a * a + b * b;
    float a1 = t - (a * a);
    float b1 = t - (b * b);
    float c1 = (-2 * t * x1) - (2 * c * a);
    float d1 = (-2 * t * y1) - (2 * c * b);
    float e1 = -2 * a * b;
    float f1 = (-c * c) + (t * x1 * x1) + (t * y1 * y1);
    std::cout << std::fixed;
    std::cout << std::setprecision(1);
    cout << "equation of parabola is " << a1 
         << " x^2 + " << b1 << " y^2 + " 
         << c1 << " x + " << d1 << " y + " 
         << e1 << " xy + " << f1 << " = 0.";
}
  
// Driver Code
int main()
{
    float x1 = 0;
    float y1 = 0;
    float a = 3;
    float b = -4;
    float c = 2;
    equation_parabola(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 equation of a parbola 
// using focus and directrix. 
import java.util.*; 
  
class solution 
  
//Function to find equation of parabola. 
static void equation_parabola(float x1, float y1, 
                    float a, float b, float c) 
    float t = a * a + b * b; 
    float a1 = t - (a * a); 
    float b1 = t - (b * b); 
    float c1 = (-2 * t * x1) - (2 * c * a); 
    float d1 = (-2 * t * y1) - (2 * c * b); 
    float e1 = -2 * a * b; 
    float f1 = (-c * c) + (t * x1 * x1) + (t * y1 * y1); 
    System.out.println( "equation of parabola is "+ a1+ 
                        " x^2 + " +b1 +" y^2 + "
                        c1 + " x + " +d1 + " y + " 
                        + e1+" xy + " + f1 +" = 0."); 
  
  
// Driver Code 
public static void main(String arr[]) 
    float x1 = 0
    float y1 = 0
    float a = 3
    float b = -4
    float c = 2
    equation_parabola(x1, y1, a, b, c); 
  
  

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find equation of a parbola 
# using focus and directrix. 
  
# Function to find equation of parabola. 
def equation_parabola(x1, y1, a, b, c) :
   
    t = a * a + b * b
    a1 = t - (a * a)
    b1 = t - (b * b); 
    c1 = (-2 * t * x1) - (2 * c * a) 
    d1 = (-2 * t * y1) - (2 * c * b)
    e1 = -2 * a * b
    f1 = (-c * c) + (t * x1 * x1) + (t * y1 * y1)
    print("equation of parabola is", a1 ,"x^2 +" ,b1,
    "y^2 +",c1,"x +", d1,"y + ",e1 ,"xy +",f1,"= 0."
  
  
# Driver Code 
if __name__ == "__main__"
  
    x1, y1, a, b, c = 0,0,3,-4,2
    equation_parabola(x1, y1, a, b, c) 
  
# This code is contributed by Ryuga

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find equation of a parbola 
// using focus and directrix. 
using System;
  
class solution 
  
//Function to find equation of parabola. 
static void equation_parabola(float x1, float y1, 
                    float a, float b, float c) 
    float t = a * a + b * b; 
    float a1 = t - (a * a); 
    float b1 = t - (b * b); 
    float c1 = (-2 * t * x1) - (2 * c * a); 
    float d1 = (-2 * t * y1) - (2 * c * b); 
    float e1 = -2 * a * b; 
    float f1 = (-c * c) + (t * x1 * x1) + (t * y1 * y1); 
    Console.WriteLine( "equation of parabola is "+ a1+ 
                        " x^2 + " +b1 +" y^2 + "
                        c1 + " x + " +d1 + " y + "
                        + e1+" xy + " + f1 +" = 0."); 
  
  
// Driver Code 
public static void Main() 
    float x1 = 0; 
    float y1 = 0; 
    float a = 3; 
    float b = -4; 
    float c = 2; 
    equation_parabola(x1, y1, a, b, c); 
  
// This Code is contributed
// by shs
  

chevron_right


Output:

equation of parabola is 16.0 x^2 + 9.0 y^2 + -12.0 x + 16.0 y + 24.0 xy + -4.0 = 0.


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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.