0

输入的错误表示 输入的良好表示

你好,

我需要帮助。

我必须绘制一个图形(更准确地说是一个计算素数数量的函数:f(givenNumber)=素数的数量<=givenNumber,n>0)。

为了做到这一点,我使用了一个矩形(这是绘制图形)、图形、钢笔和尺寸(用于客户空间)。

我的大问题如下:

- 如果我输入 10-100,则图形正确绘制到矩形中而不会退出。然而,如果我输入 1000,图形会“无限”地脱离矩形。

我如何缩放,使用什么,这样无论我有多少点,图形都会保持它的限制? 如果有帮助,代码将是:

// get the graphics object and create our pen for drawing.
object[] args;
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Blue);
// figure out the client space
Size sz = this.ClientSize;
// Determine the rectangle that we will use to draw
int offsetHeight = this.textBox1Limita.Location.Y + this.textBox1Limita.Height + 20;
int offsetWidth = 40;
int maxWidth = sz.Width - offsetWidth * 2;
int maxHeight = sz.Height - offsetHeight * 2;
Rectangle drawingSpace = new Rectangle(offsetWidth, offsetHeight, maxWidth, maxHeight);
// Draw the begining rectangle
g.DrawRectangle(pen, offsetWidth, offsetHeight, maxWidth, maxHeight);
// at this point, only continue if the user pressed enter in one of the two input boxes.
if (!ValuesEntered) return;
try
{
   args = new object[1];
   args[0] = int.Parse(this.textBox1Limita.Text);
}
catch (Exception)
{
   // We reached here, the user input some bogus values into the box.
   // Just return so we dont do anything with those values.
   return;
}

int lim=(int)mi.Invoke(o, args);
int step =5;
int k = 1;

g.DrawLine(pen, drawingSpace.X + 1 * step, offsetWidth + maxHeight - 1 * step, drawingSpace.X + 2 * step, offsetWidth + maxHeight - 1 * step);

for (int j = 0; j <lim; j++)
{
   if (j == lim-1)
   {
      g.DrawLine(pen, drawingSpace.X + vectorPrime[j] * step, offsetWidth + maxHeight - k * step, drawingSpace.X + (int)args[0] * step, offsetWidth + maxHeight - k * step);
   }
   else
  {
      g.DrawLine(pen, drawingSpace.X + vectorPrime[j] * step, offsetWidth + maxHeight - k * step, drawingSpace.X + (vectorPrime[j + 1]-1) * step, offsetWidth + maxHeight - k * step);

      g.DrawLine(pen, drawingSpace.X + (vectorPrime[j + 1]-1) * step, offsetWidth + maxHeight - k * step, drawingSpace.X + vectorPrime[j+1]*step, offsetWidth + maxHeight - (k+1)*step);
      k++;
  }
}

哦,还有别的东西,我用 Graphics.FillEllipse 找到了一些东西......这会是一个解决方案吗?

谢谢你。

4

0 回答 0