📜  给定三点的多边形的最小面积

📅  最后修改于: 2021-05-06 07:55:26             🧑  作者: Mango

给定正多边形的三个点(n> 3),找到给定点可能的正多边形(所有边相同)的最小面积。
例子:

Input : 0.00 0.00
        1.00 1.00
        0.00 1.00
Output : 1.00
By taking point (1.00, 0.00) square is 
formed of side 1.0 so area = 1.00 .

在进行下一步之前要注意的一件事是,边数必须至少为4(注意n> 3)。
在这里,我们必须找到规则多边形可能的最小面积,因此要计算最小可能的面积,我们需要计算所需的n值。由于没有给出边长,因此我们首先计算由这些点形成的三角形外接半径。由公式给出
R = abc / 4A
其中a,b,c是所形成三角形的边,而A是三角形的面积。在此,三角形的面积可以通过Heron公式计算。
在计算出三角形的外接半径之后,我们可以通过以下公式计算多边形的面积
A = NX(SIN(360 / N)XR2分之2)
在此,r表示n边(n边的规则多边形)的外接半径。
但是,首先我们必须计算n的值。要计算n,我们首先必须通过余弦公式计算所有三角形的角度
cosA =(b 2 + c 2 -a 2 )/ 2bc
cosB =(a 2 + c 2 -b 2 )/ 2ac
cosC =(a 2 + b 2 -c 2 )/ 2ab
然后,n由
n = pi / GCD(A,B,C)
其中A,B和C是三角形的角度。在计算n之后,我们将此值代入计算多边形面积的公式。
下面是给定方法的实现:

C++
// CPP program to find minimum area of polygon of
// number of sides more than three with given three points.
#include 
using namespace std;
 
// assigning pi value to variable
const double pi = 3.14159265359;
 
// calculating gcd value of two double values .
double gcd(double x, double y)
{
    return fabs(y) < 1e-4 ? x : gcd(y, fmod(x, y));
}
 
// Calculating minimum area of polygon through this function .
double min_area_of_polygon(double Ax, double Ay, double Bx,
                            double By, double Cx, double Cy)
{
    double a, b, c, Radius, Angle_A, Angle_B, Angle_C,
                              semiperimeter, n, area;
 
    // calculating the length of the sides of the triangle
    // formed from given points a, b, c represents the
    // length of different sides of triangle .
    a = sqrt((Bx - Cx) * (Bx - Cx) + (By - Cy) * (By - Cy));
    b = sqrt((Ax - Cx) * (Ax - Cx) + (Ay - Cy) * (Ay - Cy));
    c = sqrt((Ax - Bx) * (Ax - Bx) + (Ay - By) * (Ay - By));
 
    // here we have calculated the semiperimeter of a triangle .
    semiperimeter = (a + b + c) / 2;
 
    // Now from the semiperimeter area of triangle is derived
    // through the heron's formula .
    double area_triangle = sqrt(semiperimeter * (semiperimeter - a)
                                * (semiperimeter - b)
                                * (semiperimeter - c));
 
    // thus circumradius of the triangle is derived from the
    // sides and area of the triangle calculated .
    Radius = (a * b * c) / (4 * area_triangle);
 
    // Now each angle of the triangle is derived from the sides
    // of the triangle .
    Angle_A = acos((b * b + c * c - a * a) / (2 * b * c));
    Angle_B = acos((a * a + c * c - b * b) / (2 * a * c));
    Angle_C = acos((b * b + a * a - c * c) / (2 * b * a));
 
    // Now n is calculated such that area is minimum for
    // the regular n-gon .
    n = pi / gcd(gcd(Angle_A, Angle_B), Angle_C);
 
    // calculating area of regular n-gon through the circumradius
    // of the triangle .
    area = (n * Radius * Radius * sin((2 * pi) / n)) / 2;
 
    return area;
}
 
int main()
{
    // three points are given as input .
    double Ax, Ay, Bx, By, Cx, Cy;
    Ax = 0.00;
    Ay = 0.00;
    Bx = 1.00;
    By = 1.00;
    Cx = 0.00;
    Cy = 1.00;
 
    printf("%.2f", min_area_of_polygon(Ax, Ay, Bx, By, Cx, Cy));
    return 0;
}


