Половина моего эллипса нарисована не в том месте

Вот код для овального метода рисования, над которым я работаю. Я применяю метод Брезенхема для построения координат и использую симметричные свойства эллипса для рисования одного и того же пикселя в четырех разных местах.

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);
}*/
}

Здесь проблема. При тестировании матрицы поворота на моей овальной функции рисования, я ожидаю, что она нарисует эллипс под уклоном от его исходного горизонтального положения, обозначенного как «угол». Вместо этого это делает форму сердца. Это мило, но не тот результат, которого я хочу.

Результат

Мне удалось заставить другой алгоритм (как видно в нижней части этого примера кода) работать успешно, но это занимает больше времени для вычислений и не так хорошо рисует линии. Я планирую использовать это, только если я не смогу заставить работать этот Брезенхем.

Кто-нибудь может помочь?

1

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector