编写一个程序,使得窗体上的一红色小球能按圆形运动,给定圆形轨迹方程为:
x=rsina
y=rcosa
r为圆的半径,a为圆心角
以下是我写的程序,可是这个小圆不能平滑的画园啊!
Graphics g=this.CreateGraphics();

Pen redpen=new Pen(Color.Red,2);
float r=100F;
    double f=0;
float x=100.0F;
float y=100.0F;
float width=20.0F;
float height=20.0F;
for(int i=0;i<360000;i++)
{
f+=45;
float a=(float)Math.Sin(f);
float b=(float)Math.Cos(f);
g.DrawEllipse(redpen,x+r*a,y+r*b,width,height);
g.Clear(this.BackColor);
}

解决方案 »

  1.   

    Sin()函数中要求的是弧度,不是角度!
    弧度=角度*3.14/180
    所以,楼主试试下面的代码:
    Graphics g=this.CreateGraphics();Pen redpen=new Pen(Color.Red,2);
    float r=100F;
    double f=0;
    float x=100.0F;
    float y=100.0F;
    float width=20.0F;
    float height=20.0F;
    for(int i=0;i<360;i++)  //360度为一个整圆
    {
    float a=(float)Math.Sin(i*3.14/180);//弧度=角度*3.14/180
    float b=(float)Math.Cos(i*3.14/180);//同上
    g.DrawEllipse(redpen,x+r*a,y+r*b,width,height);
    System.Threading.Thread.Sleep(100);//延时一下
    g.Clear(this.BackColor);
    }