在OpenGL中沿椭圆路径动画

问题描述 投票:0回答:1

我试图使用OpenGL中的DDA算法在半圆的路径上制作一个红色圆圈。我几乎拥有它,尽管圆圈在其X轴上稍微偏移,随着半圆的角度增加而增加。

enter image description here enter image description here

任何帮助将不胜感激!这是我的代码:

scrPt movecircle (scrPt p1, scrPt p2)
{
    scrPt circlePos;

    float angle, x = p1.x, y = p1.y, vectorX, vectorY;

    // Get tahe x distance between the two points
    int dx = p2.x - p1.x, steps;

    // Get the y distance between the two points
    int dy = p2.y - p1.y;

    // Get the length between the points
    float length = sqrt(dx*dx + dy*dy);

    if (fabs (dx) > fabs (dy)) 
    steps = fabs (dx); 
    else 
        steps = fabs (dy);

    // calculate the direction
    float xIncrement = float (dx) / float (steps); 
    float yIncrement = float (dy) / float (steps); 

    if (nextPos == 0)
    {
        for(int i = 0; i < steps; i++)
        {
            glClear(GL_COLOR_BUFFER_BIT);
            angle = PI * i / steps;
            vectorX = x + (length / 2) * cos(angle + theta);
            vectorY = y + dy / 2 + (length / 2) * sin(angle + theta);
            circlePos.x = round(vectorX - length / 2);
            circlePos.y = round(vectorY);
            drawCircle (circlePos.x, circlePos.y);
            drawArch();
            glFlush();
            usleep(3000);
        }
    }
    else
    {   
        for (int i = 0; i < steps; i++)
        {
            glClear(GL_COLOR_BUFFER_BIT);
            drawCircle (round(x),round(y));
            glFlush();
            usleep(3000);
            x += xIncrement; 
            y += yIncrement;
        }
    }

    return circlePos;
}
c++ animation opengl drawing
1个回答
0
投票

for循环中有一些错误导致了这个问题。我需要改变

这个:

vectorX = x + (length / 2) * cos(angle + theta);

对此:

vectorX = x + (dx / 2) + (length / 2) * cos(angle + theta);

还有这个:

circlePos.x = round(vectorX - (length / 2));

对此:

circlePos.x = round(vectorX);
© www.soinside.com 2019 - 2024. All rights reserved.