编辑:谢谢大家,伙计们。我很感激你。
我正在开发一个在两个值之间绘制函数的程序。这是代码:
public partial class Form2 : Form
{
public class E
{
public static double A;
public static double B;
public static double C;
public static int s1;
public static int s2;
public static int K=(s2 - s1) * 18;
}
public Form2(double a, double b, double c, double s1, double s2)
{
InitializeComponent();
E.A = a;
E.B = b;
E.C = c * 1.3333;
E.s1 = Convert.ToInt32(s1);
E.s2 = Convert.ToInt32(s2);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
Calc();
}
public PointF[] p = new PointF[E.K]; //Value in E.K isn't applied here :(
private void Calc()
{
for (int x = 18 * E.s1; x < 18 * E.s2; x++)
{
double res = (E.A * Math.Pow(x, E.B) + E.C);
p[x - 18 * E.s1] = new PointF(x, (float)res);
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
float Y = (float)E.C;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.TranslateTransform(203, 203 + 14 * (-Y));
if (E.B == 0 || E.B == 1)
{
e.Graphics.ScaleTransform(1, -1F);
}
else
{
e.Graphics.ScaleTransform(1F, -0.05F);
}
e.Graphics.DrawLines(Pens.Blue, p);
}
我做了一些分析,发现当E.K
进入PointF
函数内部时,它变成了0,因此程序给出了IndexOutOfRangeException
. 你们有什么建议或替代想法吗?