这是我正在研究的椭圆绘图方法的代码 . 我正在应用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);
    }*/
}

这是问题所在 . 在我的椭圆绘图功能上测试旋转矩阵时,我希望它从原始水平位置倾斜绘制一个椭圆,如'angle'所示 . 相反,它使心脏形状 . 这很好,但不是我想要的结果 .

Result

我已经设法让其他算法(如该代码示例的底部所示)成功运行,但是它需要更多的时间来计算,并且不能很好地绘制线条 . 我只打算使用它,如果我不能让这个Bresenham工作 .

有人可以帮忙吗?