给定一个椭圆,主轴长度为2a和2b ,任务是找到可以在其中刻出的最大三角形的面积。
例子:
Input: a = 4, b = 2
Output: 10.3923
Input: a = 5, b = 3
Output: 10.8253
方法:所以我们知道椭圆只是一个圆的缩放阴影,让我们找到缩放因子。
x^2/a^2 + y^2/b^2 = 1 is an ellipse. Rewrite this as:
(y*(a/b))^2+x^2 = a^2
这只是一个垂直缩小的半径为a的圆(认为光线从顶部以一定角度落下),垂直系数为a / b 。然后,椭圆中的最大三角形是该圆形中最大三角形的放大版本。通过使用少量几何图形并考虑对称性,我们可以理解最大的三角形是等边三角形。侧面为√3a ,面积为(3√3)a ^ 2/4
将其转换为椭圆项–我们将水平尺寸按a / b比例放大,椭圆中最大三角形的面积为,
A = (3√3)a^2/4b
下面是上述方法的实现:
C++
// C++ Program to find the biggest triangle
// which can be inscribed within the ellipse
#include
using namespace std;
// Function to find the area
// of the triangle
float trianglearea(float a, float b)
{
// a and b cannot be negative
if (a < 0 || b < 0)
return -1;
// area of the triangle
float area = (3 * sqrt(3) * pow(a, 2)) / (4 * b);
return area;
}
// Driver code
int main()
{
float a = 4, b = 2;
cout << trianglearea(a, b) << endl;
return 0;
}
Java
//Java Program to find the biggest triangle
//which can be inscribed within the ellipse
public class GFG {
//Function to find the area
//of the triangle
static float trianglearea(float a, float b)
{
// a and b cannot be negative
if (a < 0 || b < 0)
return -1;
// area of the triangle
float area = (float)(3 * Math.sqrt(3) * Math.pow(a, 2)) / (4 * b);
return area;
}
//Driver code
public static void main(String[] args) {
float a = 4, b = 2;
System.out.println(trianglearea(a, b));
}
}
Python3
# Python 3 Program to find the biggest triangle
# which can be inscribed within the ellipse
from math import *
# Function to find the area
# of the triangle
def trianglearea(a, b) :
# a and b cannot be negative
if a < 0 or b < 0 :
return -1
# area of the triangle
area = (3 * sqrt(3) * pow(a, 2)) / (4 * b)
return area
# Driver Code
if __name__ == "__main__" :
a, b = 4, 2
print(round(trianglearea(a, b),4))
# This code is contributed by ANKITRAI1
C#
// C# Program to find the biggest
// triangle which can be inscribed
// within the ellipse
using System;
class GFG
{
// Function to find the area
// of the triangle
static float trianglearea(float a, float b)
{
// a and b cannot be negative
if (a < 0 || b < 0)
return -1;
// area of the triangle
float area = (float)(3 * Math.Sqrt(3) *
Math.Pow(a, 2)) / (4 * b);
return area;
}
// Driver code
public static void Main()
{
float a = 4, b = 2;
Console.WriteLine(trianglearea(a, b));
}
}
// This code is contributed
// by Akanksha Rai(Abby_akku)
PHP
Javascript
输出:
10.3923