C# 在C中绘制半径递增的圆#
我正在使用下面的代码绘制半径越来越大的圆(从csv文件读取中心)。半径增加为每圈5个单位C# 在C中绘制半径递增的圆#,c#,C#,我正在使用下面的代码绘制半径越来越大的圆(从csv文件读取中心)。半径增加为每圈5个单位 namespace MATLAB_file { public partial class Form1 : Form { string[] read; float th; int c = 0; int r; public List<PointF> circleCoordinates = new List<PointF>();
namespace MATLAB_file
{
public partial class Form1 : Form
{
string[] read;
float th;
int c = 0;
int r;
public List<PointF> circleCoordinates = new List<PointF>();
int rl;
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
Pen linePen = new Pen(System.Drawing.Color.CornflowerBlue);
Graphics grphx = this.CreateGraphics();
grphx.Clear(this.BackColor);
foreach (PointF point in this.circleCoordinates)
{
Pen redPen1 = new Pen(Color.Red, 100);
e.Graphics.DrawArc(Pens.Red, point.X, point.Y, 1, 1, 0, 120F);
}
linePen.Dispose();
base.OnPaint(e);
}
private void Form1_Load(object sender, EventArgs e)
{
double xx, yy;
int i;
int n = 0;
float[] centre1 = new float[1000];
System.IO.StreamReader sr;
sr = new System.IO.StreamReader("centers.txt", true);
char[] seperators = { ',' };
string data = sr.ReadLine();
read = data.Split(new Char[] { ',' });
rl = read.Length;
int a1 = rl / 2;
for (c = 0; c < rl; c++)
{
centre1[c] = float.Parse(read[c]);
}
while (r < 200)
{
for (i = 0; i < a1; i++)
{
while (th < 360)
{
xx = r * Math.Cos(th) + centre1[2 * i] + 100;
xx1 = (float)xx;
yy = r * Math.Sin(th) + centre1[2 * i + 1] + 100;
yy1 = (float)yy;
this.circleCoordinates.Add(new PointF(xx1, yy1));
this.Invalidate();
th = th + .360F;
}
th = 0;
}
r = r + 5;
}
}
}
名称空间MATLAB\u文件
{
公共部分类Form1:Form
{
字符串[]读取;
浮动th;
int c=0;
INTR;
public List circleCoordinates=新列表();
int rl;
公共表格1()
{
初始化组件();
}
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
Pen linePen=新笔(System.Drawing.Color.CornflowerBlue);
Graphics grphx=this.CreateGraphics();
grphx.Clear(此背景色);
foreach(此循环坐标中的点F点)
{
Pen redPen1=新笔(颜色:红色,100);
e、 图形.绘图弧(红色笔,X点,Y点,1,1,0,120F);
}
linePen.Dispose();
基础漆(e);
}
私有void Form1\u加载(对象发送方、事件参数e)
{
双xx,yy;
int i;
int n=0;
浮动[]中心1=新浮动[1000];
System.IO.StreamReader sr;
sr=新System.IO.StreamReader(“centers.txt”,true);
char[]分隔符={',};
字符串数据=sr.ReadLine();
read=data.Split(新字符[]{',});
rl=读取长度;
int a1=rl/2;
对于(c=0;c
}
上面的代码显示了所有的圆,但我不希望所有的圆都显示在画布上,而是应该随着半径的逐渐增加只显示一个圆
请建议如何在绘制新圆时删除先前绘制的圆。如果我以后使用的方法包括基于“th”值删除某些部分的圆,还有其他方法吗?如果在
foreach
中移动Clear
调用,您将只看到最后绘制的圆,尽管这也可以通过仅绘制last
圆坐标来实现
foreach (PointF point in this.circleCoordinates)
{
grphx.Clear(this.BackColor);
Pen redPen1 = new Pen(Color.Red, 100);
e.Graphics.DrawArc(Pens.Red, point.X, point.Y, 1, 1, 0, 120F);
}
另一种解释是:
您需要动画,该动画生成onPaint
事件(通过计时器计时或用户输入),并增加计数器以选择要绘制的圆
为此,您需要在程序中添加一个新成员,如intindex代码>并且您可以基于此选择圆,使用以下代码片段(假设您始终至少有1个圆坐标
,动画在完成后重新启动):
或(动画后屏幕上保留最后一个圆圈)
仅供参考,Visual Studio是一种IDE,而不是一种编程语言。主要问题是,要观看动画,您需要一个计时器。否则,将不调用绘制事件,或者,与代码中一样,没有时间显示中间圆。使用CreateGraphics几乎总是错误的。这里也是。将圆/半径数据保存在类级别变量中,绘制绘图,在计时器中无效。勾选,一切正常..看起来他想要一个动画。你确实提到了计时器,但只是顺便提一下。我想你可以再指出一点它的重要性。因为它读起来好像移动clear会更接近动画,但事实并非如此。谢谢@TaW,我已经更新了一点答案,尽管我不确定他是不是这个意思,所以我也保留了第一部分。我想读一本关于在Visual studio中使用C#生成动画的不同方法的书。请推荐。谢谢@Gabor。我有几个基本问题,请澄清。现在,我正在使用paint event创建动画。你能建议使用图片框更好吗。此外,我还能画圆的动画。你能告诉我,使用定时器是否会改善所获得的结果,即增加半径的圆。另外,我使用的是Windows窗体,而不是WPF,因为我只知道C#。请建议,如果WPF是更好的动画目的或Windows窗体是好去。
PointF point = this.circleCoordinates[Index % circleCoordinates.Length];
PointF point = this.circleCoordinates[Math.Min(Index, circleCoordinates.Length - 1)];