Equation of a normal to a Circle from a given point
Last Updated :
20 Oct, 2022
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++
#include <bits/stdc++.h>
using namespace std;
double normal_slope( double a, double b,
double x1, double y1)
{
double g = a / 2;
double f = b / 2;
if (g - x1 == 0)
return (-1);
double slope = (f - y1) / (g - x1);
if (slope == 0)
return (-2);
return slope;
}
void normal_equation( double a, double b,
double x1, double y1)
{
double slope = normal_slope(a, b, x1, y1);
if (slope == -1) {
cout << "x = " << x1;
}
if (slope == -2) {
cout << "y = " << y1;
}
if (slope != -1 && slope != -2) {
x1 *= -slope;
x1 += y1;
if (x1 > 0)
cout << "y = " << slope
<< "x + " << x1;
else
cout << "y = " << slope
<< "x " << x1;
}
}
int main()
{
int a = 4, b = 6, c = 5;
int x1 = 12, y1 = 14;
normal_equation(a, b, x1, y1);
return 0;
}
|
Java
import java.util.*;
class GFG{
static double normal_slope( double a, double b,
double x1, double y1)
{
double g = a / 2 ;
double f = b / 2 ;
if (g - x1 == 0 )
return (- 1 );
double slope = (f - y1) / (g - x1);
if (slope == 0 )
return (- 2 );
return slope;
}
static void normal_equation( double a, double b,
double x1, double y1)
{
double slope = normal_slope(a, b, x1, y1);
if (slope == - 1 )
{
System.out.print( "x = " + x1);
}
if (slope == - 2 )
{
System.out.print( "y = " + y1);
}
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);
}
}
public static void main(String[] args)
{
int a = 4 , b = 6 ;
int x1 = 12 , y1 = 14 ;
normal_equation(a, b, x1, y1);
}
}
|
Python3
def normal_slope(a, b, x1, y1):
g = a / 2
f = b / 2
if (g - x1 = = 0 ):
return ( - 1 )
slope = (f - y1) / (g - x1)
if (slope = = 0 ):
return ( - 2 )
return slope
def normal_equation(a, b, x1, y1):
slope = normal_slope(a, b, x1, y1)
if (slope = = - 1 ) :
print ( "x = " , x1)
if (slope = = - 2 ) :
print ( "y = " , y1)
if (slope ! = - 1 and slope ! = - 2 ):
x1 * = - slope
x1 + = y1
if (x1 > 0 ) :
print ( "y = " , slope, "x + " , x1)
else :
print ( "y = " , slope, "x " , x1)
a = 4
b = 6
c = 5
x1 = 12
y1 = 14
normal_equation(a, b, x1, y1)
|
C#
using System;
class GFG{
static double normal_slope( double a, double b,
double x1, double y1)
{
double g = a / 2;
double f = b / 2;
if (g - x1 == 0)
return (-1);
double slope = (f - y1) / (g - x1);
if (slope == 0)
return (-2);
return slope;
}
static void normal_equation( double a, double b,
double x1, double y1)
{
double slope = normal_slope(a, b, x1, y1);
if (slope == -1)
{
Console.WriteLine( "x = " + x1);
}
if (slope == -2)
{
Console.WriteLine( "y = " + y1);
}
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));
}
}
public static void Main(String []args)
{
int a = 4, b = 6;
int x1 = 12, y1 = 14;
normal_equation(a, b, x1, y1);
}
}
|
Javascript
<script>
function normal_slope( a, b, x1, y1)
{
var g = a / 2;
var f = b / 2;
if (g - x1 == 0)
return (-1);
var slope = (f - y1) / (g - x1);
if (slope == 0)
return (-2);
return slope;
}
function normal_equation( a, b, x1, y1)
{
var slope = normal_slope(a, b, x1, y1);
if (slope == -1) {
document.write( "x = " + x1);
}
if (slope == -2) {
document.write( "y = " + y1);
}
if (slope != -1 && slope != -2) {
x1 *= -slope;
x1 += y1;
if (x1 > 0)
document.write(
"y = " + slope + "x + " + x1.toFixed(1)
);
else
document.write(
"y = " + slope+ "x " + x1.toFixed(1)
);
}
}
var a = 4, b = 6, c = 5;
var x1 = 12, y1 = 14;
normal_equation(a, b, x1, y1);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...