Ejemplo n.º 1
0
void ArcTerrain::diamondSquare(int tL[], int tR[], int bL[], int bR[], int level)
{
  // PROBLEM: ACCESSING UNASSIGNED VALUES FOR AVERAGE
  //std::cout << "bR = " << bR[0] << ", " << bR[1] << std::endl;
  //std::cout << "bL = " << bL[0] << ", " << bL[1] << std::endl;
  // find average
  float average = (mMap[tL[0]][tL[1]] +
		   mMap[tR[0]][tR[1]] +
		   mMap[bL[0]][bL[1]] +
		   mMap[bR[0]][bR[1]]) / 4;
  // find midPoint
  int cX = (tL[0] + bR[0]) / 2;
  int cY = (tL[1] + bR[1]) / 2;
  int C[] = {cX, cY};


  mMap[cX][cY] = average + randomHeight(level--);
  if (mMap[cX][cY] > mMax) mMax = mMap[cX][cY];
  if (mMap[cX][cY] < mMin) mMin = mMap[cX][cY];
  int T[] = {cX, tL[1]};
  double tA = (mMap[tL[0]][tL[1]] + 
	       mMap[tR[0]][tR[1]] + 
	       mMap[cX][cY]) / 3;

  //std::cout << cX << ", " << bL[1] << std::endl;
  int B[] = {cX, bL[1]};
  double bA = (mMap[bL[0]][bL[1]] + 
	       mMap[bR[0]][bR[1]] + 
	       mMap[cX][cY]) / 3;

  int L[] = {tL[0], cY};
  double lA = (mMap[tL[0]][tL[1]] + 
	       mMap[bL[0]][bL[1]] + 
	       mMap[cX][cY]) / 3;

  int R[] = {tR[0], cY};
  double rA = (mMap[tR[0]][tR[1]] + 
	       mMap[bR[0]][bR[1]] + 
	       mMap[cX][cY]) / 3;
 
  // Set midpoint to average + random height
  mMap[T[0]][T[1]] = tA;
  mMap[B[0]][B[1]] = bA;
  mMap[L[0]][L[1]] = lA;
  mMap[R[0]][R[1]] = rA;
  //level--;

  if (level > 0)
  {
    diamondSquare(tL, T, L, C, level); // TL
    diamondSquare(T, tR, C, R, level); // TR
    diamondSquare(L, C, bL, B, level); // BL
    diamondSquare(C, R, B, bR, level); // BR
  }
}
Ejemplo n.º 2
0
void DiamondSquare::run(int seed,double * array,int width,int height)
{
    this->height=height;
    this->width=width;
    values=array;

    int featuresize=512;
    //values = new float[width * height];
    srand (seed);

    for( int y = 0; y < height; y += featuresize)
        for (int x = 0; x < width; x += featuresize)
        {
            setSample(x, y, random());
            //setSample(x,y,0);
        }

    int samplesize = featuresize;

    double scale = 1.0;

    while (samplesize > 1)
    {

        diamondSquare(samplesize, scale);

        samplesize /= 2;
        scale /= 2.0;
    }

    double val;
    for( int y = 0; y < height; y ++)
        for (int x = 0; x < width; x ++)
        {
            val=sample(x,y);
            if(val>1.0)
                setSample(x, y, 1.0);
            else if(val<-1.0)
                setSample(x,y,-1.0);
            //setSample(x,y,0);
        }
}
Ejemplo n.º 3
0
int main() {
	srand(time(NULL));
	initscr();
	checkMakeColors();
	
	noecho();
	
	int timer = 0;
	int maxY;
	int maxX;
	double a;
	double map[HEIGHT * WIDTH]; //h,w
	
	//initMap(map);
	
	for (;;) {
		if (timer >= RUNLENGTH) {
			break;
		}
		getmaxyx(stdscr, maxY, maxX); //get max coords
		
		//if (timer % 100 == 0) {
			initMap(map);
			diamondSquare(map);
		//}
		
		//erase();
		drawMap(map, maxY, maxX);
		printThatThingInTheCenter(maxY, maxX, timer);
		refresh(); //spit to screen
		
		usleep(1000 * DELAY);
		timer++;
	}
	
	endwin(); //End curses mode
	return 0;
}