0

I am working on a project that needs to fill the cells of a 2D Array with the proper values in order to build two shapes, a pyramid and a cone. I will immensely appreciate any help on implementing this algorithm. Basically, I need to pass the x and y coordinates of the 2D Array and the function should return the proper height value at those coordinates. The minimum height is 0 and the maximum height is 100. enter image description here enter image description here

My functions have the following prototypes:

    float getPyramidHeight(int x, int y)
    {

    }

    float getConeHeight(int x, int y)
    {

    }

Basically, I have 2 variables:

w = 2D Array Length

and

h = 2D Array Width

This is where I am at right now:

#include <iostream>
#include <stdio.h>
using namespace std;

#define LENGTH 1000
#define WIDTH 800

float pyramidHeights[LENGTH][WIDTH]={0};
float coneHeights[LENGTH][WIDTH]={0};

float getPyramidHeight(int x, int y);
float getConeHeight(int x, int y);
void fillPyramid(int x, int y);
void fillCone(int x, int y);

int main()
{
    fillPyramid(LENGTH, WIDTH);
    fillCone(LENGTH, WIDTH);
    return 0;
}

void fillPyramid(int x, int y)
{
  for(int i=0;i<x;i++)
  {
     for(int j=0;j<y;j++)
     {
        pyramidHeights[i][j]=getPyramidHeight(i,j);
     }
  }
}

void fillCone(int x, int y)
{
  for(int i=0;i<x;i++)
  {
     for(int j=0;j<y;j++)
     {
        coneHeights[i][j]=getConeHeight(i,j);
     }
  }
}

float getPyramidHeight(int x, int y)
{
    //This is the first function I need to implement.

   return 0;
}

float getConeHeight(int x, int y)
{
    //This is the second function I need to implement.

   return 0;
}

I know it's not complete but this is as far as I could go. Thank you for your time!

AestheticCode
  • 115
  • 1
  • 7

1 Answers1

1

This is more about geometry than programming. First consider the cone and LENGTH=WIDTH. Due to symmetry, the height above a given point only depends on its distance from the center of the base. How? Draw a vertical cross section of the cone through the apex and try to find a relation between the two. Look for similar triangles.

In case LENGTH is not equal to WIDTH, the rotational symmetry is gone. Circles have transformed into ellipses. Each layer projects into the base as an ellipse, not a circle. The simplest thing to do is to imagine your cone was made by anisotropic stretching from a circular cone with unit radius and untransform your point into it. Then you can use the above. (In fact, scaling just one axis should be enough). I'll leave the rest up to you.

Given that your 2D grid is discrete, you should make sure that all computations are done with correct types and that you don't for example do integer division instead of floating-point division.

As for the code, I have following suggestions:

  • Do you really need both the headers you've included?

  • using namespace std at file scope is generally not recommended.

  • Don't use #defines this way. constexpr size_t or constexpr int might be more appropriate.

  • Usage of global variables is generally discouraged, although in a simple task like this they may be simpler to use than passing things around (if you can be sure that the project will not grow and will not be multithreaded).

  • If you decide to get rid of globals (but even if you don't), using std::array instead of C array would make passing and returning your structure easier. Using std::vector would even make the bounds configurable at runtime.

  • The forward declarations are fine, but in this case they're just needless boilerplate. I would consider reordering the functions instead.

  • Hey Lukas, after reading your explanation multiple times, I understood that the vertical height is proportional to the distance from the center, so, basically i mapped the distance from the center to the height and I've managed to build the pyramid. I've solved this in a single line! It's incredible, It's unbelievable how easy it is when you change the perspective of how you look at this problem. In regards to the global variables and defines, I am going to do how you told above because I've always did it like my example but I know it's not right. Thank You! – AestheticCode Jun 09 '20 at 10:43