我知道在堆栈溢出和整个网络上都有很多斐波那契问题和答案,但这是一个困扰我一段时间的问题,我似乎无法破解或找到解决方案。
创建斐波那契算法很容易,有很多方法,但是我正在尝试使用C#以图形形式以螺旋形式创建框。这不适合Uni或其他任何东西,这只是一个问题,我花了太多时间在上面,现在我需要找到解决方案,如果您知道我的意思呢?
这是我到目前为止所拥有的,现在我确实有一个更好的配置,但是由于无数小时地修改了代码,这就是我目前所拥有的:
public partial class Form1 : Form
{
public const int FIBNUM = 6;
public const int CENTRE = 10;
public const int SIZE = 10;
public const int OFFSET = 100;
public Form1()
{
InitializeComponent();
drawSpiral();
}
private int fib(int n)
{
switch (n)
{
case 0:
return 0;
case 1:
return 1;
default:
return fib(n - 1) + fib(n - 2);
}
}
private void drawSpiral()
{
if (pictureBox1.Image == null)
{
pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
Rectangle r = new Rectangle(0, 0, 0, 0);
int fibnum = 0;
int centre = 0;
int size = 0;
int cnt = 0;
for (int n = 1; n <= FIBNUM; n++)
{
fibnum = fib(n);
centre = fibnum * CENTRE;
size = fibnum * SIZE;
++cnt;
if (cnt == 1)
{
if (n == 1)
{
r = new Rectangle(fibnum + OFFSET, fibnum + OFFSET, size, size);
g.DrawRectangle(Pens.Red, r);
r = new Rectangle((fibnum + size) + OFFSET, fibnum + OFFSET, size, size);
g.DrawRectangle(Pens.Purple, r);
n++;
}
else
{
r = new Rectangle((centre - size) + OFFSET, (centre - size) + OFFSET, size, size);
g.DrawRectangle(Pens.Black, r);
}
continue;
}
if(cnt == 2)
{
r = new Rectangle((fibnum) + OFFSET, (fibnum - size) + OFFSET, size, size);
g.DrawRectangle(Pens.Blue, r);
continue;
}
if (cnt == 3)
{
r = new Rectangle((fibnum - size) + OFFSET, (fibnum - size) + OFFSET, size, size);
g.DrawRectangle(Pens.Green, r);
continue;
}
if (cnt == 4)
{
r = new Rectangle((fibnum - size / 2) + OFFSET, (fibnum - size) + OFFSET, size, size);
g.DrawRectangle(Pens.Gray, r);
}
cnt = 0;
}
}
pictureBox1.Invalidate();
}
我从维基百科拍摄了一张我想以图形方式创建的图像:
提前致谢。
我认为您的代码太复杂了,因为您尝试一次执行很多操作。考虑以下代码,其中围绕原点绘制了螺旋,而没有缩放和平移:
// the current fibonacci numbers
int current = 1;
int previous = 0;
// the current bounding box
int left = 0;
int right = 1;
int top = 0;
int bottom = 0;
// the number of boxes you want to draw
const int N = 10;
for (int i = 0; i < N; i++) {
switch (i % 4) {
case 0: // attach to bottom of current rectangle
drawRectangle(g, left, right, bottom, bottom + current);
bottom += current;
break;
case 1: // attach to right of current rectangle
drawRectangle(g, right, right + current, top, bottom);
right += current;
break;
case 2: // attach to top of current rectangle
drawRectangle(g, left, right, top - current, top);
top -= current;
break;
case 3: // attach to left of current rectangle
drawRectangle(g, left - current, left, top, bottom);
left -= current;
break;
}
// update fibonacci number
int temp = current;
current += previous;
previous = temp;
}
然后,您可以使用单独的方法处理实际的绘图部分drawRectangle
(我省略了有关实际图形对象的所有详细信息,但您可以自己完成)。
const int SCALE = 5;
const int OFFSET = 150;
private void drawRectangle(Graphics g, int left, int right, int top, int bottom)
{
g.DrawRectangle(Pens.Red, new Rectangle(SCALE * left + OFFSET,
SCALE * top + OFFSET,
SCALE * (right - left),
SCALE * (bottom - top)));
}
输出:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句