Author Topic: Fractal Fern  (Read 6373 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Fractal Fern
« on: August 06, 2020, 12:53:27 am »
This might have been posted before. But I just found out how to make a fractal fern leaf. I'll post the URL below as well as the original code and also my own mod of it swaying in the wind. :)
Thanks to a friend of mine who found this code for me.

The code is from here: https://en.wikipedia.org/wiki/Barnsley_fern

Original (I added the URL):

Code: QB64: [Select]
  1. 'From: https://en.wikipedia.org/wiki/Barnsley_fern
  2. WINDOW (-5, 0)-(5, 10)
  3.         CASE IS < .01
  4.             nextX = 0
  5.             nextY = .16 * y
  6.         CASE .01 TO .08
  7.             nextX = .2 * x - .26 * y
  8.             nextY = .23 * x + .22 * y + 1.6
  9.         CASE .08 TO .15
  10.             nextX = -.15 * x + .28 * y
  11.             nextY = .26 * x + .24 * y + .44
  12.         CASE ELSE
  13.             nextX = .85 * x + .04 * y
  14.             nextY = -.04 * x + .85 * y + 1.6
  15.     END SELECT
  16.     x = nextX
  17.     y = nextY
  18.     PSET (x, y)
  19.  


Here is my own mod version of it swaying in the wind:

Code: QB64: [Select]
  1. 'Original Fern From: https://en.wikipedia.org/wiki/Barnsley_fern
  2. WINDOW (-5, 0)-(5, 10)
  3. x2 = 1
  4.     _LIMIT 10
  5.     FOR t = 1 TO 100000
  6.         SELECT CASE RND
  7.             CASE IS < .01
  8.                 nextX = 0
  9.                 nextY = .16 * y
  10.             CASE .01 TO .08
  11.                 nextX = .2 * x - .26 * y
  12.                 nextY = .23 * x + .22 * y + 1.6
  13.             CASE .08 TO .15
  14.                 nextX = -.15 * x + .28 * y
  15.                 nextY = .26 * x + .24 * y + .44
  16.             CASE ELSE
  17.                 nextX = .85 * x + .04 * y
  18.                 nextY = -.04 * x + .85 * y + 1.6
  19.         END SELECT
  20.         x = nextX
  21.         y = nextY
  22.         x = x + xx
  23.         PSET (x, y)
  24.     NEXT t
  25.     _DISPLAY
  26.     CLS
  27.     IF xx > .75 THEN x2 = 2
  28.     IF xx < -.75 THEN x2 = 1
  29.     IF x2 = 1 THEN xx = xx + .1
  30.     IF x2 = 2 THEN xx = xx - .1
  31.  
« Last Edit: August 06, 2020, 12:56:37 am by SierraKen »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Fractal Fern
« Reply #1 on: August 06, 2020, 01:00:43 am »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Fractal Fern
« Reply #2 on: August 06, 2020, 01:42:19 am »
The Fern I had seen before but the "swaying" version was a nice twist... Nice job as both draw very quickly....

Ah... The classic Mandelbrot... That was pretty quick too... Oh, you just have to love QB's palette of colours...

Can't go past Fractals and Brots.... Cool... ;)
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Fractal Fern
« Reply #3 on: August 06, 2020, 12:33:06 pm »
Yes nice twist:
Code: QB64: [Select]
  1. _TITLE "Classic Fern sway in wind mod, press spacebar for new landscape, esc to quit" 'b+ 2020-08-06
  2. SCREEN _NEWIMAGE(1000, 600, 32)
  3. _DELAY .25
  4. WINDOW (-5, 0)-(5, 11)
  5.  
  6. TYPE obj
  7.     x AS SINGLE 'offset from centered fern
  8.     y AS SINGLE 'offest from centered fern
  9.     scale AS SINGLE ' from 0 to 1
  10.     c AS _UNSIGNED LONG
  11.  
  12. DIM SHARED nFerns
  13. REDIM SHARED fern(1 TO nFerns) AS obj
  14.  
  15. initFerns
  16. wind = 0: dw = .01: dir = 1
  17. WHILE _KEYDOWN(27) = 0
  18.     IF INKEY$ = " " THEN initFerns
  19.     FOR i = 4 TO 11 STEP .2
  20.         LINE (-5, i)-(5, i + .2), _RGB32(60, (1 - i / 11) * 150 + 60, (1 - i / 11) * 75 + 180), BF
  21.     NEXT
  22.     FOR i = 0 TO 4 STEP .2
  23.         LINE (-5, i)-(5, i + .2), _RGB32(i / 4 * 90 + 10, i / 4 * 45 + 5, i / 4 * 22 + 2), BF
  24.     NEXT
  25.     FOR i = 1 TO nFerns
  26.         drawFern fern(i).x, fern(i).y, fern(i).scale, fern(i).c, wind
  27.     NEXT
  28.  
  29.     _DISPLAY
  30.     _LIMIT 10
  31.     wind = wind + dir * dw
  32.     IF wind > .06 OR wind < -.72 THEN dir = -dir
  33.  
  34. SUB initFerns
  35.     nFerns = 4 + INT(RND * 9)
  36.     REDIM fern(1 TO nFerns) AS obj
  37.     FOR i = 1 TO nFerns
  38.         fern(i).x = RND * 10 - 5
  39.         fern(i).y = RND * 2 - 1
  40.         fern(i).scale = RND * .7 + .3
  41.         g = RND * 100 + 40
  42.         fern(i).c = _RGB32(g - 20 - RND * 60, g, g - 20 - RND * 60)
  43.     NEXT
  44.  
  45. SUB drawFern (xoff0, yoff0, scale, c AS _UNSIGNED LONG, w)
  46.     yAdj = yoff0 + (1 - scale) * 5
  47.     FOR i = 1 TO 90000 'enough dots to get idea
  48.         SELECT CASE RND
  49.             CASE IS < .01
  50.                 nextX = 0
  51.                 nextY = .16 * y
  52.             CASE .01 TO .08
  53.                 nextX = .2 * x - .26 * y
  54.                 nextY = .23 * x + .22 * y + 1.6
  55.             CASE .08 TO .15
  56.                 nextX = -.15 * x + .28 * y
  57.                 nextY = .26 * x + .24 * y + .44
  58.             CASE ELSE
  59.                 nextX = .85 * x + .04 * y
  60.                 nextY = -.04 * x + .85 * y + 1.6
  61.         END SELECT
  62.         x = nextX + w * nextY / 10
  63.         y = nextY
  64.         LINE (x * scale + xoff0, y * scale + yAdj)-STEP(0, 0), c, BF
  65.     NEXT
  66.  
  67.  

It's amazing how much hand drawn graphics can be executed in a second!
« Last Edit: August 06, 2020, 12:51:22 pm by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Fractal Fern
« Reply #4 on: August 06, 2020, 04:38:15 pm »
Awesome mod Bplus, more realistic swaying than mine is, I'll have to check out the code.

Yep Johno, the stuff you can find buried online... there's amazing gems out there.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Fractal Fern
« Reply #5 on: August 06, 2020, 08:13:23 pm »
Dang! It's back again!

 
It's back again!.PNG

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Fractal Fern
« Reply #6 on: August 06, 2020, 08:58:58 pm »
Amazing! The things you find hiding in the grass....
Logic is the beginning of wisdom.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Fractal Fern
« Reply #7 on: August 06, 2020, 09:43:22 pm »
ROFL! I bet that looks amazing with the animation.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Fractal Fern
« Reply #8 on: August 06, 2020, 11:57:44 pm »
Good job, @bplus and @SierraKen :-)
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Fractal Fern
« Reply #9 on: August 07, 2020, 12:04:45 am »
Thanks Ashish, it wasn't too hard for me since I just added a few lines to the code I found online.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Fractal Fern
« Reply #10 on: August 07, 2020, 12:10:14 am »
Sierra, if you are interesed, you can see a collection of fractals in QB64 here - https://www.qb64.org/forum/index.php?topic=182
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Fractal Fern
« Reply #11 on: August 07, 2020, 01:56:02 am »
Ashish, I am awe-struck. I have no idea how you did those. Is there a trick to it? How did you get all the numbers? I don't know one thing about making these. I saved all of yours to my hard drive. There was one though called Leaf (number 6) that wouldn't work. It tries to work but the window quickly vanishes and doesn't show anything. You might want to take a look at it. But I am just amazed how good that is. Especially the plants and trees. I guess I never got that advanced in math or something. lol
« Last Edit: August 07, 2020, 01:57:06 am by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Fractal Fern
« Reply #12 on: August 07, 2020, 12:51:47 pm »
Thanks @Ashish

