各位大佬,我想问一下,c#里面怎么画一个球做圆周运动?
private void AroundCircle()
{
// 确定圆心坐标,半径,和初始角度
int x0 = this.Width / 2;
int y0 = this.Height / 2;
int r = 40;
int a = 0;
while (true)
{
a += 1; // 角度变换
if (button1.Text == "开始")
{
return;
}
float x = (float)(x0 + r * Math.Cos(a * Math.PI / 180)); // 根据圆心,角度,半径,计算目标圆的圆心位置
float y = (float)(y0 + r * Math.Sin(a * Math.PI / 180));
this.Invoke(new MethodInvoker(delegate
{
int w = 20;
int h = w;
Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Red, 2);
g.DrawEllipse(p, x, y, w, h); // 画出目标圆
g.DrawEllipse(p, x0, y0, w, h); // 画出圆心圆
Thread.Sleep(100); // 等待100毫秒
g.Clear(Color.White); // 清除画布
}));
}
}
```
你俩可以交流沟通一下,说不定能碰撞出什么思路,https://ask.csdn.net/questions/7465512?spm=1005.2025.3001.5141
很简单的,用GDI的接口自己算算坐标画一画就ok了,粗糙的帮你划了一下,具体看下面。
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
namespace WindowsFormsApp3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.DoubleBuffered = true;
this.Load += Form2_Load;
}
CircularMotion cm = new CircularMotion();
Timer timer = new Timer();
Stopwatch stopwatch = new Stopwatch();
int count = 0;
private void Form2_Load(object sender, EventArgs e)
{
timer.Tick += Timer_Tick;
timer.Interval = 10;
cm.CenterPoint = new Point(200, 200);
cm.Radium = 150;
cm.W = 0.0001;
timer.Start();
stopwatch.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
cm.TimeSpan = stopwatch.ElapsedMilliseconds;
this.Invalidate();
count++;
}
protected override void OnPaint(PaintEventArgs e)
{
cm.Draw(e.Graphics);
base.OnPaint(e);
}
}
public class CircularMotion
{
// 角速度
public double W { get; set; }
// 圆周运动半径
public double Radium { get; set; }
// 中心坐标
public Point CenterPoint { get; set; }
// 经过的时间 ms
public double TimeSpan { get; set; }
public int MessPointX { get; private set; }
public int MessPointY{ get; private set; }
// 画质点
private void DrawMassPoint(Graphics e)
{
// 转过的弧度
double radian = W * TimeSpan;
// 把弧度换算成角度
double angle = 180 * radian / Math.PI;
// 计算质点实时坐标
MessPointX = (int)(CenterPoint.X + Math.Cos(angle) * Radium);
MessPointY = (int)(CenterPoint.Y + Math.Sin(angle) * Radium);
// 画质点
int r = 5;
int rLeft = (int)(MessPointX - r);
int rTop = (int)(MessPointY - r);
e.FillEllipse(new SolidBrush(Color.Red), new Rectangle(rLeft, rTop, (int)(2 * r), (int)(2 * r)));
}
// 画质点
private void DrawPath(Graphics e)
{
int rLeft = (int)(CenterPoint.X - Radium);
int rTop = (int)(CenterPoint.Y - Radium);
e.DrawEllipse(new Pen(Color.Red, 1f), new Rectangle(rLeft, rTop, (int)(2 * Radium), (int)(2 * Radium)));
}
// 画中心点
private void DrawCenterPoint(Graphics e)
{
int r = 3;
int rLeft = (int)(CenterPoint.X - r);
int rTop = (int)(CenterPoint.Y - r);
e.DrawEllipse(new Pen(Color.Red, 1f), new Rectangle(rLeft, rTop, (int)(2 * r), (int)(2 * r)));
}
// 画质点
private void DrawChainLine(Graphics e)
{
e.DrawLine(new Pen(Color.Red, 1f),CenterPoint.X, CenterPoint.Y, MessPointX, MessPointY);
}
public void Draw(Graphics e)
{
DrawCenterPoint(e);
DrawPath(e);
DrawMassPoint(e);
DrawChainLine(e);
}
}
}