#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
using myPoint=complex;
class MandelbrotSet {
public:
MandelbrotSet(int _count, double _ex_limit, int _wide, int _height, char *_path) {
WIDE = _wide * 4, HEIGHT = _height * 4;
ORIG_X = WIDE / 2;
ORIG_Y = HEIGHT / 2;
EXLIMIT = _ex_limit;
COUNT = _count;
PATH = _path;
}
double solve() {
double start = omp_get_wtime();
img = Mat(HEIGHT, WIDE, CV_8UC3);
vector > g;
for (int i = 0; i < HEIGHT; i++)
for (int j = 0; j < WIDE; j++)
g.emplace_back(i, j);
#pragma omp parallel for num_threads(4) schedule(dynamic)
for (int k = 0; k < g.size(); k++) {
int &i = g[k].first;
int &j = g[k].second;
if (Iteration(i, j)) {
img.ptr(i)[j * 3] = 0;
img.ptr(i)[j * 3 + 1] = (double) abs(j - ORIG_X) / WIDE * 255;
img.ptr(i)[j * 3 + 2] = (double) abs(i - ORIG_Y) / HEIGHT * 255;
} else {
img.ptr(i)[j * 3] = 0;
img.ptr(i)[j * 3 + 1] = 255 - (double) abs(j - ORIG_X) / WIDE * 255;
img.ptr(i)[j * 3 + 2] = 255 - (double) abs(i - ORIG_Y) / HEIGHT * 255;
}
}
double dur = omp_get_wtime() - start;
if (PATH == NULL) {
cvNamedWindow("分形");
setMouseCallback("分形", &MandelbrotSet::on_mouse, this);
imshow("分形", img);
for (int i = 0; i < 2; i++)
waitKey();
} else if (!imwrite(PATH, img))
puts("Fail to Save the Image!");
return dur;
}
private:
int WIDE, HEIGHT, COUNT, ORIG_X, ORIG_Y;
double EXLIMIT;
char *PATH;
Mat img;
bool Iteration(int y, int x) {
myPoint c = transAxis(this, x, y);
myPoint z = myPoint(0, 0);
for (int i = 0; i <= COUNT; i++) {
z = z * z + c;
if (abs(z) > EXLIMIT)
return true;
}
return false;
}
static inline myPoint transAxis(MandelbrotSet *self, int x, int y) {
return {(x - self->ORIG_X) / (self->WIDE / 4.0), (self->ORIG_Y - y) / (self->HEIGHT / 4.0)}; //[-2,2]*[-2,2]
}
static void on_mouse(int event, int x, int y, int flags, void *userdata) {
auto *self = (MandelbrotSet *) userdata;
if (event == CV_EVENT_MOUSEMOVE) {
Mat t_img = self->img.clone();
Point pt = Point(x, y);
char temp[16];
Vec3b col = Vec3b(255, 255, 255) - t_img.at(pt);
myPoint np = transAxis(self, x, y);
sprintf(temp, "(%.2lf,%.2lf)", real(np), imag(np));
putText(t_img, temp, Point(x, y), FONT_HERSHEY_DUPLEX, 0.3, col, 1);
imshow("分形", t_img);
}
}
};
int main(int argc, char *argv[]) {
int mp = 400, rs = 800, error_cnt = 0;
char *path = nullptr;
double er = 2.0;
for (int i = 1; i < argc;) {
if (strcmp(argv[i], "-mp") == 0) {
if (i + 1 < argc && sscanf(argv[i + 1], "%d", &mp) == 1) {
if (mp <= 0)
printf("Error %d:parameter of -mp should be positive!\n", ++error_cnt);
else if (mp > 1000)
printf("Error %d:parameter of -mp too big!\n", ++error_cnt);
i += 2;
} else {
printf("Error %d:-mp needs a parameter.\n", ++error_cnt);
++i;
}
} else if (strcmp(argv[i], "-er") == 0) {
if (i + 1 < argc && sscanf(argv[i + 1], "%lf", &er) == 1) {
if (er <= 0)
printf("Error %d:parameter of -er should be positive!\n", ++error_cnt);
else if (er > 10)
printf("Error %d:parameter of -er too big!\n", ++error_cnt);
i += 2;
} else {
printf("Error %d:-er needs a parameter.\n", ++error_cnt);
++i;
}
} else if (strcmp(argv[i], "-rs") == 0) {
if (i + 1 < argc && sscanf(argv[i + 1], "%d", &rs) == 1) {
if (rs <= 0)
printf("Error %d:parameter of -rs should be positive!\n", ++error_cnt);
else if (rs > 10000)
printf("Error %d:parameter of -rs too big!\n", ++error_cnt);
i += 2;
} else {
printf("Error %d:-rs needs a parameter.\n", ++error_cnt);
++i;
}
} else if (strcmp(argv[i], "-p") == 0) {
if (i + 1 < argc) {
path = argv[i + 1];
i += 2;
} else {
printf("Error %d:-er needs a parameter.\n", ++error_cnt);
++i;
}
} else {
printf("Error %d:Unrecognizable parameter name \"%s\"\n", ++error_cnt, argv[i]);
++i;
}
}
if (!error_cnt) {
MandelbrotSet M = MandelbrotSet(mp, er, rs, rs, path);
printf("Finished in %.10lf seconds\n", M.solve());
}
return 0;
}