C# 斐波那契盒

C# 斐波那契盒,c#,winforms,fibonacci,C#,Winforms,Fibonacci,我知道在堆栈溢出和web上有很多关于Fibonacci的问题和答案,但这是一个困扰我一段时间的问题,我似乎无法破解它或找到解决方案 创建斐波那契算法非常简单,有很多算法,但我正在尝试使用C#图形化地以螺旋形式创建长方体。这不是为了大学或其他什么,这只是一个我花了太多时间在上面的问题,我现在需要找到一个解决方案,如果你知道我的意思的话 到目前为止,我得到的是一个更好的配置,但是经过无数个小时的修改,现在我得到的是: public partial class Form1 : Form { p

我知道在堆栈溢出和web上有很多关于Fibonacci的问题和答案,但这是一个困扰我一段时间的问题,我似乎无法破解它或找到解决方案

创建斐波那契算法非常简单,有很多算法,但我正在尝试使用C#图形化地以螺旋形式创建长方体。这不是为了大学或其他什么,这只是一个我花了太多时间在上面的问题,我现在需要找到一个解决方案,如果你知道我的意思的话

到目前为止,我得到的是一个更好的配置,但是经过无数个小时的修改,现在我得到的是:

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();
    }
公共部分类表单1:表单
{
公共常数int FIBNUM=6;
公共建筑中心=10;
公共const int SIZE=10;
公共const int OFFSET=100;
公共表格1()
{
初始化组件();
drawSpiral();
}
私有整数fib(整数n)
{
开关(n)
{
案例0:
返回0;
案例1:
返回1;
违约:
返回fib(n-1)+fib(n-2);
}
}
私有空间
{
if(pictureBox1.Image==null)
{
pictureBox1.Image=新位图(pictureBox1.Width,pictureBox1.Height);
}
使用(Graphics g=Graphics.FromImage(pictureBox1.Image))
{
矩形r=新矩形(0,0,0,0);
int fibnum=0;
int中心=0;
int size=0;
int-cnt=0;

对于(int n=1;n),我认为你的代码太复杂了,因为你试图同时做很多事情。考虑下面的代码,其中螺旋是围绕原点绘制的,没有缩放和转换:

// 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;
}
输出:

private int fib(int n)
{
    switch (n)
    {
        case 0:
            return 0;
        case 1:
            return 1;
        default:
            return fib(n - 1) + fib(n - 2);
    }
}

这已经足够慢了,也许在这个任务中并不重要,但是使用数组
fib[1..n]
fib[0]=0
fib[1]=1
来表示(i=2;i给你:

public partial class Form1 : Form
{
    public const int FIBNUM = 8;
    public const int CENTERX = 300;
    public const int CENTERY = 300;
    public const int ZOOM = 10;

    public Form1()
    {
        InitializeComponent();
        drawSpiral();
    }

    private int fib(int n, int p = 0, int q = 1)
    {
        switch (n)
        {
            case 0: return 0;
            case 1: return q;
            default: return fib(n - 1, q, p + q);
        }
    }

    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 x = CENTERX;
            int y = CENTERY;

            for (int n = 1; n <= FIBNUM; n++)
            {
                int fibnum = fib(n)*ZOOM;

                r = new Rectangle(x, y, fibnum, fibnum);
                g.DrawRectangle(Pens.Red, r);

                switch (n % 4)
                {
                    case 0:
                        {
                            y += fibnum;
                            break;
                        }
                    case 1:
                        {
                            x += fibnum;
                            y -= fib(n - 1) * ZOOM;
                            break;
                        }
                    case 2:
                        {
                            x -= fib(n - 1)*ZOOM;
                            y -= fib(n + 1)*ZOOM;
                            break;
                        }
                    case 3:
                        {
                            x -= fib(n + 1) * ZOOM;
                            break;
                        }
                }
            }
            pictureBox1.Invalidate();
        }
    }
}
公共部分类表单1:表单
{
公共常数int FIBNUM=8;
公共常数int CENTERX=300;
中心公共常数=300;
公共常数int ZOOM=10;
公共表格1()
{
初始化组件();
drawSpiral();
}
私有整数fib(整数n,整数p=0,整数q=1)
{
开关(n)
{
案例0:返回0;
案例1:返回q;
默认值:返回fib(n-1,q,p+q);
}
}
私有空间
{
if(pictureBox1.Image==null)
{
pictureBox1.Image=新位图(pictureBox1.Width,pictureBox1.Height);
}
使用(Graphics g=Graphics.FromImage(pictureBox1.Image))
{
矩形r=新矩形(0,0,0,0);
int x=中心x;
int y=中心;

对于(int n=1;n 1.你的实际问题是什么?2.如果这是一个UI问题,你能给我们一些我们可以运行(没有UI)的代码吗?你在绘图部分有问题吗?将框链接在一起?谢谢,经过一些调整和更改,我想我现在已经完全明白我想要的了;)@你说得对。我现在将
right
初始化为
1
,输出正确,此外,我添加了一些缩放以创建更好的图片(请参见打印屏幕)太好了!我重新调整了一下,但这正是我想要达到的。我不确定我会尝试把金色螺旋放进盒子里,也许太难了,哈哈
public partial class Form1 : Form
{
    public const int FIBNUM = 8;
    public const int CENTERX = 300;
    public const int CENTERY = 300;
    public const int ZOOM = 10;

    public Form1()
    {
        InitializeComponent();
        drawSpiral();
    }

    private int fib(int n, int p = 0, int q = 1)
    {
        switch (n)
        {
            case 0: return 0;
            case 1: return q;
            default: return fib(n - 1, q, p + q);
        }
    }

    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 x = CENTERX;
            int y = CENTERY;

            for (int n = 1; n <= FIBNUM; n++)
            {
                int fibnum = fib(n)*ZOOM;

                r = new Rectangle(x, y, fibnum, fibnum);
                g.DrawRectangle(Pens.Red, r);

                switch (n % 4)
                {
                    case 0:
                        {
                            y += fibnum;
                            break;
                        }
                    case 1:
                        {
                            x += fibnum;
                            y -= fib(n - 1) * ZOOM;
                            break;
                        }
                    case 2:
                        {
                            x -= fib(n - 1)*ZOOM;
                            y -= fib(n + 1)*ZOOM;
                            break;
                        }
                    case 3:
                        {
                            x -= fib(n + 1) * ZOOM;
                            break;
                        }
                }
            }
            pictureBox1.Invalidate();
        }
    }
}
    private int move(int n, int a, int currentFib)
    {
        switch (a)
        {
            case 1: return currentFib;
            case 2: return -fib(n - 1) * ZOOM;
            case 3: return -fib(n + 1) * ZOOM;
            default: return 0;
        }
    }

    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 x = CENTERX;
            int y = CENTERY;

            for (int n = 1; n <= FIBNUM; n++)
            {
                int fibnum = fib(n)*ZOOM;

                r = new Rectangle(x, y, fibnum, fibnum);
                g.DrawRectangle(Pens.Red, r);

                x += move(n, n % 4, fibnum);
                y += move(n, (n + 1) % 4, fibnum);
            }
            pictureBox1.Invalidate();
        }
    }