Author Topic: Barnsley Fern (Rosetta Code task)  (Read 3084 times)

0 Members and 1 Guest are viewing this topic.

Offline AndyA

  • Newbie
  • Posts: 73
    • View Profile
Barnsley Fern (Rosetta Code task)
« on: April 16, 2021, 03:23:43 pm »
https://rosettacode.org/wiki/Barnsley_fern#QB64

'==================================================
Task
Create this fractal fern, using the following transformations:

ƒ1   (chosen 1% of the time)
        xn + 1 = 0
        yn + 1 = 0.16 yn
ƒ2   (chosen 85% of the time)
        xn + 1 = 0.85 xn + 0.04 yn
        yn + 1 = −0.04 xn + 0.85 yn + 1.6
ƒ3   (chosen 7% of the time)
        xn + 1 = 0.2 xn − 0.26 yn
        yn + 1 = 0.23 xn + 0.22 yn + 1.6
ƒ4   (chosen 7% of the time)
        xn + 1 = −0.15 xn + 0.28 yn
        yn + 1 = 0.26 xn + 0.24 yn + 0.44.
Starting position: x = 0, y = 0
'==================================================

Code: QB64: [Select]
  1. _Title "Barnsley Fern"
  2. Dim As Integer sw, sh
  3. sw = 400: sh = 600
  4. Screen _NewImage(sw, sh, 8)
  5.  
  6. Dim As Long i, ox, oy
  7. Dim As Single sRand
  8. Dim As Double x, y, x1, y1, sx, sy
  9. sx = 60: sy = 59
  10. ox = 180: oy = 4
  11.  
  12. x = 0
  13. y = 0
  14. For i = 1 To 400000
  15.     sRand = Rnd
  16.     Select Case sRand
  17.         Case Is < 0.01
  18.             x1 = 0: y1 = 0.16 * y
  19.         Case Is < 0.08
  20.             x1 = 0.2 * x - 0.26 * y: y1 = 0.23 * x + 0.22 * y + 1.6
  21.         Case Is < 0.15
  22.             x1 = -0.15 * x + 0.28 * y: y1 = 0.26 * x + 0.24 * y + 0.44
  23.         Case Else
  24.             x1 = 0.85 * x + 0.04 * y: y1 = -0.04 * x + 0.85 * y + 1.6
  25.     End Select
  26.     x = x1
  27.     y = y1
  28.     PSet (x * sx + ox, sh - (y * sy) - oy), 10
  29.  

Offline david_uwi

  • Newbie
  • Posts: 71
    • View Profile
Re: Barnsley Fern (Rosetta Code task)
« Reply #1 on: April 19, 2021, 03:56:58 am »
Nice picture. Note:the DIMs have to be changed to the QB64 format for it to work.
NOT dim as integer I, dim I as integer (or have I got an old compiler).

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Barnsley Fern (Rosetta Code task)
« Reply #2 on: April 19, 2021, 07:27:39 am »
Nice picture. Note:the DIMs have to be changed to the QB64 format for it to work.
NOT dim as integer I, dim I as integer (or have I got an old compiler).

In version 1.5, we now support the new format you’re seeing.

Instead of:   DIM x AS INTEGER: DIM y AS NTEGER: DIM z AS INTEGER...

You can now:  DIM AS INTEGER x, y, z
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!