c#如何使多个面板控件一起沿圆形轨迹运动
答案:2 悬赏:80 手机版
解决时间 2021-03-21 22:58
- 提问者网友:活着好累
- 2021-03-20 23:55
要代码。。
最佳答案
- 五星知识达人网友:大漠
- 2021-03-21 00:43
用三角函数吧
在 winform 上放一个按钮叫做 btn,然后就可以了。。。
考虑到 GDI+ 的绘图效率,可能会稍微有一点卡顿,开了双缓存也没用
public partial class Form1 : Form
{
private System.Threading.Timer animation;
private delegate void ohmygod();
private ohmygod updating;
private readonly int radius = 100; // 轨迹半径
private double angle = 0;
private double speed = 2; // 速度
private int[] initpos = { 200, 200 }; // 初始坐标
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
updating += () => {
btn.Location = new Point(initpos[0] + (int)(radius * Math.Sin(angle * Math.PI / 180)), initpos[1] + -(int)(radius * Math.Cos(angle * Math.PI / 180)));
angle += speed; // 顺时针旋转用 +=,逆时针用 -=
if(angle >= 360)
angle = 0;
};
this.animation = new System.Threading.Timer((_) => this.Invoke(updating), null, 0, 10);
}
}
在 winform 上放一个按钮叫做 btn,然后就可以了。。。
考虑到 GDI+ 的绘图效率,可能会稍微有一点卡顿,开了双缓存也没用
public partial class Form1 : Form
{
private System.Threading.Timer animation;
private delegate void ohmygod();
private ohmygod updating;
private readonly int radius = 100; // 轨迹半径
private double angle = 0;
private double speed = 2; // 速度
private int[] initpos = { 200, 200 }; // 初始坐标
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
updating += () => {
btn.Location = new Point(initpos[0] + (int)(radius * Math.Sin(angle * Math.PI / 180)), initpos[1] + -(int)(radius * Math.Cos(angle * Math.PI / 180)));
angle += speed; // 顺时针旋转用 +=,逆时针用 -=
if(angle >= 360)
angle = 0;
};
this.animation = new System.Threading.Timer((_) => this.Invoke(updating), null, 0, 10);
}
}
全部回答
- 1楼网友:执傲
- 2021-03-21 02:01
x = r*cos(a);
y = r*sin(a);
a = 0~360
r = 半径
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