Game Develop

[Algorithm]Baekjoon 11758번 :: CCW 본문

Algorithm/Baekjoon

[Algorithm]Baekjoon 11758번 :: CCW

MaxLevel 2025. 3. 21. 18:50

https://www.acmicpc.net/problem/11758

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>
#include <functional>
#include <sstream>
#include <memory.h>
#include <deque>
#include <set>
#include <unordered_map>
#include <stack>
#include <numeric>
#include <climits>
#include <bitset>
#include <cmath>
#include <mutex>
 
using namespace std;
 
struct Vector
{
    int x = 0;
    int y = 0;
    int z = 0;
};
 
Vector Cross(const Vector& v1, const Vector& v2)
{
    Vector result =
    {
        v1.y * v2.z - v1.z * v2.y,
        v1.z * v2.x - v1.x * v2.z,
        v1.x * v2.y - v1.y * v2.x
    };
 
    return result;
}
 
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 
    int x1, y1, x2, y2, x3, y3;
 
    cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
  
    Vector v1 = { x2 - x1, y2 - y1, 0 };
    Vector v2 = { x3 - x2, y3 - y2, 0 };
 
    int dir = Cross(v1, v2).z;
 
    if (dir < 0)
    {
        cout << -1;
    }
    else if (dir == 0)
    {
        cout << 0;
    }
    else
    {
        cout << 1;
    }
 
    return 0;
}
 
 
 
 
 
cs

 

세 점이 주어지고 각 점을 순서대로 이었을 때, 시계방향인지, 반시계방향인지(CCW인지), 그냥 일직선인지를 판별해야하는 문제이다.

 

외적을 통해 쉽게 구할 수 있기 때문에, 외적을 구하는 공식을 알고있어야 풀 수 있다. 

문제에서는 2차원좌표라서 위의 코드처럼 할 필요는 없다만, 그냥 다시 숙지할 겸 3차원이라 가정하고 외적코드를 작성했다.