Calculation of the volume of an irregular plot of land

Asked

Viewed 445 times

2

The code consists of calculating the volume of an uneven surface where I am using a matrix 2 x 2 of maximum size 1000 x 1000, which represents a rectangle, so the maximum area of the land is 1.000.000 m², and I must use a variable y free, which represents the height of the land, as the terrain is irregular the y varies, in areas of slopes and slopes of the land.

I want to calculate the total volume of the land, where I should use an incremental delta applied to the coordinates X, Y and Z. At each step of applying the delta to the coordinates, a calculation/addition of the volume of a small cube of volume delta³ to the total volume of the land, not exceeding the dimensions of the land or the calculation will be wrong, where here also for the coordinate Y, I must calculate a line of interpolation between two heights Y in order to determine the limit of cubes added for the calculation of the volume, that is, a limit for the progression of the loop that will systematically scan the height of the terrain.

When a cube crosses the boundary of the coordinate Y interpolated is the time to end the loop that increments the coordinate Y.

I’m doubtful how to do this I wrote a code, below follows the loop part, however an error occurs and I’m doubtful how to fix:

for(i=0; i<x; i++){ // x representa a largura do terreno
    for(j=0; j<z; j++){ // z representa a profundidade do terreno
        fscanf(y, "%.2f\t", altitude[i][j]); // Armazena os valores das coordenadas x e z na altura y ( DÚVIDA NESSA LINHA, SEMPRE QUE TENTO ARMAZENAR NA VARIÁVEL Y, OCORRE ERRO )
    }
} 

for(i=0; i<x; i++){
    for(j=0; j<z; j++){
        delta = aux - altitude[i][j];
        volume = volume +(altitude[i][j]*delta);
        aux=altitude[i][j];
    }
}

printf("\n O volume do terreno é: %f\n", volume);
  • 2

    What error appears when you compile?

  • Basically the error occurs in this line fscanf(y, "%.2f t", altitude[ i ] [ j ]); the program does not compile, and I also doubt if just this loop I wrote is enough to solve my problem.

  • Yeah, your question is really unclear. First of all, okay, the program doesn’t compile... but what is the compiler error message? Second, what do you want to do in this line of fscanf? If you only want to store the array value in the variable y, why didn’t you just y = altitude[i][j]?. On the question of calculation, you are considering the terrain as being divided into discrete portions (the cells of the array), right? Maybe if you prepare an illustrative drawing of how your matrix represents your terrain this will help (even you) in understanding the problem.

  • fscanf(f, "%f" , &( altitude[i][j] )) ?

1 answer

0

I don’t quite understand what you want to do in line with the instruction fscanf, but if you are trying to save the result to a file, it would be best to use the instruction fputs. Take a little look here.

But if you just want to calculate the total volume of the land, you just need to sum up all the heights you stored in the array. Something like this:

// inicializa volume com 0 para acumular no loop
int volume = 0;
for(i=0; i<x; i++){
    for(j=0; j<z; j++){
      // adiciona o volume de cada área
      volume += altitude[i][j];
    }
}

At the end of this loop, the variable volume shall have the total volume of the land.

  • On that line I want to store the value of the array in variable y, which is the height, because the height is varying and I need to take it into account for the volume calculation later.

  • And I also need to take into account the incremental delta for volume calculation, this incremental delta is analogous to a Riemann sum where to calculate the value of an integral you divide the curve into several rectangles.

  • From what I understand, each element of the array will have the height at point x,z. If so, there is no need for the delta, as the sum of the heights of all points will give you the total volume. In the calculation, the division to infinity is done to calculate the area of the curve (or in this case, the volume of a surface), but the interpolation is limited by the dimension of the array, where each element of the array is a minimum area to be added in the sum. In this case, as I said before, just add all the elements of the array to reach the total volume.

  • For example, if your land has a dimension of 10x10, and each point has a height of 10 (I’m simplifying), adding each value of the array you would reach 1000, which is the volume of the cube of 10x10x10 length.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.