📜  求内接在 N 边正多边形中的三角形面积的程序

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

给定一个三角形内接在一个给定边长的 N 边正多边形中,由多边形的任意 3 个顶点形成,任务是找到这个三角形的面积。
例子:

Input: N = 6, side = 10
Output: 129.904

Input: N = 8, side = 5
Output: 45.2665

方法:考虑第一个例子:

  • 给定的是一个内接三角形 AEC 的 6 边正多边形 ABCDEF。
  • 可以看出,三角形将给定的多边形划分为 6 个相等的三角形区域,其中三角形 AEC 的交点是三角形的质心。

  • 求正多边形的面积。正多边形的面积可以借助公式(A*P)/2 计算,其中 P 是该多边形的周长,A 是该多边形的上心点。
  • 根据对称定律,每个三角部分的面积将是 (TriangulatedArea = Area of N 边正多边形 / N)。
  • 由于三角形 ACE 由 6 个中的 3 个组成,所以三角形 ACE 的面积将为 (3 * TriangulatedArea)
  • 因此,一般情况下,如果有一个面积为 A 的 N 边正多边形,则内接三角形的面积为(A/N)*3

下面是上述方法的实现:

C++
// C++ Program to find the area of a triangle
// inscribed in N-sided regular polygon
 
#include 
#include 
using namespace std;
 
// Function to find the area of the polygon
double area_of_regular_polygon(double n, double len)
{
 
    // area of a regular polygon with N sides
    // and side length len
    double P = (len * n);
    double A
        = len
          / (2 * tan((180 / n)
                     * 3.14159 / 180));
    double area = (P * A) / 2;
 
    return area;
}
 
// Function to find the area of a triangle
double area_of_triangle_inscribed(double n, double len)
{
 
    double area = area_of_regular_polygon(n, len);
 
    // area of one triangle
    // in an N-sided regular polygon
    double triangle = area / n;
 
    // area of inscribed triangle
    double ins_tri = (triangle * 3);
 
    return ins_tri;
}
 
// Driver code
int main()
{
    double n = 6, len = 10;
 
    cout << area_of_triangle_inscribed(n, len)
         << endl;
 
    return 0;
}


Java
// Java Program to find the area of a triangle
// inscribed in N-sided regular polygon
import java.util.*;
 
class GFG
{
 
// Function to find the area of the polygon
static double area_of_regular_polygon(double n,
                                      double len)
{
 
    // area of a regular polygon with N sides
    // and side length len
    double P = (len * n);
    double A = len / (2 * Math.tan((180 / n) *
                             3.14159 / 180));
    double area = (P * A) / 2;
 
    return area;
}
 
// Function to find the area of a triangle
static double area_of_triangle_inscribed(double n,
                                         double len)
{
    double area = area_of_regular_polygon(n, len);
 
    // area of one triangle
    // in an N-sided regular polygon
    double triangle = area / n;
 
    // area of inscribed triangle
    double ins_tri = (triangle * 3);
 
    return ins_tri;
}
 
// Driver code
static public void main(String[] arg)
{
    double n = 6, len = 10;
 
    System.out.printf("%.3f",
           area_of_triangle_inscribed(n, len));
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 Program to find the area
# of a triangle inscribed in
# N-sided regular polygon
import math
 
# Function to find the area of the polygon
def area_of_regular_polygon(n, len):
 
    # area of a regular polygon with
    # N sides and side length len
    P = (len * n);
    A = len / (2 * math.tan((180 / n) *
                      3.14159 / 180))
    area = (P * A) / 2
 
    return area
 
# Function to find the area of a triangle
def area_of_triangle_inscribed(n, len):
 
    area = area_of_regular_polygon(n, len)
 
    # area of one triangle
    # in an N-sided regular polygon
    triangle = area / n
 
    # area of inscribed triangle
    ins_tri = (triangle * 3);
 
    return ins_tri
 
# Driver code
n = 6
len = 10
print(round(area_of_triangle_inscribed(n, len), 3))
 
# This code is contributed by divyamohan


C#
// C# Program to find the area of a triangle
// inscribed in N-sided regular polygon
using System;
                     
class GFG
{
 
// Function to find the area of the polygon
static double area_of_regular_polygon(double n,
                                      double len)
{
 
    // area of a regular polygon with N sides
    // and side length len
    double P = (len * n);
    double A = len / (2 * Math.Tan((180 / n) *
                             3.14159 / 180));
    double area = (P * A) / 2;
 
    return area;
}
 
// Function to find the area of a triangle
static double area_of_triangle_inscribed(double n,
                                         double len)
{
    double area = area_of_regular_polygon(n, len);
 
    // area of one triangle
    // in an N-sided regular polygon
    double triangle = area / n;
 
    // area of inscribed triangle
    double ins_tri = (triangle * 3);
 
    return ins_tri;
}
 
// Driver code
static public void Main(String[] arg)
{
    double n = 6, len = 10;
 
    Console.Write("{0:F3}",
            area_of_triangle_inscribed(n, len));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
129.904

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