)
如果您喜欢此文章请收藏、点赞、评论谢谢祝您快乐每一天。已知正多边形n 3的三个顶点求由这三个顶点构成的正多边形四边相等的最小面积。例如输入0.00 0.001.00 1.000.00 1.00输出1.00通过取点 (1.00, 0.00) 形成边长为 1.0 的正方形因此面积 1.00。在继续之前需要注意一点边数必须至少为 4注意 n 3 的条件。这里我们需要找到正多边形的最小面积因此为了计算最小面积我们需要计算所需的 n 值。由于边长未知我们首先计算由这些点构成的三角形的外接圆半径。外接圆半径的计算公式为R abc / 4A 其中 a、b、c 是所构成三角形的三边长A 是三角形的面积。三角形的面积可以使用海伦公式计算。计算出三角形的外接圆半径后我们使用公式 A n × (sin(360/n) × r² /2)计算正多边形的面积。这里r 代表 n 边形n 边正多边形的外接圆半径。但是首先我们需要计算 n 的值。为了计算 n我们首先需要利用余弦公式 计算三角形的三个角cosA (b² c² - a² ) / 2bccosB (a² c² - b² ) / 2accosC (a² b² - c² ) / 2ab。然后n 的值由公式 n π / GCD(A, B, C)给出 其中 A、B 和 C 是三角形的三个角。计算出 n 后将其代入多边形面积的计算公式。以下是该方法的实现// 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 variablestatic 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 herons 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 codestatic 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时间复杂度 O(log(min(A,B,C)))辅助空间 O(1)因为没有占用额外的空间。如果您喜欢此文章请收藏、点赞、评论谢谢祝您快乐每一天。