-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgraham_scan.cpp
More file actions
149 lines (106 loc) · 2.21 KB
/
graham_scan.cpp
File metadata and controls
149 lines (106 loc) · 2.21 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
//Convex hull(볼록 껍질) 연습
struct pos {
long long x, y;
};
pos v[100010];
int N; //얼마나 많은 점일 것인가
int M; //얼마나 넓은 좌표체계
stack<pos> s;/// 점들의 좌표가 쌓이는 container
vector<pos> s1; // 오리지널 데이터
vector<pos> s2; // 남겨진 데이터
vector<vector<int>> data0;
void print1()
{
cout << endl;
for (int x = 0; x < M; x++)
{
for (int y = 0; y < M; y++)
{
cout << data0[x][y] << " ";
}
cout << endl;
}
cout << endl;
}
bool comp_y(pos a, pos b) {
if (a.y != b.y) return a.y < b.y;
else return a.x < b.x;
}
long long ccw(pos a, pos b, pos c) { //ccw
return a.x * b.y + b.x * c.y + c.x * a.y - (b.x * a.y + c.x * b.y + a.x * c.y);
}
bool comp_c(pos a, pos b) { //반시계 정렬
long long cc = ccw(v[0], a, b);
if (cc != 0) return cc > 0; // 각도 작은 순
else return (a.x + a.y) < (b.x + b.y); //일직선 있을경우 좌표가 작은
}
void algorithm()
{
ios_base::sync_with_stdio(false); cin.tie(NULL);
//main 함수 위의 입력예시 부분 참조
cout << "M값 입력 (0,0) ~ (M,M) " << endl;
cin >> M;
cout << "좌표영역은 0,0 에서 " << M << "," << M << "까지" << endl;
cout << endl;
cout << "N값 입력" << endl;
cin >> N;
cout << "N개의 점의 x,y 값 입력" << endl;
for (int i = 0; i < N; i++) {
cin >> v[i].x >> v[i].y;
s1.push_back(v[i]);
}
//---------------------------
sort(v, v + N, comp_y);
sort(v + 1, v + N, comp_c);
s.push(v[0]);
s.push(v[1]);
pos first, second;
for (int i = 2; i < N; i++) {
while (s.size() >= 2) {
second = s.top();
s.pop();
first = s.top();
if (ccw(first, second, v[i]) > 0) { //좌회전
s.push(second);
break;
}
}
s.push(v[i]);
}
//여기 까지가 점 정하기
data0.assign(M, vector<int>(M, 0));
while(s.size() > 0){
s2.push_back(s.top());
s.pop();
}
for (int x = 0; x < s2.size(); x++)
{
data0[s2[x].x][s2[x].y] = 1;
}
print1();
//cout << s2.size();
//making map
// cout << endl;
//cout << s.size();
}
/* 입력예시
5
8
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
*/
int main(void) {
algorithm();
return 0;
}