Opengl - 在鼠标拖动时绘制平滑圆圈

时间:2013-06-11 08:23:30

标签: c++ opengl mouse

我想在鼠标拖动上绘制一个平滑的圆圈。这是在第一次鼠标点击时,设置起点,在拖动鼠标时,终点得到更新,圆圈得到扩展。我在某处读过可以用线条画画。但这不符合我的要求。

简而言之,我需要逻辑使用opengl使用给定的两个点绘制圆圈。

1 个答案:

答案 0 :(得分:2)

好吧,如果你有:

top-left position (X1 Y1) 

bottom-right position (X2 Y2)

你也有

diameter (sqrt([X2-X1]^2+[Y2-Y1]^2))
radius (diameter/2)

和中心:

CenterX = X1 + (radius * (sin(-atan2(Y2-Y1,X2-X1))));
CenterY = Y1 + (radius * (cos(-atan2(Y2-Y1,X2-X1))));
从那时起,您可以以任何您喜欢的方式从中心以给定半径绘制一个圆圈!

double max = 2.0 * PI;
double precision = 0.1;
double current = 0.0;
struct point
{
    double x;
    double y;
};

while(current <= max)
{
    point one;
    point two;
    one.x = Center.x + (radius * (sin(-current)));
    one.y = Center.y + (radius * (cos(-current)));
    current += precision;
    two.x = Center.x + (radius * (sin(-current)));
    two.y = Center.y + (radius * (cos(-current)));

    //draw line between one and two?
    //draw here
}

(我不会进一步回答,因为线条不是你想要的?我不知道以其他方式“画”一个“圆圈”)

ByTheWay:通过正确修改此代码,您甚至可以绘制“伪圆圈”的部分内容(while(current<=max)

无论如何,这就是我在代码中绘制圆圈的方式:

//draw fun
{
    struct point
    {
        double x,y;
        point(double x,double y) : x(x), y(y) {}
        point(){ x = 0.0, y = 0.0; }
    };

    point start(100.0,100.0);
    point end(150.0,150.0);
    point center;

    double diameter = sqrt(pow(end.x-start.x,2.0)+pow(end.y-start.y,2.0));
    double radius = diameter/2.0;
    double max = 2.0 * PId;
    double precision = max/180.0;
    double current = 0.0;

    center.x = start.x + (radius * (sin(-atan2(end.y-start.y,end.x-start.x))));
    center.y = start.y + (radius * (cos(-atan2(end.y-start.y,end.x-start.x))));
    //render->BeginRender();
    while(current <= max)
    {
        point one;
        //point two;
        one.x = center.x + (radius * (sin(-current)));
        one.y = center.y + (radius * (cos(-current)));
        render->D3DBox((float)one.x,(float)one.y,1.0f,1.0f,0xFFFFFFFF);//create a dot
        current += precision;
        //two.x = center.x + (radius * (sin(-current)));
        //two.y = center.y + (radius * (cos(-current)));
        //
        //render->DrawLine(D3DXVECTOR3((float)one.x,(float)one.y,1.0),D3DXVECTOR3((float)two.x,(float)two.y,1.0),0xFFFFFFFF);
    }
    //render->EndRender();
}

输出:

enter image description here