-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoord.cpp
More file actions
314 lines (282 loc) · 6.48 KB
/
Copy pathcoord.cpp
File metadata and controls
314 lines (282 loc) · 6.48 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "coord.h"
//Constructor
/*!
* \brief Coord::Coord
*
* Empty constructor of a Coord.
*/
Coord::Coord()
{
if (DEBUG > 100) cout << "Coord::Coord()"<< endl;
n = 0;
x = NULL;
y = NULL;
}
/*!
* \brief Coord::Coord
* \param n size of Coord
* \param x x vector (size n)
* \param y y vector (size n)
*
* Full constructor of a Coord.
*/
Coord::Coord(int n, double *x, double *y)
{
if (DEBUG > 100) cout << "Coord::Coord(int n, double *x, double *y)" << endl;
this->n = n;
if (n > 0){
this->x = new double[n];
this->y = new double[n];
for (int i = 0; i < n; i++){
this->x[i] = x[i];
this->y[i] = y[i];
}
}
}
/*!
* \brief Coord::set_size
* \param n size of Coord
*
* Partial constructor of a Coord. x and y are set to zero.
*/
void Coord::set_size(int n)
{
if (DEBUG > 100) cout << "void Coord::set_size(int n)" << endl;
this->n = n;
if (x != NULL){
delete[] x;
x = NULL;
}
if (y != NULL){
delete[] y;
y = NULL;
}
if (n > 0){
x = new double[n];
y = new double[n];
for (int i = 0; i < n; i++){
x[i] = 0.;
y[i] = 0.;
}
}
}
/*!
* \brief Coord::set_coord
* \param val value
* \param i position
* \param type XCOORD or YCOORD
*
* Set coordinate in position i of vector type of a Coord.
*/
void Coord::set_coord(double val, int i, char type)
{
if (DEBUG > 100) cout << "void Coord::set_coord(double val, int i, char type)" << endl;
if (i >= n){
cout << "Coord::set_coord " << COORD_ERROR_001 << endl;
cout << "n = " << n << ", i = " << i << endl;
}
if (type == XCOORD){
x[i] = val;
}
else if (type == YCOORD){
y[i] = val;
}
else{
cout << "Coord::set_coord " << COORD_ERROR_002 << endl;
cout << "type = " << type << endl;
}
}
/*!
* \brief Coord::set_coords
* \param x vector x (at least size n)
* \param y vector y (at least size n)
*
* Set coordinates of a Coord.
*/
void Coord::set_coords(double *x, double *y)
{
if (DEBUG > 100) cout << "void Coord::set_coords(double *x, double *y)" << endl;
if (n <= 0){
cout << "Coord::set_coords " << COORD_ERROR_003 << endl;
}
else{
if (this->x != NULL){
delete[] this->x;
this->x = NULL;
}
if (this->y != NULL){
delete[] this->y;
this->y = NULL;
}
this->x = new double[n];
this->y = new double[n];
for (int i = 0; i < n; i++){
this->x[i] = x[i];
this->y[i] = y[i];
}
}
}
/*!
* \brief Coord::size
* \return size of Coord
*
* Get the size of a Coord.
*/
int Coord::size()
{
if (DEBUG > 100) cout << "int Coord::size()" << endl;
return n;
}
/*!
* \brief Coord::coord
* \param i position
* \param type XCOORD or YCOORD
* \return coordinate i of vector type
*
* Get coordinate in position i of the vector type of a Coord.
*/
double Coord::coord(int i, char type)
{
if (DEBUG > 100) cout << "double Coord::coord(int i, char type)" << endl;
if (i >= n){
cout << "Coord::coord " << COORD_ERROR_001 << endl;
cout << "n = " << n << ", i = " << i << endl;
return 0.;
}
if (type == XCOORD){
return x[i];
}
else if (type == YCOORD){
return y[i];
}
else{
cout << "Coord::coord " << COORD_ERROR_002 << endl;
cout << "type = " << type << endl;
return 0.;
}
}
/*!
* \brief Coord::coords
* \param type XCOORD or YCOORD
* \return vector type
*
* Get vector type of a Coord.
*/
double *Coord::coords(char type)
{
if (DEBUG > 100) cout << "double *Coord::coords(char type)" << endl;
if (type == XCOORD){
double *out_x = new double[n];
for (int i = 0; i < n; i++){
out_x[i] = x[i];
}
return out_x;
}
else if (type == YCOORD){
double *out_y = new double[n];
for (int i = 0; i < n; i++){
out_y[i] = y[i];
}
return out_y;
}
else{
cout << "Coord::coords " << COORD_ERROR_002 << endl;
cout << "type = " << type << endl;
return NULL;
}
}
/*!
* \brief Coord::clear
*
* Clear a Coord.
*/
void Coord::clear()
{
if (DEBUG > 100) cout << "void Coord::clear()" << endl;
if (x != NULL){
delete[] x;
x = NULL;
}
if (y != NULL){
delete[] y;
y = NULL;
}
n = 0;
}
/*!
* \brief Coord::multiply
* \param type XCOORD or YCOORD
* \param a multiply parameter
*
* Multiply vector type by a of a Coord.
*/
void Coord::multiply(char type, double a)
{
if (DEBUG > 100) cout << "void Coord::multiply(char type, double a)" << endl;
if (n > 0){
if (type == XCOORD){
for (int i = 0; i < n; i++){
x[i] = x[i] * a;
}
}
else if (type == YCOORD){
for (int i = 0; i < n; i++){
y[i] = y[i] * a;
}
}
else{
cout << "Coord::multiply" << COORD_ERROR_002 << endl;
cout << "type = " << type << endl;
}
}
}
/*!
* \brief Coord::sort
* \param type
*/
void Coord::sort(char type)
{
if (DEBUG > 100) cout << "void Coord::sort(char type)" << endl;
if (type == XCOORD){
for (int i = 0; i < n; i++){
int ordered = 1;
for (int j = 1; j < n-i; j++){
if (x[j-1] < x[j]){
double x_tmp = x[j-1];
x[j-1] = x[j];
x[j] =x_tmp;
double y_tmp = y[j-1];
y[j-1] = y[j];
y[j] = y_tmp;
ordered = 0;
}
}
if (ordered == 1){
break;
}
}
}
else if (type == YCOORD){
for (int i = 0; i < n; i++){
int ordered = 1;
for (int j = 1; j < n-i; j++){
if (y[j-1] > y[j]){
double y_tmp = y[j-1];
y[j-1] = y[j];
y[j] = y_tmp;
double x_tmp = x[j-1];
x[j-1] = x[j];
x[j] =x_tmp;
ordered = 0;
}
}
if (ordered == 1){
break;
}
}
}
else{
cout << "Coord::sort" << COORD_ERROR_002 << endl;
cout << "type = " << type << endl;
}
}