Fractaler V1.2

Post Reply
User avatar
rbytes
Posts: 1580
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 6
iPad 4
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Fractaler V1.2

Post by rbytes » Tue Mar 13, 2018 5:36 pm

I have a fascination with mathematical drawings, especially fractals. Simple equations can generate drawings with amazing topography. I have had Fractaler for a few years. It was written by Mr. Kibernetik as a grayscale fractal generator in 2014, and enhanced with color by Dutchman that same year. I decided I would play with it a bit and see if I could add some useful parameters.

The result is below. When you first run it you get a very pixelated fractal. That was done deliberately so that it would render quickly until you had positioned and scaled it. Just drag your finger over it to position it, and use two fingers to shrink or enlarge it. You can zoom in on tiny areas of the starting fractal and thus produce a new one. Set the sliders and radio buttons to your preference.

When you have something you like, slide the DET slider all the way to the left. Now you will render a very finely detailed fractal. It will take time. The first screenshot below shows a fractal that took 3 minutes and 42 seconds to complete.

Other controls are:
SAVE button - saves the image as "frac.png" and exports it to the Camera Roll
STOP button - ends the program
DET slider - increases detail as it is slid toward the left
HUE slider - sets the outermost color to any hue in the spectrum
GAIN slider - increases the hue change between color areas as it is slid toward the left
SHP slider - modifies the basic shape of the fractal
DEP slider - increases the # of iterations (depth) as it is slid toward the left
BG radio buttons - set the background to transparent (clear), white or black
The clear option allows you to make a very cool photo frame. See the example.

Code: Select all

'Fractaler V1.2
'fractal browser
'by Mr.Kibernetik, 2014
'V1.2  Additional controls and options by rbytes, March 2018
'V1.1 Palet-function (simplified) added by Dutchman, February 2014
'
'g'---- parameters to change colour palette
REVERSE=0 '0 OR 1
touched.shift=1  ' between 0 and 1 (hue start color)
touched.gain=1 ' larger than 0 (hue step distance)
touched.param1=5 ' 2 to 5 (basic shape of fractal)
touched.param2=80 ' 0 to 80 (iterations of fractal)
''------------------------

SET TOOLBAR OFF
SET BUTTONS CUSTOM
init
LOOP: DRAWIT
wait: SLOWDOWN!IF touched THEN LOOP ELSE wait

DEF touched
IF SLIDER_CHANGED("hue") THEN
  shift=SLIDER_VALUE("hue")
  touched =1
  RETURN
END IF
IF SLIDER_CHANGED("gain") THEN
  gain=SLIDER_VALUE("gain")*5
  touched =1
  RETURN
END IF
IF SLIDER_CHANGED("param1") THEN
  param1=SLIDER_VALUE("param1")*3+2
  GRAPHICS CLEAR
  touched =1
  RETURN
END IF
IF SLIDER_CHANGED("param2") THEN
  param2=SLIDER_VALUE("param2")*40+40
  GRAPHICS CLEAR
  touched =1
  RETURN
END IF
IF SLIDER_CHANGED("step") AND SLIDER_VALUE("step")<>init.old_slider THEN
  p=0.2
  PAUSE p
  WHILE SLIDER_CHANGED("step") ! PAUSE p ! END WHILE
  GRAPHICS CLEAR
  sv=SLIDER_VALUE("step")
  init.old_slider=sv
  scr.STEP=INT(sv*(scr.maxstep-1))+1
  touched=1
  RETURN
END IF
touched=0
first0=1
first1=1
LOOP: GET TOUCH 0 AS x0,y0
IF BUTTON_PRESSED("Save") THEN
  SPRITE "frac" SAVE "frac.png"
  ALBUM EXPORT "frac.png"
END IF
IF BUTTON_PRESSED("clr") THEN
  BUTTON "clr" TEXT "🔘"
  BUTTON "clr2" TEXT "⚪️"
  BUTTON "clr3" TEXT "⚪️"
  cr=0!BACKER
