-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline2pixel.h
196 lines (180 loc) · 4.27 KB
/
line2pixel.h
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <vector>
#include <fstream>
#include <cmath>
#include <iostream>
using namespace std;
#define POLYGON_SIZE 5
void line2pixel(long int inpoint[2][2], vector<vector<long int> >& outpoint);
//needs to read data from sample_1tilesskipbroken.txt
//long int round(double r)
//{
// return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5);
//
//}
void polygonRaster(vector<vector<long int>> inpoint, vector<vector<long int> >& outpoint){
for(int i = 0; i < inpoint.size() - 1; i++){
long int two_end[2][2];
two_end[0][0] = inpoint[i][0];
two_end[0][1] = inpoint[i][1];
two_end[1][0] = inpoint[i+1][0];
two_end[1][1] = inpoint[i+1][1];
line2pixel(two_end, outpoint);
}
}
void line2pixel(long int inpoint[2][2], vector<vector<long int> >& outpoint){
long int xlo = inpoint[0][0];
long int ylo = inpoint[0][1];
long int xhi = inpoint[1][0];
long int yhi = inpoint[1][1];
//cout << xlo << " " << ylo << " " << xhi << " " << yhi << endl;
vector<long int> single_point;
//parallel to y direction, y is changing
if(xhi == xlo){
if(yhi < ylo){
long int temp = ylo;
while(yhi < temp){
single_point.push_back(xlo);
single_point.push_back(temp);
outpoint.push_back(single_point);
single_point.clear();
temp--;
}
}
else {
long int temp = ylo;
while(yhi > temp){
single_point.push_back(xlo);
single_point.push_back(temp);
outpoint.push_back(single_point);
single_point.clear();
temp++;
}
}
return;
}
//parallel to x direction, x is changing
if(yhi == ylo){
if(xhi < xlo){
long int temp = xlo;
while(xhi < temp){
single_point.push_back(temp);
single_point.push_back(ylo);
outpoint.push_back(single_point);
single_point.clear();
temp--;
}
}
else {
long int temp = xlo;
while(xhi > temp){
single_point.push_back(temp);
single_point.push_back(ylo);
outpoint.push_back(single_point);
single_point.clear();
temp++;
}
}
return;
}
double a = (double)(yhi - ylo) / (double)(xhi - xlo);
if(a > 0) {
long int x = xlo;
double y = ylo;
long int prevx = xlo;
long int prevy = ylo;
if(xhi > xlo) {
for(; x <= xhi; x++){
long int tempy = round(y);
if(x > prevx && tempy > prevy) {
//extra point needs to be added in between
while(tempy > prevy) {
single_point.push_back(x);
single_point.push_back(prevy);
outpoint.push_back(single_point);
single_point.clear();
prevy++;
}
}
single_point.push_back(x);
single_point.push_back(tempy);
outpoint.push_back(single_point);
single_point.clear();
prevx = x;
prevy = tempy;
y = y + a;
}
}
else {
for(; x >= xhi; x--){
long int tempy = round(y);
if(x < prevx && tempy < prevy) {
//extra point needs to be added in between
while(tempy < prevy){
single_point.push_back(x);
single_point.push_back(prevy);
outpoint.push_back(single_point);
single_point.clear();
prevy--;
}
}
single_point.push_back(x);
single_point.push_back(tempy);
outpoint.push_back(single_point);
single_point.clear();
prevx = x;
prevy = tempy;
y = y - a;
}
}
}
else {
long int x = xlo;
double y = ylo;
long int prevx = xlo;
long int prevy = ylo;
if(xhi > xlo) {
for(; x <= xhi; x++){
long int tempy = round(y);
if(x > prevx && tempy < prevy) {
//extra point needs to be added in between
while(tempy < prevy){
single_point.push_back(x);
single_point.push_back(prevy);
outpoint.push_back(single_point);
single_point.clear();
prevy--;
}
}
single_point.push_back(x);
single_point.push_back(tempy);
outpoint.push_back(single_point);
single_point.clear();
prevx = x;
prevy = tempy;
y = y + a;
}
}
else {
for(; x >= xhi; x--){
long int tempy = round(y);
if(x < prevx && tempy > prevy) {
//extra point needs to be added in between
while(tempy > prevy){
single_point.push_back(x);
single_point.push_back(prevy);
outpoint.push_back(single_point);
single_point.clear();
prevy++;
}
}
single_point.push_back(x);
single_point.push_back(tempy);
outpoint.push_back(single_point);
single_point.clear();
prevx = x;
prevy = tempy;
y = y - a;
}
}
}
}