给定六个整数, a 、 b 、 c 、 i 、 j和k表示圆的方程和直线方程 ,任务是找到从给定线到圆的截距长度。
例子:
Input: a = 0, b = 0, c = -4, i = 2, j = -1, k = 1
Output: 3.89872
Input: a = 5, b = 6, c = -16, i = 1, j = 4, k = 3
Output: 6.9282
处理方法:按照以下步骤解决问题:
- 找到圆心,说作为和 .
- 与中心的垂线将截距分成两个相等的部分,因此计算其中一个部分的长度并将其乘以 2 得到截距的总长度。
- 使用以下公式计算半径 (r)的值: , 在哪里和
- 使用以下公式计算中心O与直线的垂直距离 ( d ) 的值:
- 现在从三角OCA 中的毕达哥拉斯定理:
- 完成上述步骤后,打印两次AC的值,得到总截距的长度。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the
// radius of a circle
double radius(int a, int b, int c)
{
// g and f are the coordinates
// of the center
int g = a / 2;
int f = b / 2;
// Case of invalid circle
if (g * g + f * f - c < 0)
return (-1);
// Apply the radius formula
return (sqrt(g * g + f * f - c));
}
// Function to find the perpendicular
// distance between circle center and the line
double centerDistanceFromLine(int a, int b,
int i, int j,
int k)
{
// Store the coordinates of center
int g = a / 2;
int f = b / 2;
// Stores the perpendicular distance
// between the line and the point
double distance
= fabs(i * g + j * f + k)
/ (sqrt(i * i + j * j));
// Invalid Case
if (distance < 0)
return (-1);
// Return the distance
return distance;
}
// Function to find the length of intercept
// cut off from a line by a circle
void interceptLength(int a, int b, int c,
int i, int j,
int k)
{
// Calculate the value of radius
double rad = radius(a, b, c);
// Calculate the perpendicular distance
// between line and center
double dist = centerDistanceFromLine(
a, b, i, j, k);
// Invalid Case
if (rad < 0 || dist < 0) {
cout << "circle not possible";
return;
}
// If line do not cut circle
if (dist > rad) {
cout << "Line not cutting circle";
}
// Print the intercept length
else
cout << 2 * sqrt(
rad * rad - dist * dist);
}
// Driver Code
int main()
{
// Given Input
int a = 0, b = 0, c = -4;
int i = 2, j = -1, k = 1;
// Function Call
interceptLength(a, b, c, i, j, k);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to find the
// radius of a circle
static double radius(int a, int b, int c)
{
// g and f are the coordinates
// of the center
int g = a / 2;
int f = b / 2;
// Case of invalid circle
if (g * g + f * f - c < 0)
return (-1);
// Apply the radius formula
return (Math.sqrt(g * g + f * f - c));
}
// Function to find the perpendicular
// distance between circle center and the line
static double centerDistanceFromLine(int a, int b,
int i, int j,
int k)
{
// Store the coordinates of center
int g = a / 2;
int f = b / 2;
// Stores the perpendicular distance
// between the line and the point
double distance = Math.abs(i * g + j * f + k) /
(Math.sqrt(i * i + j * j));
// Invalid Case
if (distance < 0)
return (-1);
// Return the distance
return distance;
}
// Function to find the length of intercept
// cut off from a line by a circle
static void interceptLength(int a, int b, int c,
int i, int j, int k)
{
// Calculate the value of radius
double rad = radius(a, b, c);
// Calculate the perpendicular distance
// between line and center
double dist = centerDistanceFromLine(
a, b, i, j, k);
// Invalid Case
if (rad < 0 || dist < 0)
{
System.out.println("circle not possible");
return;
}
// If line do not cut circle
if (dist > rad)
{
System.out.println("Line not cutting circle");
}
// Print the intercept length
else
System.out.println(2 * Math.sqrt(
rad * rad - dist * dist));
}
// Driver code
public static void main(String[] args)
{
// Given Input
int a = 0, b = 0, c = -4;
int i = 2, j = -1, k = 1;
// Function Call
interceptLength(a, b, c, i, j, k);
}
}
// This code is contributed by abhinavjain194
Python3
# Python3 program for the above approach
import math
# Function to find the
# radius of a circle
def radius(a, b, c):
# g and f are the coordinates
# of the center
g = a / 2
f = b / 2
# Case of invalid circle
if (g * g + f * f - c < 0):
return(-1)
# Apply the radius formula
return(math.sqrt(g * g + f * f - c))
# Function to find the perpendicular
# distance between circle center and the line
def centerDistanceFromLine(a, b, i, j, k):
# Store the coordinates of center
g = a / 2
f = b / 2
# Stores the perpendicular distance
# between the line and the point
distance = (abs(i * g + j * f + k) /
(math.sqrt(i * i + j * j)))
# Invalid Case
if (distance < 0):
return (-1)
# Return the distance
return distance
# Function to find the length of intercept
# cut off from a line by a circle
def interceptLength(a, b, c, i, j, k):
# Calculate the value of radius
rad = radius(a, b, c)
# Calculate the perpendicular distance
# between line and center
dist = centerDistanceFromLine(
a, b, i, j, k)
# Invalid Case
if (rad < 0 or dist < 0):
print("circle not possible")
return
# If line do not cut circle
if (dist > rad):
print("Line not cutting circle")
# Print the intercept length
else:
print(2 * math.sqrt(
rad * rad - dist * dist))
# Driver Code
if __name__ == "__main__":
# Given Input
a = 0
b = 0
c = -4
i = 2
j = -1
k = 1
# Function Call
interceptLength(a, b, c, i, j, k)
# This code is contributed by ukasp
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the
// radius of a circle
static double radius(int a, int b, int c)
{
// g and f are the coordinates
// of the center
int g = a / 2;
int f = b / 2;
// Case of invalid circle
if (g * g + f * f - c < 0)
return (-1);
// Apply the radius formula
return(Math.Sqrt(g * g + f * f - c));
}
// Function to find the perpendicular
// distance between circle center and the line
static double centerDistanceFromLine(int a, int b,
int i, int j,
int k)
{
// Store the coordinates of center
int g = a / 2;
int f = b / 2;
// Stores the perpendicular distance
// between the line and the point
double distance = Math.Abs(i * g + j * f + k) /
(Math.Sqrt(i * i + j * j));
// Invalid Case
if (distance < 0)
return (-1);
// Return the distance
return distance;
}
// Function to find the length of intercept
// cut off from a line by a circle
static void interceptLength(int a, int b, int c,
int i, int j, int k)
{
// Calculate the value of radius
double rad = radius(a, b, c);
// Calculate the perpendicular distance
// between line and center
double dist = centerDistanceFromLine(
a, b, i, j, k);
// Invalid Case
if (rad < 0 || dist < 0)
{
Console.WriteLine("circle not possible");
return;
}
// If line do not cut circle
if (dist > rad)
{
Console.WriteLine("Line not cutting circle");
}
// Print the intercept length
else
Console.WriteLine(2 * Math.Sqrt(
rad * rad - dist * dist));
}
// Driver code
public static void Main(String []args)
{
// Given Input
int a = 0, b = 0, c = -4;
int i = 2, j = -1, k = 1;
// Function Call
interceptLength(a, b, c, i, j, k);
}
}
// This code is contributed by sanjoy_62
Javascript
输出:
3.89872
时间复杂度: O(1)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。