C# 无法使用图形c创建函数char#
我试图用图形以一定的间隔绘制函数y=x^2,但我做不到。当我写start+=1/10(增加点数)时,去掉了索引超出数组的错误,在这种情况下,图形看起来不像它应该的那样。也许有人能帮我用图形绘制这个图表。 附言:是的,我知道我可以使用图表,但任务是通过图形来完成C# 无法使用图形c创建函数char#,c#,winforms,charts,system.drawing,C#,Winforms,Charts,System.drawing,我试图用图形以一定的间隔绘制函数y=x^2,但我做不到。当我写start+=1/10(增加点数)时,去掉了索引超出数组的错误,在这种情况下,图形看起来不像它应该的那样。也许有人能帮我用图形绘制这个图表。 附言:是的,我知道我可以使用图表,但任务是通过图形来完成 using System.Drawing; using System.Windows.Forms; namespace FormsForProgrammin { public partial class Form15 : Form
using System.Drawing;
using System.Windows.Forms;
namespace FormsForProgrammin
{
public partial class Form15 : Form
{
PointF[] p = new PointF[10];
int count = 0;
public Form15()
{
InitializeComponent();
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form15_Paint);
Calc();
}
private void Calc()
{
float start = float.Parse(textBox1.Text);
while(start<= float.Parse(textBox2.Text))
{
float res = start * start;
p[count] = new PointF(start, res);
count += 1;
start += 1;
}
}
private void Form15_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.TranslateTransform(150, 150);
e.Graphics.ScaleTransform(1, 0.25F);
e.Graphics.DrawLines(Pens.Blue, p);
}
}
}
使用系统图;
使用System.Windows.Forms;
命名空间FormsForProgrammin
{
公共部分类表格15:表格
{
PointF[]p=新的PointF[10];
整数计数=0;
表格15(
{
初始化组件();
this.Paint+=new System.Windows.Forms.PaintEventHandler(this.Form15_-Paint);
计算();
}
私有无效计算()
{
float start=float.Parse(textBox1.Text);
而(start您有一个10个点的数组,并且您正在使用count变量对它们进行索引。该代码中有几个问题:
你从不重置计数
将“开始”设置为textBox1值,并将其递增1,直到其超过textBox2值。但此间距可能大于数组中的点数
要求解1,您可以在循环完成后重置计数,也可以在Calc方法本身中定义计数(后者是更好的选择,因为计数仅与Calc方法相关)
要求解2,需要计算步长:值之间的差值除以点数,
使用此步数确保循环的运行次数不超过您拥有的点数不要使用数组,请使用列表
。当您使用新值调用Calc()
时(将来自文本框控件的值传递给该方法,该控件已使用float.TryParse()
进行验证),您可以重置列表。或者,处理多个列表
,以添加到例如词典
,因此您可以选择仅重置词典中的一个列表,或全部列表,或添加新列表。