본문 바로가기

PS/알고리즘

angular sort

auto checkQuadrant = [](const Point &p) -> bool {
	return p.y < 0 || (p.y == 0 && p.x < 0); // PI <= atan2(p) < 2 * PI
};

sort(planes.begin(), planes.end(), [&](const Point &a, const Point &b) {
	bool aq = checkQuadrant(a), bq = checkQuadrant(b);
	if constexpr (isCCW) return aq != bq ? aq < bq : crossProduct(a, b) > 0;
	else return aq != bq ? aq < bq : crossProduct(a, b) < 0;
});

실수 오차 때문에 atan2를 사용하기 꺼려지는 경우 ccw로 구현할 수 있습니다.

$0 \le \theta \lt \pi$와 $\pi \le \theta \lt 2 \pi$인 경우를 분리해서 생각하면 사이각이 180도 미만일 때만 ccw를 호출하므로 순서가 꼬이지 않습니다.

'PS > 알고리즘' 카테고리의 다른 글

세그트리 메모리 최적화  (0) 2026.02.20
bitset unbounded knapsack  (0) 2026.02.07
밀러라빈  (0) 2024.03.24
Deterministic Verification of Integer Matrix Multiplication in Quadratic Time  (1) 2024.02.27
간단한 exgcd  (0) 2024.02.18