📌  相关文章
📜  椭圆中可内接的最大正方形的面积

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

给定一个长轴长度为2a2b的椭圆,任务是找到可以内接的最大矩形的面积。
例子:

Input: a = 4, b = 2
Output: 1.25

Input: a = 5, b= 3
Output: 0.604444

方法:如果正方形内接于椭圆,则正方形中心到其任意角的距离将等于原点到下图中右上角点之间的距离,其中x=y

下面是上述方法的实现:

C++
// C++ Program to find the biggest square
// which can be inscribed within the ellipse
#include 
using namespace std;
 
// Function to find the area
// of the square
float squarearea(float a, float b)
{
 
    // a and b cannot be negative
    if (a < 0 || b < 0)
        return -1;
 
    // area of the square
    float area = 4 * ((pow(a, 2) + pow(b, 2))
                      / (pow(a, 2) * pow(b, 2)));
 
    return area;
}
 
// Driver code
int main()
{
    float a = 4, b = 2;
    cout << squarearea(a, b) << endl;
 
    return 0;
}


Java
// Java Program to find the biggest square
// which can be inscribed within the ellipse
import java.io.*;
 
class GFG {
 
 
// Function to find the area
// of the square
static float squarearea(float a, float b)
{
 
    // a and b cannot be negative
    if (a < 0 || b < 0)
        return -1;
 
    // area of the square
    float area = 4 *(float) ((Math.pow(a, 2) + Math.pow(b, 2))
                    / (Math.pow(a, 2) * Math.pow(b, 2)));
 
    return area;
}
 
// Driver code
 
    public static void main (String[] args) {
        float a = 4, b = 2;
    System.out.println( squarearea(a, b));
 
    }
}
// This code is contributed by inder_verma.


Python 3
# Python3 Program to find the biggest square
# which can be inscribed within the ellipse
 
 
# Function to find the area
# of the square
def squarearea( a, b):
 
 
    # a and b cannot be negative
    if (a < 0 or b < 0):
        return -1
     
 
    # area of the square
    area = 4 * (((pow(a, 2) + pow(b, 2)) /
               (pow(a, 2) * pow(b, 2))))
 
    return area
 
 
# Driver code
if __name__=='__main__':
    a = 4
    b = 2
    print(squarearea(a, b))
 
# This code is contributed by ash264


C#
// C# Program to find the biggest
// square which can be inscribed
// within the ellipse
using System;
 
class GFG
{
 
// Function to find the area
// of the square
static float squarearea(float a, float b)
{
 
    // a and b cannot be negative
    if (a < 0 || b < 0)
        return -1;
 
    // area of the square
    float area = 4 *(float) ((Math.Pow(a, 2) +
                              Math.Pow(b, 2)) /
                             (Math.Pow(a, 2) *
                              Math.Pow(b, 2)));
 
    return area;
}
 
// Driver code
public static void Main ()
{
    float a = 4, b = 2;
    Console.WriteLine( squarearea(a, b));
}
}
 
// This code is contributed by inder_verma


PHP


Javascript


输出:
1.25

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