给定代表循环四边形边长的四个正整数A , B , C和D ,任务是找到循环四边形的面积。
例子:
Input: A = 10, B = 15, C = 20, D = 25
Output: 273.861
Input: A = 10, B = 30, C = 50, D = 20
Output: 443.706
方法:可以根据以下观察结果解决给定问题:
- 循环四边形是所有顶点都位于一个圆上的四边形。该圆称为外接圆或外接圆,并且顶点被称为是环状的。
- 在上图中, r是外接圆的半径, A , B , C和D分别是边PQ , QR , RS和SP的长度。
- Bretschneider公式给出的四边形面积为:
where, A, B, C, and D are the sides of the triangle and
α and γ are the opposite angles of the quadrilateral.
Since, the sum of opposite angles of the quadrilateral is 180 degree. Therefore, the value of cos(180/2) = cos(90) = 0.
Therefore, the formula for finding the area reduces to \sqrt(s – A)*(s – B)*(s – C)*(s – D).
因此,想法是打印出作为给定四边形的合成面积。
下面是上述方法的实现:
C++
// C++ programm for the above approach
#include
using namespace std;
// Function to find the area
// of cyclic quadrilateral
float calculateArea(float A, float B,
float C, float D)
{
// Stores the value of
// half of the perimeter
float S = (A + B + C + D) / 2;
// Stores area of cyclic quadrilteral
float area = sqrt((S - A) * (S - B)
* (S - C) * (S - D));
// Return the resultant area
return area;
}
// Driver Code
int main()
{
float A = 10;
float B = 15;
float C = 20;
float D = 25;
cout << calculateArea(A, B, C, D);
return 0;
}
输出:
273.861
时间复杂度: O(1)
辅助空间: O(1)