main.cpp 3.16 KB
Newer Older
plgruener's avatar
plgruener committed
1
#include <vector>
2
#include <random>
plgruener's avatar
plgruener committed
3
#include <cmath>
4
#include <iostream>
5
#include <fstream>
plgruener's avatar
plgruener committed
6

7
static const double PI = std::acos(-1);
plgruener's avatar
plgruener committed
8

9
int main(int argc, char* argv[]) {
10

11 12 13 14
  // Default parameters
  int iterations = 100;
  int seedpoints = 10;
  int intervals = 100;
plgruener's avatar
plgruener committed
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  // Get parameters from command line
  if (argc == 1) {
    std::cout << "Warning: Not all parameters were given. Using parameters " 
    << iterations << ", " << seedpoints << ", " << intervals << " .." <<std::endl;
  }
  else if (argc == 2) {
    iterations = atoi(argv[1]);
    std::cout << "Warning: Not all parameters were given. Using parameters " 
    << iterations << ", " << seedpoints << ", " << intervals << " .." <<std::endl;
  }
  else if (argc == 3) {
    iterations = atoi(argv[1]);
    seedpoints = atoi(argv[2]);
    std::cout << "Warning: Not all parameters were given. Using parameters " 
    << iterations << ", " << seedpoints << ", " << intervals << " .." <<std::endl;
  }
  else {
    iterations = atoi(argv[1]);
    seedpoints = atoi(argv[2]);
    intervals = atoi(argv[3]);
  }
37

38 39 40
  double alphabetamax = 1;
  double intervalsize = alphabetamax/intervals;
  std::cout << "intervallsize: " << intervalsize <<std::endl;
plgruener's avatar
plgruener committed
41

42
  // Generate vectors x, y
43
  const int sample_size = seedpoints * intervals * intervals;
44 45 46 47 48
  std::vector<double> x(sample_size);
  std::vector<double> y(sample_size);
  double * x_ = x.data();
  double * y_ = y.data();

49

50
  // Generate seedpoints
51 52
  std::mt19937 gen(std::random_device{}());
  std::uniform_real_distribution<> dis(0, 1);
53
  std::vector<double> randv;
54
  for(int i=0; i < seedpoints; ++i) {
55 56 57 58
    double randn = dis(gen);
    randv.push_back(randn);
  }

59
  // Fill vectors x, y with seedpoints
60 61 62 63 64 65
  int j; 
  for (int i = 0; i < sample_size; ++i) {
    j = i % seedpoints;
    y_[i] = randv[j];
    x_[i] = randv[j];
  }
66

67 68 69 70 71 72
  // Save seedpoints
  std::vector<double> x_start = x;
  std::vector<double> y_start = y;

  // Generate pixel vector
  std::vector<int> pixel(intervals * intervals, 1);
73

74 75
  const double TWO_PI = 2 * PI;
  for (int j = 0; j < iterations; ++j) {
76
    for (int i = 0; i < sample_size; ++i) {
77 78 79
      double beta = ((i / seedpoints) % intervals) * intervalsize;
      double alpha = int((i / seedpoints) / intervals) * intervalsize;
      // std::cout << i << ": beta: " << beta << " -- alpha: " << alpha <<std::endl;
80 81 82
      y_[i] = y_[i] + beta * std::sin(TWO_PI * x_[i]);
      x_[i] = x_[i] + alpha * std::sin(TWO_PI * y_[i]);
    }
83 84
  }

85
  // Map calculated vectors x and y to pixel vector
86
  for (int i = 0; i < sample_size; ++i) {
87 88
    y_start[i] = y_[i] - y_start[i];
    x_start[i] = x_[i] - x_start[i];
89

90
    if (y_start[i] > 1 || x_start[i] > 1) {
91
      pixel[int(i / seedpoints)] = 255;
92
    }
93

94 95
    //std::cout << i << "::: --- y: " << y_start[i] << ", x: " << x_start[i] <<std::endl;
  }
96

97 98 99 100 101 102
  // Output pixel vector into PGM File
  std::string filename = "picture.pgm";
  std::ofstream ostrm(filename);
  ostrm << "P2" << '\n';
  ostrm << intervals << ' ' << intervals << '\n';
  ostrm << 255 << '\n'; // max. gray value
103
  for (int i = 0; i < intervals*intervals; ++i){
104
    if ( (i%intervals) == 0) {
105
      ostrm << '\n';
106
    }
107
    ostrm << pixel[i] << ' ';
108
  }
109
  ostrm <<std::endl;
plgruener's avatar
plgruener committed
110 111 112

  return 0;
}