给定两个整数A和B表示椭圆的半长轴和半短轴的长度,任务是计算椭圆中所刻的任何三角形与由其辅助圆上的相应点形成的三角形的比率。
例子:
Input: A = 1, B = 2
Output: 2
Explanation: Ratio = B / A = 2 / 1 = 2
Input: A = 2, B = 3
Output: 1.5
方法:
这个想法基于以下数学公式:
- 令椭圆上的3个点为P(a cosX,b sinX),Q(a cosY,b sinY),R(a cosZ,b sinZ)。
- 因此,辅助圆上的对应点是A(a cosX,sinX),B(a cosY,sinY),C(a cosZ,sinZ)。
- 现在,使用公式使用三角形的给定点计算三角形的面积。
Area(PQR) / Area(ABC) = b / a
请按照以下步骤解决问题:
- 将椭圆的半长轴与半短轴之比存储在变量中,例如结果。
- 将结果值打印为所需答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate ratio of a
// triangle inscribed in an ellipse to
// the triangle on the auxiliary circle
void triangleArea(int a, int b)
{
// Stores the ratio of the
// semi-major to semi-minor axes
double ratio = (double)b / a;
// Print the ratio
cout << ratio;
}
// Driver Code
int main()
{
int a = 1, b = 2;
triangleArea(a, b);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to calculate ratio of a
// triangle inscribed in an ellipse to
// the triangle on the auxiliary circle
static void triangleArea(int a, int b)
{
// Stores the ratio of the
// semi-major to semi-minor axes
double ratio = (double)b / a;
// Print the ratio
System.out.println(ratio);
}
// Driver Code
public static void main(String args[])
{
int a = 1, b = 2;
triangleArea(a, b);
}
}
// This code is contributed by AnkThon
Python3
# Python3 program for the above approach
# Function to calculate ratio of a
# triangle inscribed in an ellipse to
# the triangle on the auxiliary circle
def triangleArea(a, b):
# Stores the ratio of the
# semi-major to semi-minor axes
ratio = b / a
# Print the ratio
print(ratio)
# Driver Code
if __name__ == "__main__" :
a = 1
b = 2
triangleArea(a, b)
# This code is contributed by AnkThon
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to calculate ratio of a
// triangle inscribed in an ellipse to
// the triangle on the auxiliary circle
static void triangleArea(int a, int b)
{
// Stores the ratio of the
// semi-major to semi-minor axes
double ratio = (double)b / a;
// Print the ratio
Console.WriteLine(ratio);
}
// Driver Code
public static void Main()
{
int a = 1, b = 2;
triangleArea(a, b);
}
}
// This code is contributed by bgangwar59
Javascript
输出:
2
时间复杂度: O(1)
辅助空间: O(1)