Page 1 of 1

CustomGradient() - Spiral gradient - Fractal gradient

Posted: Sun Aug 16, 2009 12:26 am
by eddy
updated 4.40b3

Image

Code: Select all

#Image=0
#Window=0
#Gadget=0

Procedure.f ATan2(x.f, y.f)
   Protected angle.f=ATan(y/x)
   If x<0
      angle+3.141592653589793238;#PI
   ElseIf y<0
      angle+6.283185307179586477;2*#PI
   EndIf
   ProcedureReturn angle
EndProcedure
Structure SpiralGradient
   px.f
   py.f
   radius.f
   loop.i
EndStructure
Procedure.f SpiralGradientCallback(x, y)
   Shared SpiralGradient.SpiralGradient
   
   Protected.f px, py, d, dx, dy, c1, c2, c3, a, value
   dx=(x-SpiralGradient\px)
   dy=(y-SpiralGradient\py)
   d=Sqr(dx*dx+dy*dy)
   a=ATan2(dx, dy)
   value=(Cos(2*#PI*d/(SpiralGradient\radius/SpiralGradient\loop)+a)+1)/2

   ProcedureReturn value
EndProcedure
Procedure SpiralGradient(x, y, radius, loop)
   Shared SpiralGradient.SpiralGradient
   SpiralGradient\px=x
   SpiralGradient\py=y
   SpiralGradient\radius=radius
   SpiralGradient\loop=loop
   CustomGradient(@SpiralGradientCallback())
EndProcedure

If OpenWindow(#Window, 0, 0, 300, 300, "Spiral Gradient", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   
   If CreateImage(#Image, 300, 300)
      If StartDrawing(ImageOutput(#Image))
            DrawingMode(#PB_2DDrawing_Gradient)
            GradientColor(0.00, $FFFF0000)
            GradientColor(0.20, $FFFFFF00)
            GradientColor(0.50, $FF00FF00)
            GradientColor(0.80, $FF00FFFF)
            GradientColor(1.00, $FF0000FF)
            SpiralGradient(150, 150, 150, 3)
            Circle(150, 150, 150)
            ;Box(0, 0, 300, 300)
         StopDrawing()
      EndIf
      
      ImageGadget(#Gadget, 0, 0, 300, 300, ImageID(#Image))
   EndIf
   
   Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf

Posted: Sat Aug 22, 2009 3:00 am
by oreopa
Interesting... I get a linker error...

"POLINK: Error: Unresolved external symbol '_PB_Custom_Gradient'

What did I break? :D

XP/4.40b2

Posted: Sat Aug 22, 2009 10:50 am
by Andre
On my MacOS x86 10.5.8 using PureBasic v4.40 beta2 this code example produces only a black window background with a red-filled circle on it.

Anything missing in the Mac version of PB?

Posted: Sat Aug 22, 2009 11:27 am
by Trond
I get the linker error, too.

Posted: Sat Aug 22, 2009 12:25 pm
by blueznl
Lollipop ahoy. Runs fine on 4.40b1 here.

Posted: Sun Aug 23, 2009 1:33 am
by SFSxOI
This is for the 4.4 beta right? It would really help if people would say that in their post or code.

Posted: Sun Aug 23, 2009 2:16 pm
by srod
Linker error here as well with PB 4.4 beta 2 x86.

Posted: Sun Aug 23, 2009 6:34 pm
by freak
There is a bug with this in beta2.

Re: CustomGradient() - spiral gradient

Posted: Wed Sep 16, 2009 1:37 am
by eddy
Image

updated for PB4.40b3 :mrgreen:

I tried to code a fractal gradient but ... too complex !

Re: CustomGradient() - spiral gradient

Posted: Wed Sep 16, 2009 2:32 pm
by STARGĂ…TE
I tried to code a fractal gradient but ... too complex !
This one ?

Code: Select all

Structure FractalGradient
 x.l
 y.l
 Size.l
 MaxIteration.l
EndStructure
Procedure.d Iteration(cx.d, cy.d)
 Shared FractalGradient.FractalGradient
 Protected Iter.l, x.d, y.d, xt.d, yt.d
 While x*x + y*y <= 6 And Iter < FractalGradient\MaxIteration
  xt = x*x - y*y + cx 
  yt = 2*x*y + cy 
  x = xt 
  y = yt
  Iter + 1 
 Wend 
 ProcedureReturn Iter/FractalGradient\MaxIteration
EndProcedure
Procedure.f FractalGradientCallback(x, y)
 Shared FractalGradient.FractalGradient
 With FractalGradient
  ProcedureReturn Iteration((x-\x)/\Size, (y-\y)/\Size)
 EndWith
EndProcedure
Procedure FractalGradient(x, y, Size, MaxIteration)
 Shared FractalGradient.FractalGradient
 With FractalGradient
  \x = x
  \y = y
  \Size = Size
  \MaxIteration = MaxIteration
 EndWith
 CustomGradient(@FractalGradientCallback())
EndProcedure

Enumeration
 #Image
 #Window
 #Gadget
EndEnumeration


CreateImage(#Image, 384, 256)
StartDrawing(ImageOutput(0))
 DrawingMode(#PB_2DDrawing_Gradient)
 FractalGradient(256, 128, 128,63)
 GradientColor(0.00, $0000FF)
 GradientColor(0.25, $00FFFF)
 GradientColor(0.50, $00FF00)
 GradientColor(0.75, $FFFF00)
 GradientColor(1.00, $FF0000)
 Box(0,0,384,256)
StopDrawing()

OpenWindow(#Window, 0, 0, ImageWidth(#Image), ImageHeight(#Image), "Fractal", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
 ImageGadget(#Gadget,0,0,ImageWidth(#Image), ImageHeight(#Image), ImageID(#Image))

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: CustomGradient() - Spiral gradient - Fractal gradient

Posted: Wed Sep 16, 2009 2:43 pm
by eddy
:o ... Cool! Thx for your code.