Related Articles

# Equation of a normal to a Circle from a given point

• Last Updated : 11 Jun, 2021

Given three integers a, b, c representing coefficients of the equation x2 + y2 + ax + by + c = 0 of a circle, the task is to find the equation of the normal to the circle from a given point (x1, y1).
Note: Normal is a line perpendicular to the tangent at the point of contact between the tangent and the curve.

Examples:

Input: a = 4, b = 6, c = 5, x1 = 12, y1 = 14
Output: y = 1.1x + 0.8

Input: a = 6, b = 12, c = 5, x1 = 9, y1 = 3
Output: y = -0.5x + 7.5

Approach: Follow the steps below to solve the problem:

• The normal to a circle passes through the center of the circle.
• Therefore, find the coordinates of the center of the circle (g, f), where g = a/2 and f = b/2.
• Since the center of the circle and the point where the normal is drawn lie on the normal, calculate the slope of the normal (m) as m = (y1 – f) / (x1 – g).
• Hence, the equation of the normal is y – y1 = m * (x – x1).

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to calculate the slope``double` `normal_slope(``double` `a, ``double` `b,``                    ``double` `x1, ``double` `y1)``{``    ``// Store the coordinates``    ``// the center of the circle``    ``double` `g = a / 2;``    ``double` `f = b / 2;` `    ``// If slope becomes infinity``    ``if` `(g - x1 == 0)``        ``return` `(-1);` `    ``// Stores the slope``    ``double` `slope = (f - y1) / (g - x1);` `    ``// If slope is zero``    ``if` `(slope == 0)``        ``return` `(-2);` `    ``// Return the result``    ``return` `slope;``}` `// Function to find the equation of the``// normal to a circle from a given point``void` `normal_equation(``double` `a, ``double` `b,``                     ``double` `x1, ``double` `y1)``{``    ``// Stores the slope of the normal``    ``double` `slope = normal_slope(a, b, x1, y1);` `    ``// If slope becomes infinity``    ``if` `(slope == -1) {``        ``cout << ``"x = "` `<< x1;``    ``}` `    ``// If slope is zero``    ``if` `(slope == -2) {``        ``cout << ``"y = "` `<< y1;``    ``}` `    ``// Otherwise, print the``    ``// equation of the normal``    ``if` `(slope != -1 && slope != -2) {` `        ``x1 *= -slope;``        ``x1 += y1;` `        ``if` `(x1 > 0)``            ``cout << ``"y = "` `<< slope``                 ``<< ``"x + "` `<< x1;``        ``else``            ``cout << ``"y = "` `<< slope``                 ``<< ``"x "` `<< x1;``    ``}``}` `// Driver Code``int` `main()``{``    ``// Given Input``    ``int` `a = 4, b = 6, c = 5;``    ``int` `x1 = 12, y1 = 14;` `    ``// Function Call``    ``normal_equation(a, b, x1, y1);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{` `// Function to calculate the slope``static` `double` `normal_slope(``double` `a, ``double` `b,``                           ``double` `x1, ``double` `y1)``{``    ` `    ``// Store the coordinates``    ``// the center of the circle``    ``double` `g = a / ``2``;``    ``double` `f = b / ``2``;` `    ``// If slope becomes infinity``    ``if` `(g - x1 == ``0``)``        ``return` `(-``1``);` `    ``// Stores the slope``    ``double` `slope = (f - y1) / (g - x1);` `    ``// If slope is zero``    ``if` `(slope == ``0``)``        ``return` `(-``2``);` `    ``// Return the result``    ``return` `slope;``}` `// Function to find the equation of the``// normal to a circle from a given point``static` `void` `normal_equation(``double` `a, ``double` `b,``                            ``double` `x1, ``double` `y1)``{``    ` `    ``// Stores the slope of the normal``    ``double` `slope = normal_slope(a, b, x1, y1);` `    ``// If slope becomes infinity``    ``if` `(slope == -``1``)``    ``{``        ``System.out.print(``"x = "` `+  x1);``    ``}` `    ``// If slope is zero``    ``if` `(slope == -``2``)``    ``{``        ``System.out.print(``"y = "` `+  y1);``    ``}` `    ``// Otherwise, print the``    ``// equation of the normal``    ``if` `(slope != -``1` `&& slope != -``2``)``    ``{``        ``x1 *= -slope;``        ``x1 += y1;` `        ``if` `(x1 > ``0``)``            ``System.out.print(``"y = "` `+  slope +``                             ``"x + "` `+  x1);``        ``else``            ``System.out.print(``"y = "` `+  slope +``                             ``"x "` `+  x1);``    ``}``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ` `    ``// Given Input``    ``int` `a = ``4``, b = ``6``;``    ``int` `x1 = ``12``, y1 = ``14``;` `    ``// Function Call``    ``normal_equation(a, b, x1, y1);``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 program for the above approach` `# Function to calculate the slope``def` `normal_slope(a, b, x1, y1):``  ` `    ``# Store the coordinates``    ``# the center of the circle``    ``g ``=` `a ``/` `2``    ``f ``=` `b ``/` `2` `    ``# If slope becomes infinity``    ``if` `(g ``-` `x1 ``=``=` `0``):``        ``return` `(``-``1``)` `    ``# Stores the slope``    ``slope ``=` `(f ``-` `y1) ``/` `(g ``-` `x1)` `    ``# If slope is zero``    ``if` `(slope ``=``=` `0``):``        ``return` `(``-``2``)` `    ``# Return the result``    ``return` `slope` `# Function to find the equation of the``# normal to a circle from a given point``def` `normal_equation(a, b, x1, y1):` `    ``# Stores the slope of the normal``    ``slope ``=` `normal_slope(a, b, x1, y1)` `    ``# If slope becomes infinity``    ``if` `(slope ``=``=` `-``1``) :``        ``print``(``"x = "``, x1)` `    ``# If slope is zero``    ``if` `(slope ``=``=` `-``2``) :``        ``print``(``"y = "``, y1)` `    ``# Otherwise, print the``    ``# equation of the normal``    ``if` `(slope !``=` `-``1` `and` `slope !``=` `-``2``):` `        ``x1 ``*``=` `-``slope``        ``x1 ``+``=` `y1` `        ``if` `(x1 > ``0``) :``            ``print``(``"y = "``, slope, ``"x + "``, x1)``        ``else` `:``            ``print``(``"y = "``, slope, ``"x "``, x1)` `# Driver Code` `# Given Input``a ``=` `4``b ``=` `6``c ``=` `5``x1 ``=` `12``y1 ``=` `14` `# Function Call``normal_equation(a, b, x1, y1)` `# This code is contributed by Dharanendra L V.`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{``    ` `// Function to calculate the slope``static` `double` `normal_slope(``double` `a, ``double` `b,``                           ``double` `x1, ``double` `y1)``{``    ` `    ``// Store the coordinates``    ``// the center of the circle``    ``double` `g = a / 2;``    ``double` `f = b / 2;`` ` `    ``// If slope becomes infinity``    ``if` `(g - x1 == 0)``        ``return` `(-1);`` ` `    ``// Stores the slope``    ``double` `slope = (f - y1) / (g - x1);`` ` `    ``// If slope is zero``    ``if` `(slope == 0)``        ``return` `(-2);`` ` `    ``// Return the result``    ``return` `slope;``}`` ` `// Function to find the equation of the``// normal to a circle from a given point``static` `void` `normal_equation(``double` `a, ``double` `b,``                            ``double` `x1, ``double` `y1)``{``    ` `    ``// Stores the slope of the normal``    ``double` `slope = normal_slope(a, b, x1, y1);`` ` `    ``// If slope becomes infinity``    ``if` `(slope == -1)``    ``{``         ``Console.WriteLine( ``"x = "` `+ x1);``    ``}`` ` `    ``// If slope is zero``    ``if` `(slope == -2)``    ``{``         ``Console.WriteLine(``"y = "` `+ y1);``    ``}`` ` `    ``// Otherwise, print the``    ``// equation of the normal``    ``if` `(slope != -1 && slope != -2)``    ``{``        ``x1 *= -slope;``        ``x1 += y1;`` ` `        ``if` `(x1 > 0)``            ``Console.WriteLine(``"y = "` `+ slope +``                              ``"x +"` `+ Math.Round(x1, 2));``        ``else``            ``Console.WriteLine(``"y = "` `+ slope +``                              ``"x "` `+ Math.Round(x1, 2));``    ``}``}` `// Driver code``public` `static` `void` `Main(String []args)``{``    ` `    ``// Given Input``    ``int` `a = 4, b = 6;``    ``//int c = 5;``    ` `    ``int` `x1 = 12, y1 = 14;`` ` `    ``// Function Call``    ``normal_equation(a, b, x1, y1);``}``}` `// This code is contributed by sanjoy_62`

## Javascript

 ``
Output:
`y = 1.1x + 0.8`

Time Complexity: O(1)
Auxiliary Space: O(1)

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up