Sign in to follow this  

using multi-dimensional arrays as input arguments

Recommended Posts

Hi there

I have 3 points of X,Y and Z values to make a triangle on a plane.

I am wishing to pass these variables to a function that would then be bale to calculate the normal for me.

I am using an array of 4, this is passed to another function which can then generate a wall and a triangle.

I then want to be able to pass this to the function for normal calculation.

int normalArray[3][3][3] = {wallArray[0], 0, wallArray[1],wallArray[0], 3, wallArray[1],wallArray[2], 3, wallArray[3]};

Do I just need to do void myFunction(normalArray[][][]) as the argument?

Many thanks

Share this post

Link to post
Share on other sites
Only the first dimension may be of unspecified length; the other dimensions must be of fixed sized. So change the function prototype to

void myFunction(int normalArray[][3][3])

But I would not recommend using raw arrays anyway. Use first class types as boost's multiarray for multi dimensional arrays that behaves more like values.

Share this post

Link to post
Share on other sites
How would I do that? Do I need to download a new library?

What I am trying to achieve is to enable for normals and drawing to occour generically on any wall. This would then allow for a user to be able to create there own walls... in idea.

I currently am taking in 4 input arguments for a wall (all walls are the same height for simplicity).
int wall1[] = {-50,-150,-50,50};

I am then passing this to a function to draw the wall and create 3 XYZ co-ordinate to define a plane so a normal can be calculated for lighting and collision detection.

int * Enviroment::drawWall(int wallArray[])

int normalArray[3][3] = {wallArray[0], 0, wallArray[1],wallArray[0], 3, wallArray[1],wallArray[2], 3, wallArray[3]};

std::cout<<"the array1 is "<<normalArray[0][0]<<std::endl;
std::cout<<"the array2 is "<<normalArray[0][1]<<std::endl;
std::cout<<"the array3 is "<<normalArray[0][2]<<std::endl;


glTexCoord2f(0,0); glVertex3f(wallArray[0], 0, wallArray[1]);
glTexCoord2f(1,0); glVertex3f(wallArray[0], 3, wallArray[1]);
glTexCoord2f(1,1); glVertex3f(wallArray[2], 3, wallArray[3]);
glTexCoord2f(0,1); glVertex3f(wallArray[2], 0, wallArray[3]);



return *normalArray;

I am then wanting to use the normal array in my last calculation for normal calculation. I am about to change thos to access the new arrays. It calculates the cross products and returns an output.

int * Collision::CalculateNormal(int normalArray[3][3],int normal[3])

//calcualte cross product
float ux = point2[0] - point1[0];
float uy = point2[1] - point1[1];
float uz = point2[2] - point1[2];
float vx = point3[0] - point1[0];
float vy = point3[1] - point1[1];
float vz = point3[2] - point1[2];

//float normal[3];
normal [0] = ((vy*uz) - (vz*uy))*-1;
normal [1] = ((vz*ux) - (vx*uz));
normal [2] = ((vx*uy) - (vy*ux));
/*normal [0] = ((uy*vz) - (uz*vy))*-1;
normal [1] = ((ux*vz) - (uz*vx));
normal [2] = ((ux*vy) - (uy*vx));*/

return normal;


Is there a better way to do this?
Is there a way to return 3 arrays?
Any help would be greatly appreciated
Thanks again

Share this post

Link to post
Share on other sites
Why not use types to logically group the data?

struct Vector3 {
float x, y, z;

struct Triangle {
Vector3 vertices[3];

Vector3 normal(const Triangle &triangle) {
// ...

You didn't really explain the purpose of the outermost dimension of your array. I don't understand what you mean by "I am using an array of 4", do you mean a 4 element array of triangles?

C++ arrays are second class citizens. They don't act like other types, you cannot assign them and they decay to pointers frequently. You cannot return them from a function. I recommend that you avoid them if you can in favour of other data structures. Wrapping an array in a structure (like the Triangle in the example above) can help them behave.

Share this post

Link to post
Share on other sites
Sorry for my lack of clarification.

the 4 array I was talking about occours in the main and is used to draw the wall. it takes in the firstX,firstZ,lastX,lastZ. At the moment I am just setting them to numbers but am hoping to change it to variables of a mouse click. a click of the mouse would give firstX and Z secon click of the mouse would give endX endZ and produce a wall.

(My Y axis is currently always between 0 and 3 for easiness and to ensure all the walls are the same size)

I am then passing these to the function to draw a wall this can then create the information required to calculate normals.

Are you suggesting within my wall method to create a struct?
This would then take my 3 points?

would this be like

vector3.x = array[0];
vector3.y = array [1];
vector3.z = array [2];
vector3.x = array2[0];
vector3.y = array2[1];
vector3.z = array2[2];

vector3.x = array3[0];
vector3.y = array3[1];
vector3.z = array3[2];


Would I then pass the vertice to my Collision Class?

like normal[] = Collision::CalculateNormal(vertice1,vertice2,vertice3);

Sorry I have never tried to pass information in multiple places, assign different sizes etc before and am new to trying this.

I just want to make sure I have the right idea

I am using C++ I don't know if that makes any difference as I am not used to C but I know C is more heavy on strcuts then C++.

I appreciate all your help

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this