帮助在c ++中组合两个函数 [英] Help in combining two functions in c++

查看:111
本文介绍了帮助在c ++中组合两个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是尝试别人的代码。

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

我有两个函数:

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;
}

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;
}

我不想在这里有两个函数。我要将 putTrianglePosition()函数复制到 Triangle()函数中。

I don't want two functions here. I want to copy the putTrianglePosition() function into the Triangle() function.

我试过这样做,但是我有很多错误。

I tried doing that, but I got a lot of errors.

有人可以告诉我怎么做?

Can somebody else show me how to do this?

推荐答案

如果只是更改行

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

into:

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

后面跟现在的整个 putTrianglePosition 从开头 {到结束} 包含,我相信它应该工作。如果没有,请修改您的问题,以添加通过此修改获得的确切,完整的代码,以及您获得的确切,完整的错误消息。

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.

这篇关于帮助在c ++中组合两个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