| |

VerySource

 Forgot password?
 Register
Search
View: 10|Reply: 3

How does this program work?

[Copy link]

2

Threads

2

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

Post time: 2020-3-23 02:00:01
| Show all posts |Read mode
Write a program to make a small red ball on the form move in a circle. Given the circular trajectory equation:
x = rsina
y = rcosa
r is the radius of the circle, a is the center angle
The following is a program I wrote, but this small circle can't be smooth!
Graphics g = this.CreateGraphics ();
Ranch
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);
}
Reply

Use magic Report

0

Threads

1

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-7-3 22:30:01
| Show all posts
This is too simple in WPF
Reply

Use magic Report

0

Threads

5

Posts

5

Credits

Newbie

Rank: 1

Credits
5

 China

Post time: 2020-7-4 21:15:01
| Show all posts
Pay attention, I also want to know how to draw a circle smoothly.
Reply

Use magic Report

0

Threads

5

Posts

5

Credits

Newbie

Rank: 1

Credits
5

 China

Post time: 2020-7-5 09:00:01
| Show all posts
The Sin() function requires radians, not angles!
Radian=Angle*3.14/180
Therefore, the landlord try the following code:
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 degrees is a full circle
{
float a=(float)Math.Sin(i*3.14/180);//radian=angle*3.14/180
float b=(float)Math.Cos(i*3.14/180);//Ibid
g.DrawEllipse(redpen,x+r*a,y+r*b,width,height);
System.Threading.Thread.Sleep(100);//Delay
g.Clear(this.BackColor);
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2020|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list