C++ 我的一半椭圆画错了地方

C++ 我的一半椭圆画错了地方,c++,opengl,graphics,ellipse,bresenham,C++,Opengl,Graphics,Ellipse,Bresenham,下面是我正在研究的椭圆形绘图方法的代码。我使用Bresenham方法绘制坐标,并利用椭圆的对称特性在四个不同的位置绘制相同的像素 void cRenderClass::plotEllipse(int xCentre, int yCentre, int width, int height, float angle, float xScale, float yScale) { if ((height == width) && (abs(xScale - yScale) <

下面是我正在研究的椭圆形绘图方法的代码。我使用Bresenham方法绘制坐标,并利用椭圆的对称特性在四个不同的位置绘制相同的像素

void cRenderClass::plotEllipse(int xCentre, int yCentre, int width, int height, float angle, float xScale, float yScale)
{
    if ((height == width) && (abs(xScale - yScale) < 0.005))
        plotCircle(xCentre, yCentre, width, xScale);

    std::vector<std::vector <float>> rotate;
    if (angle > 360.0f)
    {
        angle -= 180.0f;
    }
    rotate = maths.rotateMatrix(angle, 'z');

    //rotate[0][0] = cos(angle)
    //rotate[0][1] = sin(angle)

    float theta = atan2(-height*rotate[0][1], width*rotate[0][0]);
    if (angle > 90.0f && angle < 180.0f)
    {
        theta += PI;
    }

    //add scalation in at a later date

    float xShear = (width * (cos(theta) * rotate[0][0])) - (height * (sin(theta) * rotate[0][1]));
    float yShear = (width * (cos(theta) * rotate[0][1])) + (height * (sin(theta) * rotate[0][0]));
    float widthAxis = abs(sqrt(((rotate[0][0] * width) * (rotate[0][0] * width)) + ((rotate[0][1] * height) * (rotate[0][1] * height))));
    float heightAxis = (width * height) / widthAxis;

    int aSquared = widthAxis * widthAxis;
    int fourASquared = 4*aSquared;
    int bSquared = heightAxis * heightAxis;
    int fourBSquared = 4*bSquared;

    x0 = 0;
    y0 = heightAxis;
    int sigma = (bSquared * 2) + (aSquared * (1 - (2 * heightAxis)));

    while ((bSquared * x0) <= (aSquared * y0))
    {
        drawPixel(xCentre + x0, yCentre + ((floor((x0 * yShear) / xShear)) + y0));
        drawPixel(xCentre - x0, yCentre + ((floor((x0 * yShear) / xShear)) + y0));
        drawPixel(xCentre + x0, yCentre + ((floor((x0 * yShear) / xShear)) - y0));
        drawPixel(xCentre - x0, yCentre + ((floor((x0 * yShear) / xShear)) - y0));

        if (sigma >= 0)
        {
            sigma += (fourASquared * (1 - y0));
            y0--;
        }

        sigma += (bSquared * ((4 * x0) + 6));
        x0++;
    }

    x0 = widthAxis;
    y0 = 0;
    sigma = (aSquared * 2) + (bSquared * (1 - (2 * widthAxis)));

    while ((aSquared * y0) <= (bSquared * x0))
    {
        drawPixel(xCentre + x0, yCentre + ((floor((x0 * yShear) / xShear)) + y0));
        drawPixel(xCentre - x0, yCentre + ((floor((x0 * yShear) / xShear)) + y0));
        drawPixel(xCentre + x0, yCentre + ((floor((x0 * yShear) / xShear)) - y0));
        drawPixel(xCentre - x0, yCentre + ((floor((x0 * yShear) / xShear)) - y0));

        if (sigma >= 0)
        {
            sigma += (fourBSquared * (1 - x0));
            x0--;
        }

        sigma += (aSquared * (4 * y0) + 6);
        y0++;
    }

    //the above algorithm hasn't been quite completed
    //there are still a few things I want to enquire Andy about
    //before I move on

    //this other algorithm definitely works
    //however
    //it is computationally expensive
    //and the line drawing isn't as refined as the first one
    //only use this as a last resort

/*  std::vector<std::vector <float>> rotate;
    rotate = maths.rotateMatrix(angle, 'z');

    float s = rotate[0][1];
    float c = rotate[0][0];
    float ratio = (float)height / (float)width;
    float px, py, xNew, yNew;

    for (int theta = 0; theta <= 360; theta++)
    {
        px = (xCentre + (cos(maths.degToRad(theta)) * (width / 2))) - xCentre;
        py = (yCentre - (ratio * (sin(maths.degToRad(theta)) * (width / 2)))) -     yCentre;

        x0 = (px * c) - (py * s);
        y0 = (px * s) + (py * c);

        drawPixel(x0 + xCentre, y0 + yCentre);
    }*/
}
void cRenderClass::plotEllipse(int xCentre、int yCentre、int width、int height、float angle、float xScale、float yScale)
{
如果((高度=宽度)和&(abs(xScale-yScale)<0.005))
绘图圆(X中心、Y中心、宽度、X刻度);
向量旋转;
如果(角度>360.0f)
{
角度-=180.0f;
}
旋转=数学旋转矩阵(角度“z”);
//旋转[0][0]=cos(角度)
//旋转[0][1]=sin(角度)
浮点θ=atan2(-高度*旋转[0][1],宽度*旋转[0][0]);
如果(角度>90.0f和角度<180.0f)
{
θ+=π;
}
//在以后的日期添加缩放
浮动x切变=(宽度*(cos(θ)*旋转[0][0])-(高度*(sin(θ)*旋转[0][1]);
浮动Y切变=(宽度*(cos(θ)*旋转[0][1])+(高度*(sin(θ)*旋转[0][0]);
浮动宽度轴=绝对值(sqrt((旋转[0][0]*宽度)*(旋转[0][0]*宽度))+((旋转[0][1]*高度)*(旋转[0][1]*高度));
浮动高度轴=(宽度*高度)/宽度轴;
int aSquared=宽度轴*宽度轴;
int-fourASquared=4*aSquared;
int bSquared=高度轴*高度轴;
int fourBSquared=4*bSquared;
x0=0;
y0=高度轴;
int-sigma=(b平方*2)+(a平方*(1-(2*高度轴));
而((b平方*x0)=0)
{
西格玛+=(四次方*(1-y0));
y0--;
}
西格玛+=(b平方*((4*x0)+6));
x0++;
}
x0=宽度轴;
y0=0;
西格玛=(平方*2)+(平方*(1-(2*宽度轴));
而((平方*y0)=0)
{
西格玛+=(四次方*(1-x0));
x0--;
}
西格玛+=(四次方*(4*y0)+6);
y0++;
}
//上述算法尚未完全完成
//还有几件事我想问安迪
//在我继续之前
//另一种算法肯定有效
//然而
//这在计算上很昂贵
//而且线条画也不像第一张那么精致
//这是最后的手段
/*向量旋转;
旋转=数学旋转矩阵(角度“z”);
浮动s=旋转[0][1];
浮动c=旋转[0][0];
浮动比率=(浮动)高度/(浮动)宽度;
浮动px、py、xNew、yNew;

对于(intθ=0;θ)来说,这正是一种可以从图片中受益的问题。@n.m.这是怎么回事?这正是一种可以从图片中受益的问题。@n.m.这是怎么回事?