# Terrain Generation

**URL:** https://discourse.processing.org/t/terrain-generation/28740
**Category:** Coding Questions
**Created:** [March 22, 2021, 7:49pm UTC](https://discourse.processing.org/t/terrain-generation/28740 "2021-03-22T19:49:21Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [March 22, 2021, 7:49pm UTC](https://discourse.processing.org/t/terrain-generation/28740/1 "2021-03-22T19:49:21Z")

</div>

hi all

how to make this shape if any one can help

![1-norm](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/587bf6393eb7aa1216219c19f3e8e16eadc9dfb1.jpeg)

i found this equation :  
centerpoint (x1, y1) and a radius r, the height of the hill at the point (x2, y2) is equivalent to:

![equation](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2594b995b7044c7c9b358ea4a492da360fda612c.gif)

how to use it to form this terrain

can and how if i can using it in this code

```auto
// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Code for: https://youtu.be/IKB1hWWedMk

int cols, rows;
int scl = 20;
int w = 2000;
int h = 1600;

float flying = 0;

float[][] terrain;

void setup() {
  size(600, 600, P3D);
  cols = w / scl;
  rows = h/ scl;
  terrain = new float[cols][rows];
 colorMode(HSB, 100, 100, 100);
}

void draw() {

  flying -= 0.1;

  float yoff = flying;
  for (int y = 0; y < rows; y++) {
    float xoff = 0;
    for (int x = 0; x < cols; x++) {
      terrain[x/2][y/2] = map(noise(xoff, yoff), 0, 1, -100, 100);
      xoff += 0.2;
    }
    yoff += 0.2;
  }

  background(0);
// stroke(255);
  //noFill();
//noStroke();
  translate(width/2, height/2+50);
  rotateX(PI/3);
  translate(-w/2, -h/2);
  for (int y = 0; y < rows-1; y++) {
   // beginShape(TRIANGLE_STRIP);
    beginShape(TRIANGLE_STRIP);
    for (int x = 0; x < cols; x++) {
      
      float z = terrain[x][y]; 
      fill(z+20, 100, 100);    
      vertex(x*scl, y*scl, z);
      
      float z1 = terrain[x][y+1];
      fill(z1+20, 100, 100);        
      vertex(x*scl, (y+1)*scl, z1);
      //rect(x*scl, y*scl, scl, scl);
    }
    endShape();
  }
}

```

thanks for all

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 22, 2021, 9:35pm UTC](https://discourse.processing.org/t/terrain-generation/28740/2 "2021-03-22T21:35:36Z")

</div>

I couldn’t do it

```auto

// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Code for: https://youtu.be/IKB1hWWedMk

int cols, rows;
int scl = 20;
int w = 2000;
int h = 1600;

float flying = 0;

float[][] terrain;

void setup() {
  size(1600, 900, P3D);
  cols = w / scl;
  rows = h/ scl;
  terrain = new float[222][222];
  colorMode(HSB, 100, 100, 100);

  // -----
  flying -= 0.1;

  float x1= 230; // w/2; 
  float y1= 230; // h/2; 
  float r = 5.015;  

  float yoff = 0;
  int y3=0; 
  for (int y = -100; y < 100; y++) {
    float xoff = 0;
    int x3=0; 
    for (int x = -100; x < 100; x++) {
      float a1 = ( pow2 ( xoff-x1 )) + ( pow2 (yoff-y1) ) ; 
      float b1 = (r*r) - a1; // map(noise(xoff, yoff), 0, 1, -100, 100);

      terrain[x3][y3] = map(b1, -100000, 100000, -3000, 3000);
      println(b1); 

      xoff += 4;
      x3++;
    }
    yoff += 4;
    y3++;
  }
}

float pow2 ( float in_) {
  return 
    in_*in_;
}

void draw() {
  background(0);
  lights();

  // stroke(255);
  //noFill();
  //noStroke();
  translate(width/2, height/2-222, -600);
  rotateX(PI/3);
  translate(-w/2, -h/2);
  for (int y = 0; y < rows-1; y++) {
    // beginShape(TRIANGLE_STRIP);
    beginShape(TRIANGLE_STRIP);
    for (int x = 0; x < cols; x++) {

      float z = terrain[x][y]; 
      fill(z+20, 100, 100);    
      //noStroke(); 
      vertex(x*scl, y*scl, z);

      float z1 = terrain[x][y+1];
      fill(z1+20, 100, 100);        
      vertex(x*scl, (y+1)*scl, z1);
      //rect(x*scl, y*scl, scl, scl);
    }
    endShape();
  }
}
//

```

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [March 22, 2021, 9:46pm UTC](https://discourse.processing.org/t/terrain-generation/28740/3 "2021-03-22T21:46:45Z")

</div>

> [@Chrisir](#):
>
> I couldn’t do it

it is more than great that you give it a try ❤

i played with this line once got some change

```auto
float z = terrain[x^2][y^2]; 

```

and once again with this got some change

```auto
terrain[x^2][y^2] = map(noise(xoff, yoff), 0, 1, -100, 100);

```

i am still learning i found this site few time a go

[https://mathcs.holycross.edu/~spl/old\_courses/126\_spring\_2004/handouts/02.25.04.html](https://mathcs.holycross.edu/~spl/old_courses/126_spring_2004/handouts/02.25.04.html)

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [March 22, 2021, 9:54pm UTC](https://discourse.processing.org/t/terrain-generation/28740/4 "2021-03-22T21:54:36Z")

</div>

its great maybe the equation above for the c++ here were i found it

[https://www.stuffwithstuff.com/robot-frog/3d/hills/hill.html](https://www.stuffwithstuff.com/robot-frog/3d/hills/hill.html)

your result is close

 ![Untitled 78](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b36673861613d2f49b73be6b112c483650279737.jpeg)

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [March 22, 2021, 9:59pm UTC](https://discourse.processing.org/t/terrain-generation/28740/5 "2021-03-22T21:59:23Z")

</div>

Hello @jafal,

The first equation you have is not correct.  
The equation of a sphere is:  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/ec892022ad6abfb87d9aed3f0e4263d98638fa3d.png)

So you actually need a z² or to take the square root of the right side of your equation.

To implement it, you simply need to compute the z value of your grid depending on the x, y coordinates.

BUT you also need to check if you are in the sphere boundary. The equation is valid only for x and y coordinate within the sphere. Outside the sphere you want the height equal to 0.

Hope this piece of code helps you:

```auto
import peasy.PeasyCam;

PeasyCam cam;
float[][] terrain;

void setup() {
  size(800, 600, P3D);
  
  cam = new PeasyCam(this, 400);
  
  terrain = new float[300][300];
  colorMode(HSB, 100, 100, 100);

  float x0= 150;
  float y0= 150;
  float r = 100;  

  for (int x = 0; x < terrain.length; x++) {
    for (int y = 0; y < terrain[0].length; y++) {
      if (r * r - ((x- x0) * (x - x0) + (y- y0) * (y - y0)) > 0) {
        terrain[x][y] = sqrt(r * r - ((x- x0) * (x - x0) + (y- y0) * (y - y0)));
      } else {
        terrain[x][y] = 0;
      }
    }
  }
}

void draw() {
  background(20);
  lights();

  translate(-150, 0, -600);
  rotateX(PI/3);
  translate(0, 0);
  for (int y = 0; y < terrain[0].length - 1; y++) {
    beginShape(TRIANGLE_STRIP);
    for (int x = 0; x < terrain.length; x++) {
      fill(220, 40, 40);
      noStroke();
      vertex(x, y, terrain[x][y]);
      vertex(x, y + 1, terrain[x][y + 1]);
    }
    endShape();
  }
}

```

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/928eb664d9f3bd0fd927f0833f05c936a10b3a6c.png)

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [March 22, 2021, 10:23pm UTC](https://discourse.processing.org/t/terrain-generation/28740/6 "2021-03-22T22:23:52Z")

</div>

Hello @[jb4x](https://discourse.processing.org/u/jb4x)

it is charming and great i have said to @Chrisir maybe its for c++ as apart of the code

many thanks for you … i am totally new and fresh 😄 to processing and my way for understanding is to study sketches for a certain issue and make comparing

God with you warm greeting

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [March 23, 2021, 1:26am UTC](https://discourse.processing.org/t/terrain-generation/28740/7 "2021-03-23T01:26:12Z")

</div>

> [@jafal](#):
>
> Hello sir @[jb4x](https://discourse.processing.org/u/jb4x)

i just wonder why it is without mesh and how to make it looks like the red mesh ?

Thanks for you

 ![mesh](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/9812488c1009e8650c2b7abb93883a47a400fe67.png)

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 23, 2021, 6:32am UTC](https://discourse.processing.org/t/terrain-generation/28740/8 "2021-03-23T06:32:06Z")

</div>

It’s because of noStroke()

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [March 23, 2021, 2:39pm UTC](https://discourse.processing.org/t/terrain-generation/28740/9 "2021-03-23T14:39:48Z")

</div>

hi  
the photo i post is with stroke i add it

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [March 24, 2021, 3:36am UTC](https://discourse.processing.org/t/terrain-generation/28740/10 "2021-03-24T03:36:42Z")

</div>

@[Chrisir](https://discourse.processing.org/u/Chrisir)  
peace upon you

I have been studying your code for one night and feel that the issue is how the grid constructed.

See the equation here gave a result with Mr [jb4x](https://discourse.processing.org/u/jb4x) code

```auto
  terrain[x][y] = sqrt(r * r - ((x- x0) * (x - x0) + (y- y0) * (y - y0)));

```

i added the same equation with your code gave a close result because the dome appeared  
And the square base remains to complete the shape

```auto
 terrain[x3][y3] = (r * r - ((xoff-x1 ) * (xoff- x1) + (yoff- y1) * (yoff - y1)));
     

```

I tried to solve the problem, but I could not

Do you have any suggestions?

```auto
// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Code for: https://youtu.be/IKB1hWWedMk

int cols, rows;
int scl = 25;
int w = 2000;
int h = 2200;

float flying = 0;

float[][] terrain;

void setup() {
  size(800, 900, P3D);
  cols = w / scl;
  rows = h/ scl;
  terrain = new float[222][222];
  colorMode(HSB, 100, 100, 100);

  // -----
  flying -= 0.1;

  float x1= 230; // w/2; 
  float y1= 230; // h/2; 
  float r = 5.015;  

  float yoff = 0;
  int y3=0; 
  for (int y = -100; y < 100; y++) {
    float xoff = 0;
    int x3=0; 
    for (int x = -100; x < 100; x++) {
     // float a1 = ( pow2 ( xoff-x1 )) + ( pow2 (yoff-y1) ) ; 
      //float b1 = (r*r) - a1; // map(noise(xoff, yoff), 0, 1, -100, 100);
  terrain[x3][y3] = (r * r - ((xoff-x1 ) * (xoff- x1) + (yoff- y1) * (yoff - y1)));
     // terrain[x3][y3] = map(b1, -100000, 100000, -3000, 3000);
     // println(b1); 

      xoff += 4;
      x3++;
    }
    yoff += 4;
    y3++;
  }
}

float pow2 ( float in_) {
  return 
    in_*in_;
}

void draw() {
  background(0);
  lights();

  // stroke(255);
  //noFill();
  //noStroke();
  translate(width/2, height/2-222, -600);
  rotateX(PI/3);
  translate(-w/2, -h/2);
  for (int y = 0; y < rows-1; y++) {
    // beginShape(TRIANGLE_STRIP);
    beginShape(TRIANGLE_STRIP);
    for (int x = 0; x < cols; x++) {

      float z = terrain[x][y]; 
      fill(z+20, 100, 100);    
      //noStroke(); 
      vertex(x*scl, y*scl, z);

      float z1 = terrain[x][y+1];
      fill(z1+20, 100, 100);        
      vertex(x*scl, (y+1)*scl, z1);
      //rect(x*scl, y*scl, scl, scl);
    }
    endShape();
  }
}
//

```

 ![Untitled 84](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5618f19b4c81b8e8040243bf7e67d92e333313d8.jpeg)

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 24, 2021, 6:41am UTC](https://discourse.processing.org/t/terrain-generation/28740/11 "2021-03-24T06:41:33Z")

</div>

jb4x code is much better than mine

In his code there is one line noStroke()

Comment it out or replace it with stroke(0);

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [March 30, 2021, 7:50pm UTC](https://discourse.processing.org/t/terrain-generation/28740/12 "2021-03-30T19:50:28Z")

</div>

[Chrisir](https://discourse.processing.org/u/Chrisir)

hi this is same [jb4x](https://discourse.processing.org/u/jb4x) code but more More flexible

```auto
int cols, rows;
int scl =15;
int w = 2000;
int h = 1600;

float flying = 0;

float[][] terrain;
import peasy.PeasyCam;

PeasyCam cam;
void setup() {
  size(800, 800, P3D);
  cam = new PeasyCam(this, 400);
  cols = w / scl;
  rows = h/ scl;
  terrain = new float[cols][rows];
}

void draw() {

  flying -= 0.1;

  float yoff = flying;
  for (int y = 0; y < rows; y++) {
    float xoff = 0;
    for (int x = 0; x < cols; x++) {
 if (2222 - ((x- 53) * (x - 53) + (y- 53) * (y - 53)) > 0) {
   terrain[x][y] = sqrt(2222 - ((x- 53) * (x - 53) + (y- 53) * (y - 53)))*12;
   
      
      
} 
     // terrain[x][y] = map(noise(xoff, yoff), 0, 1, -100, 100);
      xoff += 0.2;
    }
    yoff += 0.2;
  }

  background(0,222,1);
 // stroke(2,5,255);
 stroke(5);
 strokeWeight(7);
  noFill();
scale(.2);
  translate(width/2, height/2+50);
  rotateX(PI/3);
  translate(-w/2, -h/2);
  for (int y = 0; y < rows-1; y++) {
    beginShape(TRIANGLE_STRIP);
    for (int x = 0; x < cols; x++) {
      vertex(x*scl, y*scl, terrain[x][y]);
      vertex(x*scl, (y+1)*scl, terrain[x][y+1]);
      //rect(x*scl, y*scl, scl, scl);
    }
    endShape();
  }
}

```

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [March 30, 2021, 7:51pm UTC](https://discourse.processing.org/t/terrain-generation/28740/13 "2021-03-30T19:51:51Z")

</div>

![Untitled 133](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/eca3de51a009f59322fe8ce40f51c6f86a511374.jpeg) ![Untitled 134](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b76569d887aacfee9f47a71f637812a248a01be1.jpeg)

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [March 30, 2021, 7:56pm UTC](https://discourse.processing.org/t/terrain-generation/28740/14 "2021-03-30T19:56:47Z")

</div>

@[jb4x](https://discourse.processing.org/u/jb4x)

many thanks for you

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [March 30, 2021, 8:03pm UTC](https://discourse.processing.org/t/terrain-generation/28740/15 "2021-03-30T20:03:02Z")

</div>

![Untitled 135](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a6af81444097c484398782668856ef57950f369d.jpeg)
