Help in combining two functions in c++

前端 未结 4 2015
野性不改
野性不改 2021-01-29 13:00

I am just trying something with somebody else\'s code.

I have two functions:

int Triangle(Render *render, int numParts, Token *nameList, Pointer *valueLi         


        
相关标签:
4条回答
  • 2021-01-29 13:09

    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;
            }
    
    0 讨论(0)
  • 2021-01-29 13:31

    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.

    0 讨论(0)
  • 2021-01-29 13:33

    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.

    0 讨论(0)
  • 2021-01-29 13:35

    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.

    0 讨论(0)
提交回复
热议问题