프로그래밍 언어/C++

[C++] 원이 겹치는지에 대한 개인 코드

ShovelingLife 2024. 5. 29. 11:56
struct Circle
{
	float x, y, z;
	float mp = 0; // middle-point
	float l = 0;

public:
	Circle() = default;

	Circle(float x, float y, float z = 0) :x(x), y(y), z(z) { }

	bool operator==(Circle& ref) { return this->x == ref.x && this->y == ref.y && this->z == ref.z; }
};

int main()
{
	// 주어진 원
	vector<Circle> v({ {-3, 0}, {0, 5}, {2, 3} });
	int size = v.size();
	vector<vector<bool>> check(size, vector<bool>(size));
	int ans = 0;

	for (int i = 0; i < size; i++)
	{
		auto& circle = v[i];

		// 중점 구하기
		circle.mp = ((circle.x + circle.y)) / 2;

		// 지름 구하기
		float x = abs(circle.x), y = abs(circle.y);
		circle.l = x > y ? x - (abs(y)) : y - (abs(x));
	}
	// 완탐 브루트포스
	for (int i = 0; i < size; i++)
	{
		for (int j = 0; j < size; j++)
		{
			auto a = v[i], b = v[j];

			if (a == b)
				continue;

			// 중복 체크 방지
			if (check[i][j] ||
				check[j][i])
				continue;

			//cout << i << ' ' << j << endl;

			// 겹치는지 확인
			float dist = sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2)); // 두 점간 거리
			float sum = (a.l / 2) + (b.l / 2); // 두 반지름의 합
			//cout << dist << ' ' << sum << endl;
			
			if (dist < sum)
				ans++;

			check[i][j] = true;
		}
	}
	cout << ans;
}