Created
April 23, 2015 15:03
-
-
Save TheAnonymous/af403f8b1a5f8ed3ae6c to your computer and use it in GitHub Desktop.
This is a small C++ programm to simulate growing fractals.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <opencv2/opencv.hpp> | |
#include <vector> | |
#include <random> | |
#include <iostream> | |
#include <cstdlib> | |
#include <iostream> | |
#include <ctime> | |
#include <unistd.h> | |
#include <thread> | |
#include "Mutex" | |
std::vector<int> initalNumericalSeries = std::vector<int>(512, 0); | |
std::vector<int> outputNumericalSeries = std::vector<int>(512); | |
std::mutex M; | |
int const wsize = 700; | |
int const startingPoints = 1000; | |
int const threadcount = 10; | |
cv::Mat img; | |
using namespace std; | |
int randomInt(int range){ | |
std::random_device rd; | |
std::mt19937 mt(rd()); | |
std::uniform_real_distribution<double > dist(1, range); | |
int random_variable = (int)dist(mt); | |
//cout << "rand value is " << random_variable << endl; | |
return random_variable; | |
} | |
bool checkColor(cv::Mat &img, int y, int x){ | |
if(y >= wsize){return false;} | |
if(x >= wsize){return false;} | |
for (int i = 0; i < 4; ++i) { | |
if (img.at<cv::Vec3b>(y , x)[i] > 0) { | |
return true; | |
} | |
} | |
return false; | |
} | |
void addPoint(cv::Mat &img, int color, int rgb){ | |
int x = randomInt(wsize); | |
int y = randomInt(wsize); | |
//img.at<cv::Vec3b>(x,y)[3] = 255; | |
while(true) | |
{ | |
if(x == wsize -1){x=wsize -2;} | |
if(y == wsize -1){y=wsize -2;} | |
if(x == 1){x=2;} | |
if(y == 1){y =2;} | |
if(x == 0) return; | |
if(y == 0) return; | |
if(checkColor(img,y,x)){ | |
return; | |
} | |
else if(checkColor(img,y+1,x)) | |
{ | |
M.lock(); | |
img.at<cv::Vec3b>(y,x)[rgb]= color; | |
M.unlock(); | |
return; | |
} | |
else if(checkColor(img,y-1,x)) | |
{ | |
M.lock(); | |
img.at<cv::Vec3b>(y,x)[rgb]= color; | |
M.unlock(); | |
return; | |
} | |
else if(checkColor(img,y,x+1)) | |
{ | |
M.lock(); | |
img.at<cv::Vec3b>(y,x)[rgb]= color; | |
M.unlock(); | |
return; | |
} | |
else if(checkColor(img,y,x-1)) | |
{ | |
M.lock(); | |
img.at<cv::Vec3b>(y,x)[rgb]= color; | |
M.unlock(); | |
return; | |
} | |
else{ | |
int direction = randomInt(5); | |
switch (direction){ | |
case 4: y++; | |
break; | |
case 1: y--; | |
break; | |
case 2: x++; | |
break; | |
case 3: x--; | |
break; | |
default: cerr << "Error1" << endl; | |
} | |
} | |
if(false){ | |
img.at<cv::Vec3b>(y,x)[3]= 255; | |
imshow("Aufgabe 4", img); | |
cv::waitKey(1); | |
img.at<cv::Vec3b>(y,x)[3]= 0; | |
} | |
} | |
} | |
void addPoints(){ | |
int color = 255; | |
int rgb = 3; | |
for (int i = 0; i < 10000; ++i) { | |
addPoint(img, color, rgb); | |
if(i%10 == 0){color--;} | |
if(color == 10){ | |
color++; | |
if(rgb != 0){ | |
color = 255; | |
rgb--; | |
} | |
if(rgb == 0){ | |
rgb = 3; | |
color=255; | |
} | |
} | |
//cout << "iteration: " << i << "\tcolor: "<< color << "\trgb: " << rgb << endl; | |
/* | |
if(i%100 == 0){ | |
cv::imshow("Aufgabe 4", img); | |
cv::waitKey(1); | |
} | |
*/ | |
} | |
} | |
int main() | |
{ | |
initalNumericalSeries[0] = 1; | |
int size_x = wsize, size_y = wsize; | |
img = cv::Mat(size_x, size_y, CV_8UC3, cv::Scalar(0, 0, 0)); | |
//initalisation | |
cv::namedWindow("Aufgabe 4"); | |
for (int j = 0; j < startingPoints; ++j) { | |
int y = randomInt(wsize); | |
int x = randomInt(wsize); | |
img.at<cv::Vec3b>(y, x)[3] = 255; | |
cout << "x" << x << "y" << y << endl; | |
} | |
vector<thread> threads; | |
for (int i = 0; i < threadcount; ++i) { | |
threads.push_back(thread(addPoints)); | |
} | |
for(thread &t:threads){ | |
while(t.joinable()){ | |
cv::imshow("Aufgabe 4", img); | |
cv::waitKey(100); | |
} | |
} | |
cv::imshow("Aufgabe 4", img); | |
cv::waitKey(0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment