c# - Fibonacci Boxes -
i know there's lot of fibonacci questions , answers on stack overflow , web in general, problem that's been vexing me while now, , can't seem crack or find solution.
creating fibonacci algorithms easy enough, there's plenty of them, i'm trying create boxes in spiral formation graphically using c#. not uni or anything, it's problem i've spent way time on need find solution for, if know mean?
here's i've got far, did have better configuration, countless hours of revising code, have @ moment:
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; (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(); }
i've taken image wikipedia of i'm trying create graphically :
thanks in advance.
i think code complicated, because try lot @ once. consider following code spiral drawn around origin, without scaling , translating:
// current fibonacci numbers int current = 1; int previous = 0; // current bounding box int left = 0; int right = 1; int top = 0; int bottom = 0; // number of boxes want draw const int n = 10; (int = 0; < n; i++) { switch (i % 4) { case 0: // attach bottom of current rectangle drawrectangle(g, left, right, bottom, bottom + current); bottom += current; break; case 1: // attach right of current rectangle drawrectangle(g, right, right + current, top, bottom); right += current; break; case 2: // attach top of current rectangle drawrectangle(g, left, right, top - current, top); top -= current; break; case 3: // attach left of current rectangle drawrectangle(g, left - current, left, top, bottom); left -= current; break; } // update fibonacci number int temp = current; current += previous; previous = temp; }
you can deal actual drawing part in separate method drawrectangle
(i left out details actual graphics objects, can yourself).
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))); }
output:
Comments
Post a Comment