ENDIF
IF BUTTON_PRESSED("clr2") THEN
  BUTTON "clr" TEXT "⚪️"
  BUTTON "clr2" TEXT "🔘"
  BUTTON "clr3" TEXT "⚪️"
  cr=1!BACKER
ENDIF
IF BUTTON_PRESSED("clr3") THEN
  BUTTON "clr" TEXT "⚪️"
  BUTTON "clr2" TEXT "⚪️"
  BUTTON "clr3" TEXT "🔘"
  cr=2!BACKER
ENDIF
IF BUTTON_PRESSED("Stop") THEN END
IF x0>-1 THEN
  GRAPHICS CLEAR
  IF first0 THEN
    first0=0
    xi0=x0 ! yi0=y0
    ss=1
  END IF
  sx=x0-xi0 ! sy=y0-yi0
  GET TOUCH 1 AS x1,y1
  IF x1>-1 THEN
    GRAPHICS CLEAR
    IF first1 THEN
      first1=0
    disi=dist(x0,y0,x1,y1)
    END IF
    dis=dist(x0,y0,x1,y1)
    ss=dis/disi
  END IF
  SPRITE "frac" AT sx,sy SCALE ss
  GOTO LOOP
END IF
IF first0=0 THEN
  frac.s/=ss
  frac.x-=sx*frac.s
  frac.y-=sy*frac.s
  SPRITE "frac" HIDE
  SPRITE "frac" AT 0,0
  touched=1
END IF
END DEF

DEF calc(x,y,param1,param2)
z=0
c=x+y*1i
COUNT=0
maxcount=param2*LOG10(INT(1/frac.s))
WHILE ABS(z)<2 AND COUNT<=maxcount
  z=z^param1+c    ' was ^2
  COUNT+=1
END WHILE
IF COUNT>maxcount THEN
  fa=0
  col.r=1 ! col.g=1 ! col.b=1
ELSE
  fa=1
'r'
  prop=COUNT/maxcount
  IF .REVERSE THEN prop=1-prop
  CALL Palet(touched.gain*prop+touched.shift)
  col.r=Palet.R ! col.g=Palet.G ! col.b=Palet.B
''
END IF
END DEF

DEF DRAWIT
maxxs=scr.width ! maxys=scr.height
xc=maxxs/2 ! yc=maxys/2
SPRITE "frac" BEGIN
again: GRAPHICS CLEAR
REFRESH
SPRITE "frac" SHOW
BACKER
FOR ys=0 TO maxys STEP scr.STEP
  IF BUTTON_PRESSED("Stop") THEN END
  y=frac.y+(ys-yc)*frac.s
  FOR xs=0 TO maxxs STEP scr.STEP
    IF touched THEN again
    x=frac.x+(xs-xc)*frac.s
      calc(x,y,touched.param1,touched.param2)
      FILL ALPHA calc.fa
      FILL COLOR col.r,col.g,col.b
      FILL RECT xs,ys TO xs+scr.STEP,ys+scr.STEP
    NEXT xs
  NEXT ys
SPRITE "frac" END
END DEF
  
DEF BACKER
  IF touched.cr=0 THEN GRAPHICS CLEAR
  IF touched.cr=1 THEN GRAPHICS CLEAR 1,1,1
  IF touched.cr=2 THEN GRAPHICS CLEAR 0,0,0
END DEF

