관리 메뉴

fatalite

CCW - 백준 11758번 본문

코딩 인터뷰/C++

CCW - 백준 11758번

fataliteforu 2023. 7. 29. 21:57

Problem

골드 5 기하학 문제

잘 알려진 문제여서 외적을 사용하면 되는 것을 알고 있지만,

내적을 사용해도 되지 않을까? 라고 생각했다.

그러나 내적을 사용하면, cos 값이 90도 일때와 270(-90)도 일 때 모두 0 이기 때문에

CCW인지 CW인지 판별하기 어렵다는 것을 유추할 수 있다.

따라서 외적을 사용해야한다.

궁금하지만 일단 나중에 알아볼게 있는데

외적과 행렬식의 관계를 알아야할 필요가 있어보인다.

Solution

#include <iostream>
#include <vector>
#include <utility>
#include <cmath>
#include <queue>


using namespace std;

int Cross(pair<int, int> p1, pair<int, int> p2) {
    int ScalarOfOuterVector =
        p1.first * p2.second - p2.first * p1.second;
    if (ScalarOfOuterVector <  0) {
        return 1;
    }
    else if (ScalarOfOuterVector > 0) {
        return -1;
    }
    else {
        return 0;
    }
}

int main() {
    
    ios::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int FirstX, FirstY, SecondX, SecondY, ThirdX, ThirdY;
    cin >> FirstX >> FirstY >> SecondX >> SecondY >> ThirdX >> ThirdY;
    pair<int, int> FirstVector({ ThirdX - SecondX, ThirdY - SecondY });
    pair<int, int> SecondVector({ SecondX - FirstX, SecondY - FirstY });
    
    cout << Cross(FirstVector, SecondVector);

}

'코딩 인터뷰 > C++' 카테고리의 다른 글

치킨 배달 / 15686번 백준  (4) 2023.09.03
N-Queens / 9663번 백준 🌋🌋🌋🌋  (0) 2023.08.31
이분 그래프 - 백준 1707  (0) 2023.05.22
☆ 용액 - 백준 2467  (0) 2023.04.11
☆ 스티커 - 백준 9465  (1) 2023.04.09