Guest User

Untitled

a guest
Nov 16th, 2020
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. struct Point
  2. {
  3.     int x;
  4.     int y;
  5.     float elevation;
  6. };
  7. class Field
  8. {
  9.   private:
  10.     int rows;
  11.     int columns;
  12.     float t = 1;
  13.     Point *points;
  14.     int* grid;
  15.   public:
  16.     void randomGrid(){
  17.         for (int i=0;i<(rows+1)*(columns+1);i++){
  18.             *(grid+i)=rand()%5+1;
  19.         }
  20.     }
  21.     Field(int rows, int columns)
  22.     {
  23.         this->rows = rows;
  24.         this->columns = columns;
  25.         this->points = new Point[rows * columns];
  26.         this->grid = new int[(rows+1)*(columns+1)];
  27.         randomGrid();
  28.     }
  29.     ~Field()
  30.     {
  31.         delete[] points;
  32.         delete[] grid;
  33.     }
  34.     void setT(int t)
  35.     {
  36.         this->t = t;
  37.     }
  38.     float interpolationX(int x, int y)
  39.     {
  40.         int num1 = *(grid+y*this->columns+x);
  41.         int num2 = *(grid+y*this->columns+x+1);
  42.         return ((1-this->t)*num1) + (this->t*num2);
  43.     }
  44.     float interpolationY(float x1, float x2){
  45.         return ((1-this->t)*x1) + (this->t*x2);
  46.     }
  47.     float setElevation(int x, int y)
  48.     {
  49.         float tx = interpolationX(x, y);
  50.         float bx = interpolationX(x, y+1);
  51.         float tb = interpolationY(tx, bx);
  52.         return tb;
  53.     }
  54.     void noiseAll()
  55.     {
  56.         for (int y = 0; y < rows; y++)
  57.         {
  58.             for (int x = 0; x < columns; x++)
  59.             {
  60.                 (points + y * columns + x)->elevation = setElevation(x, y);
  61.             }
  62.         }
  63.     }
  64.     float getElevation(int x, int y){
  65.         return (points+y*columns+x)->elevation;
  66.     }
  67. };
Advertisement
Add Comment
Please, Sign In to add comment