Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Point
- {
- int x;
- int y;
- float elevation;
- };
- class Field
- {
- private:
- int rows;
- int columns;
- float t = 1;
- Point *points;
- int* grid;
- public:
- void randomGrid(){
- for (int i=0;i<(rows+1)*(columns+1);i++){
- *(grid+i)=rand()%5+1;
- }
- }
- Field(int rows, int columns)
- {
- this->rows = rows;
- this->columns = columns;
- this->points = new Point[rows * columns];
- this->grid = new int[(rows+1)*(columns+1)];
- randomGrid();
- }
- ~Field()
- {
- delete[] points;
- delete[] grid;
- }
- void setT(int t)
- {
- this->t = t;
- }
- float interpolationX(int x, int y)
- {
- int num1 = *(grid+y*this->columns+x);
- int num2 = *(grid+y*this->columns+x+1);
- return ((1-this->t)*num1) + (this->t*num2);
- }
- float interpolationY(float x1, float x2){
- return ((1-this->t)*x1) + (this->t*x2);
- }
- float setElevation(int x, int y)
- {
- float tx = interpolationX(x, y);
- float bx = interpolationX(x, y+1);
- float tb = interpolationY(tx, bx);
- return tb;
- }
- void noiseAll()
- {
- for (int y = 0; y < rows; y++)
- {
- for (int x = 0; x < columns; x++)
- {
- (points + y * columns + x)->elevation = setElevation(x, y);
- }
- }
- }
- float getElevation(int x, int y){
- return (points+y*columns+x)->elevation;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment