我想在鼠标拖动时画一个平滑的圆圈。即在第一次鼠标单击时,设置起点,在拖动鼠标时,终点会更新,圆圈会扩大。我在某处读到可以使用线条进行绘制。但这不符合我的要求。
简而言之,我需要使用opengl使用给定的两个点绘制圆的逻辑。
好吧,如果你有:
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();
}
输出: