SZTUOJ 1013 - The Area of a Sector
Description
Given a circle and two points on it, calculate the area of the sector with its central angle no more than 180 degrees.
Input
There are multiple test cases.
Each line contains 6 float numbers denote the center of the circle xc, yc, the two points on the circle x1, y1 and x2, y2.
1 <= xc, yc, x1, y1, x2, y2 <= 10000.
Output
The area of the sector. The result should be accurated to three decimal places.
Sample Input
0 0 1 1 -1 1
2.5 2.5 3.25 3.5 3.5 3.25
Sample Output
1.571
0.222
Hint
With math.h, you could define pi (3.1415926...) as const double pi = acos(-1.0);
Inverse trigonometric function could be obtained by acos, atan, asin, etc..
Be careful that angles like pi in C/C++ are in 3.1415926... type rather than 180.
You’d better use double instead of float. Load double with "%lf" but output it with "%f".
Source
2021 SZTU AI Class Qualifying.
思路分析
这道题来自2021伦琴AI班选拔机试C题,当时只有包括我在内的两个人做了出来。但题目本身并不难,根据描述按步编写即可。
本题需要求解扇形面积。根据圆的面积公式,我们可以推导出半径为\(r\),圆心角为\(\theta\)的扇形面积公式:
\[S_\theta=\frac{\theta}{2}r^2\ (0<\theta\leq2\pi) \]根据上述公式,我们了解到,若想求解扇形面积,需要先求出扇形半径\(r\)和圆心角\(\theta\).
根据输入数据\(O(x_0,y_0),P_1(x_1,y_1),P_2(x_2,y_2)\),我们可以计算出半径和弦长的欧氏距离:
\[r=\sqrt{(x_0-x_1)^2+(y_0-y_1)^2}\\ l=\sqrt{(x_2-x_1)^2+(y_2-y_1)^2} \]作\(\triangle OP_1P_2\)的中垂线,根据几何性质,有:
\[\sin{\frac{1}{2}\angle O}=\sin{\frac{\theta}{2}}=\frac{l}{2r} \]即:
\[\theta=2\arcsin{\frac{l}{2r}} \]至此,求解面积所需的全部参量已经求出,带入扇形面积公式即可得解。
代码实现
// Language: C
#include<stdio.h>
#include<math.h>
typedef struct Point{
double x;
double y;
}point;
int main() {
point o, p1, p2;
double ox, oy, x1, y1, x2, y2;
while (scanf("%lf%lf%lf%lf%lf%lf", &o.x, &o.y, &p1.x, &p1.y, &p2.x, &p2.y) != EOF) {
double r = hypot(p1.x - o.x, p1.y - o.y);
double l = hypot(p1.x - p2.x, p1.y - p2.y);
double theta = 2 * asin(0.5 * l / r);
printf("%.3f\n", r * r * theta / 2);
}
return 0;
}