📜  从给定点到圆的法线方程

📅  最后修改于: 2021-10-23 08:46:48             🧑  作者: Mango

给定三个整数a, b, c代表方程x 2 + y 2 + ax + by + c = 0的系数 一个圆,任务是从给定的点(x 1 , y 1 )找到圆的法线方程。
注意:法线是在切线和曲线之间的接触点处垂直于切线的线。

例子:

处理方法:按照以下步骤解决问题:

  • 圆的法线穿过圆心。
  • 因此,找到圆心的坐标(g, f) ,其中g = a/2f = b/2
  • 由于圆心和绘制法线的点位于法线上,计算法线的斜率(m)m = (y 1 – f) / (x 1 – g) 
  • 因此,法线方程为y – y 1 = m * (x – x 1 )

下面是上述方法的实现:

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


输出:
y = 1.1x + 0.8

时间复杂度: O(1)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程