Ken's right that Leaf fractal #6 won't even put up a screen, I tried a couple things but still not working.


Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Fractal Fern
« Reply #13 on: August 07, 2020, 01:03:26 pm »
@SierraKen
The equations were originally taken from here - http://paulbourke.net/fractals/ifs/

About Leaf fractal : It does works on my machine.
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Fractal Fern
« Reply #14 on: August 07, 2020, 09:21:18 pm »
Check this fractal out as well, a Mandelbrot:

https://github.com/adamlwgriffiths/qbasic-fractal/blob/master/MANDELBR.BAS

Must be a good time for me to plug mine:
Code: QB64: [Select]
  1. defint a-z
  2.  
  3. const sw = 640
  4. const sh = 480
  5.  
  6. dim shared mx,my,mbl,mbr,mw
  7.  
  8. z = 0.01
  9. zz = 0.1
  10.  
  11. p1 = _newimage(sw, sh, 32)
  12. screen _newimage(sw, sh, 32)
  13.  
  14. redraw = -1
  15. iter = 100
  16.  
  17. dim c(100) as long, cc as long
  18.  
  19. for i=0 to iter-1
  20.         if i < iter/6 then
  21.                 r = 155
  22.                 g = (i mod (iter/6))*(255/(iter/6))
  23.                 b = 0
  24.         elseif i < 2*iter/6 then
  25.                 r = 155 - (i mod (iter/6))*(255/(iter/6))
  26.                 g = 155
  27.                 b = 0
  28.         elseif i < 3*iter/6 then
  29.                 r = 0
  30.                 g = 155
  31.                 b = (i mod (iter/6))*(255/(iter/6))
  32.         elseif i < 4*iter/6 then
  33.                 r = 0
  34.                 g = 155 - (i mod (iter/6))*(255/(iter/6))
  35.                 b = 155
  36.         elseif i < 5*iter/6 then
  37.                 r = (i mod (iter/6))*(255/(iter/6))
  38.                 g = 0
  39.                 b = 155
  40.         else
  41.                 r = 155
  42.                 g = 0
  43.                 b = 155 - (i mod (iter/6))*(255/(iter/6))
  44.         end if
  45.         c(i) = _rgb(r, g, b)
  46.  
  47.         mw = 0
  48.         getmouse
  49.  
  50.         if redraw then
  51.                 for y = 0 to sh-1
  52.                 for x = 0 to sw-1
  53.                         u = 0
  54.                         v = 0
  55.  
  56.                         xx = (x - sw/2)*z + x0
  57.                         yy = (y - sh/2)*z + y0
  58.                         for i = 0 to iter
  59.                                 '''mandelbrot
  60.                                 uu = u*u - v*v + xx
  61.                                 vv = 2*u*v + yy
  62.                                 '''
  63.  
  64.                                 '''burning ship
  65.                                 'u = abs(u)
  66.                                 'v = abs(v)
  67.                                 'uu = u*u - v*v + xx
  68.                                 'vv = 2*u*v + yy
  69.                                 '''
  70.  
  71.                                 '''tricorn
  72.                                 'u = u
  73.                                 'v = -v
  74.                                 'uu = u*u - v*v + xx
  75.                                 'vv = 2*u*v + yy
  76.                                 '''
  77.  
  78.                                 '''tetration
  79.                                 'u = u
  80.                                 'v = v
  81.                                 'cexp uu, vv, u, v, u, v
  82.                                 'cexp uu, vv, uu, vv, xx, yy
  83.                                 '''
  84.  
  85.                                 u = uu
  86.                                 v = vv
  87.  
  88.                                 if (u*u + v*v) > 4 then exit for
  89.                         next
  90.                         if i > iter then
  91.                                 pset(x, y), _rgb(0,0,0)
  92.                         else
  93.                                 pset(x, y), c(i mod 100)
  94.                         end if
  95.                 next
  96.                 next
  97.  
  98.                 'locate 1,1
  99.                 'print "iter =";iter
  100.                 _title str$(iter)
  101.  
  102.                 _dest p1
  103.                 _putimage , 0
  104.                 _dest 0
  105.  
  106.                 _putimage , p1
  107.                 _autodisplay
  108.  
  109.                 redraw = 0
  110.         end if
  111.  
  112.         if mw < 0 then
  113.                 zz = zz + 0.01
  114.         elseif mw > 0 then
  115.                 if zz > 0.01 then zz = zz - 0.01
  116.         end if
  117.  
  118.         'draw box
  119.         if omx <> mx or omy <> my or mw <> 0 then
  120.                 _putimage , p1
  121.                 line (mx - (sw*zz/2), my - (sh*zz/2))-step(sw*zz,sh*zz),_rgb(255,255,255),b
  122.                 _autodisplay
  123.  
  124.                 omx = mx
  125.                 omy = my
  126.         end if
  127.  
  128.         if mbl then
  129.                 do
  130.                         getmouse
  131.                 loop while mbl
  132.  
  133.                 x0 = x0 + (mx - sw/2)*z
  134.                 y0 = y0 - (sh/2 - my)*z
  135.                 z = z*zz
  136.  
  137.                 iter = iter + 50
  138.  
  139.                 redraw = -1
  140.         elseif mbr then
  141.                 do
  142.                         getMouse
  143.                 loop while mbr
  144.  
  145.                 x0 = x0 + (mx - sw/2)*z
  146.                 y0 = y0 - (sh/2 - my)*z
  147.                 z = z/zz
  148.                 redraw = -1
  149.         end if
  150.  
  151.         k = _keyhit
  152.         if k = 43 then
  153.                 iter = iter + 50
  154.                 redraw = -1
  155.         elseif k = 45 then
  156.                 if iter > 50 then iter = iter - 50
  157.                 redraw = -1
  158.         end if
  159.  
  160. loop until k = 27
  161.  
  162. sub getmouse ()
  163.         do
  164.                 mx = _mousex
  165.                 my = _mousey
  166.                 mbl = _mousebutton(1)
  167.                 mbr = _mousebutton(2)
  168.                 mw = mw + _mousewheel
  169.         loop while _mouseinput
  170.  
  171. 'u + iv = (x + iy) ^ (a + ib)
  172. sub cexp (u as double, v as double, x as double, y as double, a as double, b as double)
  173.         dim mag as double, arg as double
  174.         dim lnz as double, argz as double
  175.  
  176.         lnz = 0.5*log((x*x + y*y)+0.00001)
  177.         argz = _atan2(y, x)
  178.  
  179.         mag = exp(a*lnz - b*argz)
  180.         arg = a*argz + b*lnz
  181.  
  182.         u = mag * cos(arg)
  183.         v = mag * sin(arg)
  184.