-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalgorithm.cpp
More file actions
350 lines (282 loc) · 7.26 KB
/
Copy pathalgorithm.cpp
File metadata and controls
350 lines (282 loc) · 7.26 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <iostream>
#include <vector>
#include <math.h>
// For Time
#include <time.h>
#define BILLION 1000000000.0
//
using namespace std;
class Pair
{
public:
float x;
float y;
Pair(float a = 0, float b = 0)
{
x = a;
y = b;
}
void dis()
{
// cout << "(" << x << "," << y << ") ";
}
};
void display(vector<float> v)
{
for (auto i = v.begin(); i != v.end(); i++)
{
// cout << *i << " ";
}
// cout << endl;
}
// For Getting Count
extern "C"
float* seq1Count(float start, int length, float step)
{
vector<float> y;
vector<float> x;
int iterations = length*10;
int i = 0;
float count = start;
int loopCount = 0;
while(i < iterations)
{
y.push_back((round(count*1000.0) / 1000.0));
if (i > 0)
x.push_back((round(-count*1000.0) / 1000.0));
if (i != iterations)
count += step;
i += 1;
if (count > ((static_cast<float>(length)/2.0) + step))
break;
}
vector<float> w;
while (!x.empty())
{
w.push_back(x.back());
x.pop_back();
}
for (auto i = y.begin(); i != y.end(); i++)
{
w.push_back(*i);
}
int x_num = w.size();
float* retSeq1 = new float[x_num+1];
retSeq1[0] = x_num;
for (int i = 0; i < x_num; i++)
retSeq1[i+1] = w[i];
// cout << "Total no. of X-coordinates: " << x_num << endl;
return retSeq1;
}
extern "C"
float* seq2Count(float start, int width, float step)
{
vector<float> y;
vector<float> x;
int iterations = width*10;
int i = 0;
float count = start;
while(i < iterations)
{
y.push_back((round(count*1000.0) / 1000.0));
if (i > 0)
x.push_back((round(-count*1000.0) / 1000.0));
if (i != iterations)
count += step;
i += 1;
if (double(count) > (width/2.0)){
// cout << endl << count << endl << (width/2.0) << endl;
break;
}
}
vector<float> w;
// Reversing the y array and adding it to
// w
while (!y.empty())
{
w.push_back(y.back());
y.pop_back();
}
// extending w array with x array
for (auto i = x.begin(); i != x.end(); i++)
{
w.push_back(*i);
}
int y_num = w.size();
// cout << "Total no. of Y-coordinates: " << y_num << endl;
float* retSeq2 = new float[y_num+1];
retSeq2[0] = y_num;
for (int i = 0; i < y_num; i++)
retSeq2[i+1] = w[i];
// cout << "Total no. of X-coordinates: " << x_num << endl;
return retSeq2;
}
/////////////////
extern "C"
vector<float> seq1(float start, int length, float step)
{
vector<float> y;
vector<float> x;
int iterations = length*10;
int i = 0;
float count = start;
int loopCount = 0;
while(i < iterations)
{
y.push_back((round(count*1000.0) / 1000.0));
if (i > 0)
x.push_back((round(-count*1000.0) / 1000.0));
if (i != iterations)
count += step;
i += 1;
if (count > ((static_cast<float>(length)/2.0) + step))
break;
}
vector<float> w;
while (!x.empty())
{
w.push_back(x.back());
x.pop_back();
}
for (auto i = y.begin(); i != y.end(); i++)
{
w.push_back(*i);
}
int x_num = w.size();
// cout << "Total no. of X-coordinates: " << x_num << endl;
return w;
}
extern "C"
vector<float> seq2(float start, int width, float step)
{
vector<float> y;
vector<float> x;
int iterations = width*10;
int i = 0;
float count = start;
while(i < iterations)
{
y.push_back((round(count*1000.0) / 1000.0));
if (i > 0)
x.push_back((round(-count*1000.0) / 1000.0));
if (i != iterations)
count += step;
i += 1;
if (double(count) > (width/2.0)){
// cout << endl << count << endl << (width/2.0) << endl;
break;
}
}
vector<float> w;
// Reversing the y array and adding it to
// w
while (!y.empty())
{
w.push_back(y.back());
y.pop_back();
}
// extending w array with x array
for (auto i = x.begin(); i != x.end(); i++)
{
w.push_back(*i);
}
int y_num = w.size();
// cout << "Total no. of Y-coordinates: " << y_num << endl;
return w;
}
extern "C"
double* inscribe(float radius, int length, int width)
{
float height = round((radius*sqrt(3))*1000.0) / 1000.0; // upto 3 - decimal
vector<float> a = seq1(0.0 , length, height);
vector<float> b = seq2(0.0 , width, radius);
// getting the odd and even indexing value of index seperating b into even indexing and odd indexing of b
vector<float> odd_ind;
auto i = b.begin();
i++;
for (i; i != b.end(); i += 2)
{
odd_ind.push_back(*i);
}
vector<float> even_ind;
i = b.begin();
for (i; i != b.end(); i += 2)
{
even_ind.push_back(*i);
if ((i + 1) == b.end())
break;
}
// getting the odd and even columns value of index seperating a into even columns and odd columns of a
vector<float> odd_col;
vector<float> even_col;
for (int i = 0; i < a.size(); i++)
{
if (i%2 == 0)
even_col.push_back(a[i]);
else
odd_col.push_back(a[i]);
}
// for placing the even columns
vector < vector<Pair> > df1;
for (int i = 0; i < even_ind.size(); i++)
{
vector <Pair> temp;
for (int j = 0; j < even_col.size(); j++)
{
Pair c(even_col[j], even_ind[i]);
temp.push_back(c);
}
df1.push_back(temp);
}
// for placing the odd columns
for (int i = 0; i < odd_ind.size(); i++)
{
vector <Pair> temp;
for (int j = 0; j < odd_col.size(); j++)
{
Pair c(odd_col[j], odd_ind[i]);
temp.push_back(c);
}
df1.push_back(temp);
}
// Displaying and getting total count //
unsigned long long final_num = 0;
for (auto i = df1.begin(); i != df1.end(); i++)
{
for (auto j = (*i).begin(); j != (*i).end(); j++)
{
// (*j).dis();
final_num++;
}
// cout << endl;
}
// cout << "No. of circles that can be inscribed in " << length << "X" << width << " canvas: " << final_num << endl;
// cout << "Max Size: " << df1.max_size() << endl;
double *df1Double = new double[(2*final_num) + 1];
df1Double[0] = final_num;
int index = 1;
for (auto i = df1.begin(); i != df1.end(); i++)
{
for (auto j = (*i).begin(); j != (*i).end(); j++)
{
df1Double[index] = (*j).x;
index++;
df1Double[index] = (*j).y;
index++;
}
// cout << endl;
}
return df1Double;
///////////////
}
// int main()
// {
// struct timespec start, end;
// clock_gettime(CLOCK_REALTIME, &start);
// inscribe(0.1, 5000, 5000);
// clock_gettime(CLOCK_REALTIME, &end);
// double time_spent = (end.tv_sec - start.tv_sec) +
// (end.tv_nsec - start.tv_nsec) / BILLION;
// cout << "\n\nTotal Time for Execution(sec): " << time_spent << endl;
// return 0;
// }