https://www.acmicpc.net/problem/7869
7869번: 두 원
첫째 줄에 두 원의 중심과 반지름 x1, y1, r1, x2, y2, r2가 주어진다. 실수는 최대 소수점 둘째자리까지 주어진다.
www.acmicpc.net
고1 수학 삼각함수의 성질을 이용하면 풀 수 있는 문제이다.
cmath는 arcsin, arccos 등을 이용해 각도를 직접 구할 수 있으므로 참고하자.
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <string>
#include <iomanip>
#define PI 3.141592653
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
double x1, y1, r1, x2, y2, r2;
cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2;
cout << fixed << setprecision(3);
if (pow(x1 - x2, 2) + pow(y1 - y2, 2) >= pow(r1 + r2, 2))
{
cout << 0.0;
} //두 원이 겹치지 않을 때
else if (pow(x1 - x2, 2) + pow(y1 - y2, 2) <= pow(r1 - r2, 2))
{
if (r1 < r2) cout << r1 * r1 * PI;
else cout << r2 * r2 * PI;
} // 한 원이 다른 원을 내포할 때
else
{
double centerDist{ sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)) };
double deg1 //코사인 제 2법칙을 이용해 각도 구하기
= acos((pow(r1, 2) + pow(centerDist, 2)
- pow(r2, 2)) / (2 * centerDist * r1)) * 2;
double deg2
= acos((pow(r2, 2) + pow(centerDist, 2)
- pow(r1, 2)) / (2 * centerDist * r2)) * 2;
cout << pow(r1, 2) * (deg1 - sin(deg1)) / 2
+ pow(r2, 2) * (deg2 - sin(deg2)) / 2;
//부채꼴의 넓이에 삼각형의 넓이를 뺀다.
} // 두 원이 일부 겹칠 때
}
9328번: 열쇠 - 그래프, 구현 (0) | 2023.01.24 |
---|---|
1069번: 집으로 - 기하, 구현 (0) | 2023.01.24 |
2162번: 선분 그룹 - CCW, 유니온 파인드 (0) | 2023.01.23 |
20149번: 선분 교차 3 - CCW (0) | 2023.01.23 |
11758번: CCW - CCW (0) | 2023.01.23 |
댓글 영역