-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgrid.cpp
More file actions
205 lines (171 loc) · 5.22 KB
/
Copy pathgrid.cpp
File metadata and controls
205 lines (171 loc) · 5.22 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
#include <iostream>
#include <png.h>
#include "grid.h"
#include "output.h"
vector_grid::vector_grid(int size) : N(size), count(0),
grid(size, std::vector<int>(size))
{}
bool vector_grid::should_stick(std::pair<int, int> p)
{
// Fix particle if needed
if(grid[p.second - 1][p.first]
|| grid[p.second + 1][p.first]
|| grid[p.second][p.first - 1]
|| grid[p.second][p.first + 1]
|| grid[p.second - 1][p.first - 1]
|| grid[p.second + 1][p.first + 1]
|| grid[p.second + 1][p.first - 1]
|| grid[p.second - 1][p.first + 1])
{
count++;
grid[p.second][p.first] = count;
return true;
}
return false;
}
void vector_grid::to_png(const std::string &filename)
{
FILE* file;
png_structp write_struct;
png_infop info_struct;
std::vector<unsigned char> data;
std::string fullname = filename;
fullname += ".png";
const int N = grid.size();
file = fopen(fullname.c_str(), "wb");
if(!file)
goto finish;
write_struct = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL,
NULL);
if(!write_struct)
goto finish;
info_struct = png_create_info_struct(write_struct);
if(!info_struct)
goto finish;
if(setjmp(png_jmpbuf(write_struct)))
goto finish;
png_init_io(write_struct, file);
// We write in 8 byte RGB format
png_set_IHDR(write_struct, info_struct, N, N, 8,
PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(write_struct, info_struct);
for(int i = 0; i < N; i++)
{
data.clear();
for(int j = 0; j < N; j++)
{
rgb val = get_colour(grid[i][j]);
data.push_back(val.r);
data.push_back(val.g);
data.push_back(val.b);
}
png_write_row(write_struct, data.data());
}
png_write_end(write_struct, NULL);
finish:
if(file)
fclose(file);
if(info_struct)
png_free_data(write_struct, info_struct, PNG_FREE_ALL, -1);
if(write_struct)
png_destroy_write_struct(&write_struct, NULL);
}
point_vector_grid::point_vector_grid(int size) : vector_grid(size), kR(100),
sR(25), angleDist(0, 6.28),
maxR(1)
{
// Set the central point
grid[N/2][N/2] = 1;
}
std::pair<int, int> point_vector_grid::get_start()
{
double angle = angleDist(engine);
return std::make_pair(static_cast<int>(sR * std::cos(angle) + N/2),
static_cast<int>(sR * std::sin(angle) + N/2));
}
bool point_vector_grid::should_kill(std::pair<int, int> p)
{
int x = N/2 - p.first;
int y = N/2 - p.second;
int radius = static_cast<int>(std::sqrt(x * x + y * y));
// Kill the particle if it strays too near the edge
return radius >= kR;
}
bool point_vector_grid::should_stick(std::pair<int, int> p)
{
int x = N/2 - p.first;
int y = N/2 - p.second;
int radius = static_cast<int>(std::sqrt(x * x + y * y));
// Only check if we are near the fractal
if(radius >= maxR + 5)
return false;
if(vector_grid::should_stick(p))
{
maxR = std::max(maxR, radius);
// Extend the kill radius if needed
if(kR <= sR + 100)
{
kR = std::min(N/2 - 1, kR + 100);
if(kR != N/2 -1)
std::cout << "Expanding kR: " << kR << std::endl;
}
// Extend the starting radius if needed
if(maxR >= sR - 50)
{
if(sR != kR)
{
sR = std::min(kR, sR + 100);
std::cout << "Expanding sR: " << sR << std::endl;
}
}
return true;
}
return false;
}
line_vector_grid::line_vector_grid(int size) : vector_grid(size), kR(100),
sR(25), dist(0, size),
maxR(1)
{
// Set the central line
for(int i = 0; i < N; i++)
grid[i][N/2] = 1;
}
std::pair<int, int> line_vector_grid::get_start()
{
int start = dist(engine);
return std::make_pair(N/2 + (pow(-1, count) * sR), start);
}
bool line_vector_grid::should_kill(std::pair<int, int> p)
{
return p.first <= 1 || p.first >= N - 1 || p.second <= 1 || p.second >= N - 1;
}
bool line_vector_grid::should_stick(std::pair<int, int> p)
{
int radius = std::abs(N/2 - p.second);
// Only check if we are near the fractal
if(radius >= maxR + 5)
return false;
if(vector_grid::should_stick(p))
{
maxR = std::max(maxR, radius);
// Extend the kill radius if needed
if(kR <= sR + 100)
{
kR = std::min(N/2 - 1, kR + 100);
if(kR != N/2 -1)
std::cout << "Expanding kR: " << kR << std::endl;
}
// Extend the starting radius if needed
if(maxR >= sR - 50)
{
if(sR != kR)
{
sR = std::min(kR, sR + 100);
std::cout << "Expanding sR: " << sR << std::endl;
}
}
return true;
}
return false;
}