Java
// Java program to find minimum
// area of polygon of number of
// sides more than three with
// given three points.
class GFG{
 
// Assigning pi value to variable
static double pi = 3.14159265359;
       
public static double fmod(double a,
                          double b)
{
  int result = (int) Math.floor(a / b);
  return a - result * b;
}
     
// calculating gcd value of
// two double values .
public static double gcd(double x,
                         double y)
{
  return Math.abs(y) < 1e-4 ? x :
         gcd(y, fmod(x, y));
}
       
// Calculating minimum area of polygon through this function .
public static double min_area_of_polygon(double Ax, double Ay,
                                         double Bx, double By,
                                         double Cx, double Cy)
{
  double a, b, c, Radius, Angle_A, Angle_B, Angle_C, 
         semiperimeter, n, area;
 
  // Calculating the length of the sides
  // of the triangle formed from given
  /// points a, b, c represents the 
  // length of different sides of triangle .
  a = Math.sqrt((Bx - Cx) * (Bx - Cx) +
                (By - Cy) * (By - Cy));
  b = Math.sqrt((Ax - Cx) * (Ax - Cx) +
                (Ay - Cy) * (Ay - Cy));
  c = Math.sqrt((Ax - Bx) * (Ax - Bx) +
                (Ay - By) * (Ay - By));
 
  // Here we have calculated the
  // semiperimeter of a triangle .
  semiperimeter = (a + b + c) / 2;
 
  // Now from the semiperimeter area
  // of triangle is derived
  // through the heron's formula .
  double area_triangle = Math.sqrt(semiperimeter *
                                  (semiperimeter - a) *
                                  (semiperimeter - b) *
                                  (semiperimeter - c));
 
  // Thus circumradius of the triangle
  // is derived from the sides and
  // area of the triangle calculated .
  Radius = (a * b * c) / (4 * area_triangle);
 
  // Now each angle of the triangle
  // is derived from the sides
  // of the triangle .
  Angle_A = Math.acos((b * b + c * c - a * a) /
                      (2 * b * c));
  Angle_B = Math.acos((a * a + c * c - b * b) /
                      (2 * a * c));
  Angle_C = Math.acos((b * b + a * a - c * c) /
                      (2 * b * a));
 
  // Now n is calculated such that
  // area is minimum for the regular n-gon .
  n = pi / gcd(gcd(Angle_A, Angle_B), Angle_C);
 
  // calculating area of regular n-gon
  // through the circumradius of the triangle .
  area = (n * Radius * Radius *
          Math.sin((2 * pi) / n)) / 2;
 
  return area;
}
   
// Driver code
public static void main(String[] args)
{
  // Three points are given as input .
  double Ax, Ay, Bx, By, Cx, Cy;
  Ax = 0.00;
  Ay = 0.00;
  Bx = 1.00;
  By = 1.00;
  Cx = 0.00;
  Cy = 1.00;
  System.out.println(String.format("%.2f",
                     min_area_of_polygon(Ax, Ay,
                                         Bx, By,
                                         Cx, Cy)));
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to find minimum area of
# polygon of number of sides more than three
# with given three points.
 
# from math lib import every function
from math import *
 
# assigning pi value to variable
pi = 3.14159265359
 
# calculating gcd value of two double values .
def gcd(x, y) :
 
    if abs(y) < 1e-4 :
        return x
    else :
        return gcd(y, fmod(x, y))
 
 
# Calculating minimum area of polygon
# through this function .
def min_area_of_polygon(Ax, Ay, Bx,
                        By, Cx, Cy) :
 
    # calculating the length of the sides of
    # the triangle formed from given points
    # a, b, c represents the length of different
    # sides of triangle
    a = sqrt((Bx - Cx) * (Bx - Cx) +
             (By - Cy) * (By - Cy))
    b = sqrt((Ax - Cx) * (Ax - Cx) +
             (Ay - Cy) * (Ay - Cy))
    c = sqrt((Ax - Bx) * (Ax - Bx) +
             (Ay - By) * (Ay - By))
 
    # here we have calculated the semiperimeter
    # of a triangle .
    semiperimeter = (a + b + c) / 2
 
    # Now from the semiperimeter area of triangle
    # is derived through the heron's formula
    area_triangle = sqrt(semiperimeter *
                        (semiperimeter - a) *
                        (semiperimeter - b) *
                        (semiperimeter - c))
 
    # thus circumradius of the triangle is derived
    # from the sides and area of the triangle calculated
    Radius = (a * b * c) / (4 * area_triangle)
 
    # Now each angle of the triangle is derived
    # from the sides of the triangle
    Angle_A = acos((b * b + c * c - a * a) / (2 * b * c))
    Angle_B = acos((a * a + c * c - b * b) / (2 * a * c))
    Angle_C = acos((b * b + a * a - c * c) / (2 * b * a))
 
    # Now n is calculated such that area is
    # minimum for the regular n-gon
    n = pi / gcd(gcd(Angle_A, Angle_B), Angle_C)
 
    # calculating area of regular n-gon through
    # the circumradius of the triangle
    area = (n * Radius * Radius *
            sin((2 * pi) / n)) / 2
 
    return area
 
# Driver Code
if __name__ == "__main__" :
 
    # three points are given as input .
    Ax = 0.00
    Ay = 0.00
    Bx = 1.00
    By = 1.00
    Cx = 0.00
    Cy = 1.00
 
    print(round(min_area_of_polygon(Ax, Ay, Bx,
                                    By, Cx, Cy), 1))
 
# This code is contributed by Ryuga


C#
// C# program to find minimum
// area of polygon of number of
// sides more than three with
// given three points. 
using System;
using System.Collections.Generic;
 
class GFG
{
     
    // Assigning pi value to variable
    static double pi = 3.14159265359;
            
    static double fmod(double a, double b)
    {
      int result = (int) Math.Floor(a / b);
      return a - result * b;
    }
          
    // calculating gcd value of
    // two double values .
    static double gcd(double x, double y)
    {
      return Math.Abs(y) < 1e-4 ? x : gcd(y, fmod(x, y));
    }
            
    // Calculating minimum area of polygon through this function .
    static double min_area_of_polygon(double Ax, double Ay,
                                             double Bx, double By,
                                             double Cx, double Cy)
    {
      double a, b, c, Radius, Angle_A, Angle_B, Angle_C, 
             semiperimeter, n, area;
      
      // Calculating the length of the sides
      // of the triangle formed from given
      /// points a, b, c represents the 
      // length of different sides of triangle .
      a = Math.Sqrt((Bx - Cx) * (Bx - Cx) +
                    (By - Cy) * (By - Cy));
      b = Math.Sqrt((Ax - Cx) * (Ax - Cx) +
                    (Ay - Cy) * (Ay - Cy));
      c = Math.Sqrt((Ax - Bx) * (Ax - Bx) +
                    (Ay - By) * (Ay - By));
      
      // Here we have calculated the
      // semiperimeter of a triangle .
      semiperimeter = (a + b + c) / 2;
      
      // Now from the semiperimeter area
      // of triangle is derived
      // through the heron's formula .
      double area_triangle = Math.Sqrt(semiperimeter *
                                      (semiperimeter - a) *
                                      (semiperimeter - b) *
                                      (semiperimeter - c));
      
      // Thus circumradius of the triangle
      // is derived from the sides and
      // area of the triangle calculated .
      Radius = (a * b * c) / (4 * area_triangle);
      
      // Now each angle of the triangle
      // is derived from the sides
      // of the triangle .
      Angle_A = Math.Acos((b * b + c * c - a * a) /
                          (2 * b * c));
      Angle_B = Math.Acos((a * a + c * c - b * b) /
                          (2 * a * c));
      Angle_C = Math.Acos((b * b + a * a - c * c) /
                          (2 * b * a));
      
      // Now n is calculated such that
      // area is minimum for the regular n-gon .
      n = pi / gcd(gcd(Angle_A, Angle_B), Angle_C);
      
      // calculating area of regular n-gon
      // through the circumradius of the triangle .
      area = (n * Radius * Radius *
              Math.Sin((2 * pi) / n)) / 2;
      
      return area;
    }  
   
  // Driver code
  static void Main()
  {
      // Three points are given as input .
      double Ax, Ay, Bx, By, Cx, Cy;
      Ax = 0.00;
      Ay = 0.00;
      Bx = 1.00;
      By = 1.00;
      Cx = 0.00;
      Cy = 1.00;
      Console.WriteLine(String.Format("{0:0.00}", min_area_of_polygon(Ax, Ay,
                                             Bx, By,
                                             Cx, Cy)));
  }
}
 
// This code is contributed by divyeshrabadiya07


输出:

1.00