DEF init
PRINT "Pan and pinch..."
PAUSE 1
GRAPHICS
GRAPHICS CLEAR
scr.width=SCREEN_WIDTH()
scr.height=SCREEN_HEIGHT()
scr.maxstep=scr.width/40
scr.STEP=scr.maxstep
frac.x=-0.5 ! frac.y=0 ! frac.s=3/scr.width
SPRITE "frac" BEGIN scr.width,scr.height
SPRITE "frac" END
SLIDER "step" VALUE 1 AT 120,scr.height-40 SIZE scr.width-160
SLIDER "hue" VALUE 1 AT 120,scr.height-80 SIZE scr.width/2-150
SLIDER "gain" VALUE 1 AT scr.width/2 + 110,scr.height-80 SIZE scr.width/2-150
SLIDER "param1" VALUE 3 AT 120,scr.height-120 SIZE scr.width/2-150
SLIDER "param2" VALUE 3 AT scr.width/2 + 110,scr.height-120 SIZE scr.width/2-150
FIELD "step" TEXT " DET" AT 50, scr.height-40 SIZE 50,30 RO
FIELD "hue" TEXT " HUE" AT 50, scr.height-80 SIZE 50,30 RO
FIELD "gain" TEXT " GAIN" AT scr.width/2 + 50, scr.height-80 SIZE 50,30 RO
FIELD "prm1" TEXT " SHP" AT 50, scr.height-120 SIZE 50,30 RO
FIELD "prm2" TEXT " DEP" AT scr.width/2 + 50, scr.height-120 SIZE 50,30 RO
FIELD "bg" TEXT " BG    Clear             White             Black" AT 50, scr.height-170 SIZE 330,40 RO
DRAW COLOR 0,0,0
BUTTON "Save" TEXT "SAVE" AT scr.width-220,600
BUTTON "clr" TEXT "🔘" AT 140,600 SIZE 35,35
BUTTON "clr2" TEXT "⚪️" AT 240,600 SIZE 35,35
BUTTON "clr3" TEXT "⚪️" AT 340,600 SIZE 35,35
BUTTON "Stop" TEXT "STOP" AT scr.width-120,600
FILL ALPHA 1
R = 255/255 ! G = 210/255 ! B = 190/255 'coloring
old_slider=-1
END DEF

DEF dist(x0,y0,x1,y1)=SQR((x0-x1)^2+(y0-y1)^2)

'r' ===== Simplified Palette-function
DEF Palet(Tint) ' simplified
REM parameter should be between zero and 1.
tint%=1
' ---- divide in 6 sections
tint*=6 ! section=INT(0.5+tint) ! tint%=1
ON Section GOTO S1,S2,S3,S4,S5,S6
S1: ! B=1 ! R=0 ! G=tint ! GOTO Sat
S2: ! G=1 ! R=0 ! B=1-tint ! GOTO Sat
S3: ! G=1 ! B=0 ! R=tint ! GOTO Sat
S4: ! R=1 ! B=0 ! G=1-tint ! GOTO Sat
S5: ! R=1 ! G=0 ! B=tint ! GOTO Sat
S6: ! B=1 ! G=0 ! R=1-tint
Sat:
END DEF ' Palet
''
Attachments
85E22708-D0DF-4A35-9B0B-B89D7426DEAE.png
85E22708-D0DF-4A35-9B0B-B89D7426DEAE.png (248.92 KiB) Viewed 197 times
11E0F5B5-CCE4-4A58-9F04-78582E39E3ED.png
11E0F5B5-CCE4-4A58-9F04-78582E39E3ED.png (1004.23 KiB) Viewed 200 times
2273C89D-68C8-4A1E-BA4D-449CF1D8E557.png
2273C89D-68C8-4A1E-BA4D-449CF1D8E557.png (242.92 KiB) Viewed 200 times
A20D27DD-61E3-4E5D-ACE1-252688EFE189.jpeg
A20D27DD-61E3-4E5D-ACE1-252688EFE189.jpeg (156.08 KiB) Viewed 200 times
####### Living the colorful life #######

User avatar
Dutchman
Posts: 774
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Fractaler V1.2

Post by Dutchman » Thu Mar 15, 2018 9:54 am

Great extension :!:
Thanks for sharing.
We consider it a refreshing mental exercise to figure out ways to circumvent foolishness (Quote from Richard)

User avatar
rbytes
Posts: 1580
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 6
iPad 4
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Fractaler V1.2

Post by rbytes » Thu Mar 15, 2018 8:04 pm

Thanks for your comment. :D Some days I think everything possible in Smart Basic has been done. Other days new possibilities worm their way into my brain.

I might as well keep on programming and posting, because it is good mental exercise. And who knows how long Smart Basic will keep functioning with no more updates.
####### Living the colorful life #######

Post Reply