tags:

views:

191

answers:

4

I am just trying something with someones code.

I have two functions:

int Triangle(Render *render, int numParts, Token *nameList, Pointer *valueList) 
    int i;
    for (i=0; i<numParts; i++)
    {
     switch (nameList[i])
     {
     case GZ_NULL_TOKEN:
      break;

     case GZ_POSITION:
      return putTrianglePosition(render, (Coord *)valueList[i]);
      break;
     }
    }

    return SUCCESS;
}

and the other is:

int putTrianglePosition(Render *render, Coord vertexList[3]) /*vertexList[3][3:xyz]*/
{
    Coord *pv[3];
    int i,j;

    // sort verts by inc. y and inc. x
    pv[0] = &vertexList[0];
    pv[1] = &vertexList[1];
    pv[2] = &vertexList[2];
    for (i=0; i<2; i++)
     for (j=i+1; j<3; j++)
     {
      if ((*pv[i])[1]>(*pv[j])[1] || 
       (*pv[i])[1]==(*pv[j])[1] && (*pv[i])[0]>(*pv[j])[0]) {
       Coord *tmp;
       tmp = pv[i];
       pv[i] = pv[j];
       pv[j] = tmp;
      }
     }
    ;

    // all y the same?
    if ((*pv[0])[1] == (*pv[2])[1]) {
     drawHorizonLine(render, *pv[0], *pv[2]);
     return SUCCESS;  
    }

    // assign middle point
    Coord mid; 
    mid[1] = (*pv[1])[1]; // y
    float ratio = ((*pv[1])[1] - (*pv[0])[1]) / ((*pv[2])[1] - (*pv[0])[1]);
    mid[0] = (*pv[0])[0] + ratio * ((*pv[2])[0] - (*pv[0])[0]); // x
    mid[2] = (*pv[0])[2] + ratio * ((*pv[2])[2] - (*pv[0])[2]); // z

    if (mid[0]<=(*pv[1])[0]) {  // compare X
     drawTrapzoid(render, *pv[0], mid, *pv[0], *pv[1]); // upper tri
     drawTrapzoid(render, mid, *pv[2], *pv[1], *pv[2]); // lower tri
    }else{
     drawTrapzoid(render, *pv[0], *pv[1], *pv[0], mid); // upper tri
     drawTrapzoid(render, *pv[1], *pv[2], mid, *pv[2]); // lower tri
    }

    return SUCCESS;
}

I dont want two functions here. I want to copy the putTrianglePosition funtion into the Triangle function. I tried doing that, but i got alot of error. Probably i am not very clear with my c++.

Can some one help me how to do this.

Regards Zee

+3  A: 

If you just change the line

            return putTrianglePosition(render, (Coord *)valueList[i]);

into:

Coord* vertexList = (Coord*) valueList[i];

followed by the whole body of what's now putTrianglePosition from the opening { to the closing } included, I believe it should just work. If not, please edit your question to add the exact, complete, code as obtained by this edit and the exact, complete error messages you get.

Alex Martelli
+7  A: 

You shouldn't put functions together, you should split them apart. Put a new function wherever you can name them -- try to make them as small as you can. If you want a function that does all of that stuff, have a function that calls the other functions.

int foobar() {

    int a;
    int b;

    /* do a whole bunch of stuff with a */

    /* do a whole bunch of stuff with b */

    return  a + b;

}

this is sort of what you're trying to do. Instead, do this:

int foo(){

    int a;

    /* do a bunch of stuff with a */

    return a;

}

int bar() {

    int b;

    /* do a bunch of stuff with b */

    return b;

}

int foobar() {

    return foo() + bar();

}

The result will be cleaner, easier to maintain and re-usable.

Carson Myers
+1 as clean code is one of the most important things in software development (and one of the things that is often ignored)
Tobias Langner
+1  A: 

I strongly recommend you to go with Functions because it allows better separation of logic and allows you to reuse the logic. But still in case if you want to use it that way please check the function below :

 int Triangle(Render *render, int numParts, Token *nameList, Pointer *valueList) 
        {
            int iOuter;
            for (iOuter=0; iOuter<numParts; iOuter++)
            {
                switch (nameList[iOuter])
                {
                case GZ_NULL_TOKEN:
                        break;

                case GZ_POSITION:
                        {

                            Coord* vertexList = (Coord*) valueList[i];
                             Coord *pv[3];
                            int i,j;

                            // sort verts by inc. y and inc. x
                            pv[0] = &vertexList[0];
                            pv[1] = &vertexList[1];
                            pv[2] = &vertexList[2];
                            for (i=0; i<2; i++)
                                for (j=i+1; j<3; j++)
                                {
                                        if ((*pv[i])[1]>(*pv[j])[1] || 
                                                (*pv[i])[1]==(*pv[j])[1] && (*pv[i])[0]>(*pv[j])[0]) {
                                                Coord *tmp;
                                                tmp = pv[i];
                                                pv[i] = pv[j];
                                                pv[j] = tmp;
                                        }
                                }
                            ;

                            // all y the same?
                            if ((*pv[0])[1] == (*pv[2])[1]) {
                                drawHorizonLine(render, *pv[0], *pv[2]);
                                return SUCCESS;  
                            }

                            // assign middle point
                            Coord mid; 
                            mid[1] = (*pv[1])[1];       // y
                            float ratio = ((*pv[1])[1] - (*pv[0])[1]) / ((*pv[2])[1] - (*pv[0])[1]);
                            mid[0] = (*pv[0])[0] + ratio * ((*pv[2])[0] - (*pv[0])[0]); // x
                            mid[2] = (*pv[0])[2] + ratio * ((*pv[2])[2] - (*pv[0])[2]); // z

                            if (mid[0]<=(*pv[1])[0]) {  // compare X
                                drawTrapzoid(render, *pv[0], mid, *pv[0], *pv[1]); // upper tri
                                drawTrapzoid(render, mid, *pv[2], *pv[1], *pv[2]); // lower tri
                            }else{
                                drawTrapzoid(render, *pv[0], *pv[1], *pv[0], mid); // upper tri
                                drawTrapzoid(render, *pv[1], *pv[2], mid, *pv[2]); // lower tri
                            }

                            return SUCCESS;

                        }

                        break;
                }
            }

            return SUCCESS;
        }
Mahin
A: 

Well, since the tag says C++ (even though the code seems to be pure C), the solution would be to put an inline modifier before the function:

inline int putTrianglePosition(Render *render, Coord vertexList[3])
{
  ...
}

However, even after thinking about this for ten minutes, I still fail a valid reason for wanting this.

sbi