fork download
  1. #include <vector>
  2. #include <iostream>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6.  
  7. bool generateDiamondMethod1 (const int &height)
  8. {
  9. static const int width = 1024;
  10. //an initial seed value for the corners of the data
  11. float SEED = 0.0f;
  12. static const unsigned int DATA_SIZE=width+1;
  13. std::vector< std::vector<float> > diamond( DATA_SIZE, std::vector<float>(DATA_SIZE) );
  14. //float diamond[DATA_SIZE][DATA_SIZE];
  15.  
  16. //initialise the values of the corners++
  17. diamond[0][0] = SEED;
  18. diamond[0][DATA_SIZE-1] = SEED;
  19. diamond[DATA_SIZE-1][0] = SEED;
  20. diamond[DATA_SIZE-1][DATA_SIZE-1] = SEED;
  21.  
  22. float h =200; //the range (-h -> h) for the average offset
  23. srand(512); //seed the random generator
  24.  
  25. //side length is the distance of a single square side
  26. //or distance of diagonal in diamond
  27. //each iteration we are looking at smaller squares and diamonds, we decrease the variation of the offset
  28. for (int sideLength = DATA_SIZE-1; sideLength >= 2; sideLength /= 2, h /= 2.0)
  29. {
  30.  
  31. int halfSide = sideLength/2;
  32.  
  33. //generate new square values
  34. for(int x=0; x<DATA_SIZE-1; x+=sideLength)
  35. {
  36. for(int y=0; y<DATA_SIZE-1; y+=sideLength)
  37. {
  38.  
  39. //x,y is upper left corner of the square
  40. //calculate average of existing corners
  41. float avg = diamond[x][y] + //top left
  42. diamond[(x+sideLength)%DATA_SIZE][y] + //top right
  43. diamond[x][ (y+sideLength)%DATA_SIZE] + //lower left
  44. diamond[(x+sideLength)%DATA_SIZE][(y+sideLength)%DATA_SIZE]; //lower right
  45.  
  46. avg /= 4.0;
  47.  
  48. //center is average plus random offset in the range (-h, h)
  49. float offset = (-h) + (float)rand() * (h - (-h)) / RAND_MAX;
  50.  
  51. diamond[x+halfSide][y+halfSide] = avg + offset;
  52.  
  53. } //for y
  54. } // for x
  55.  
  56. //Generate the diamond values
  57. //Since diamonds are staggered, we only move x by half side
  58. //NOTE: if the data shouldn't wrap the x < DATA_SIZE and y < DATA_SIZE
  59. for (int x=0; x<DATA_SIZE-1; x+=halfSide)
  60. {
  61. for (int y=(x+halfSide)%sideLength; y<DATA_SIZE-1; y+=sideLength)
  62. {
  63.  
  64. //x,y is center of diamond
  65. //we must use mod and add DATA_SIZE for subtraction
  66. //so that we can wrap around the array to find the corners
  67.  
  68. float avg =
  69. diamond[(x-halfSide+DATA_SIZE)%DATA_SIZE][y] + //left of center
  70. diamond[(x+halfSide)%DATA_SIZE][y] + //right of center
  71. diamond[x][(y+halfSide)%DATA_SIZE] + //below center
  72. diamond[x][(y-halfSide+DATA_SIZE)%DATA_SIZE]; //above center
  73.  
  74. avg /= 4.0;
  75.  
  76. //new value = average plus random offset
  77. //calc random value in the range (-h,+h)
  78. float offset = (-h) + (float)rand() * (h - (-h)) / RAND_MAX;
  79.  
  80. avg = avg + offset;
  81.  
  82. //update value for center of diamond
  83. diamond[x][y] = avg;
  84.  
  85. //wrap values on the edges
  86. //remove this and adjust loop condition above
  87. //for non-wrapping values
  88. if (x == 0) diamond[DATA_SIZE-1][y] = avg;
  89. if (y == 0) diamond[x][DATA_SIZE-1] = avg;
  90. } //for y
  91. } //for x
  92. } //for sideLength
  93.  
  94.  
  95. cout << "\r\nData Size " << DATA_SIZE;
  96. cout << "\r\nImage Size " << width << "x" << width;
  97. cout << "\r\nLocation1 "<< diamond[305][742];
  98. cout << "\r\nLocation2 " <<diamond[288][732];
  99.  
  100. /// Set maxY and minY to 0.0f
  101. float maxY = diamond[1][1];
  102. float minY = diamond[1][1];
  103.  
  104. cout << "\r\nMinimumY " << minY;
  105. cout << "\r\nMaximumY " << maxY;
  106.  
  107. cout << "(Loop to find highest and new minimum)";
  108.  
  109. for (int x = 0; x<DATA_SIZE; x++)
  110. {
  111. for(int y = 0; y<DATA_SIZE; y++)
  112. {
  113. if ((float)diamond[x][y] > maxY)
  114. {
  115. maxY = diamond[x][y];
  116. }
  117. if ((float)diamond[x][y] < minY)
  118. {
  119. minY = diamond[x][y];
  120. }
  121. }
  122. }
  123.  
  124. cout << "\r\nMinimumY " << minY;
  125. cout << "\r\nMaximumY " << maxY << "\r\n";
  126.  
  127. /// Calculate height from 0 to 1
  128. for(int x=0; x < DATA_SIZE; x++)
  129. {
  130. for(int y=0; y < DATA_SIZE; y++)
  131. {
  132. //change range to 0..1
  133. diamond[x][y] = (diamond[x][y] - minY) / (maxY - minY);
  134. }
  135.  
  136. }
  137.  
  138. /// Copy color float from create texture
  139. for(unsigned y = 0; y<width; y++)
  140. {
  141. for(unsigned x = 0; x<height; x++)
  142. {
  143. /// incremennt memory which seems to work
  144. int index = (y*width)+x;
  145.  
  146. //buffer[index]=diamond[x][y];
  147. }
  148. }
  149.  
  150. return true;
  151. }
  152.  
  153. int main()
  154. {
  155. generateDiamondMethod1(1024);
  156. }
Success #stdin #stdout 0.06s 3432KB
stdin
Standard input is empty
stdout
Data Size 1025
Image Size 1024x1024
Location1 -24.0164
Location2 -13.1731
MinimumY 0.0260689
MaximumY 0.0260689(Loop to find highest and new minimum)
MinimumY -59.9904
MaximumY 194.645