PDA

View Full Version : math Art using the Canvas



zak
25-07-2011, 08:06
in this thread (http://www.thinbasic.com/community/showthread.php?10349-MathArt-Port-of-code-by-Stan-Blank&highlight=MathArt) "MathArt: Port of code by Stan Blank" the program uses TBGL module to plot the points, below is the same code but using the Canvas, the part of the code :
x1 = x*25 + 300
y1 = y*25 + 280
means to amplify and shift the draw, in the original TBGL code this is done by using a virtual window to set the resolution and the coordinate system using:
TBGL_RenderMatrix2D (-axrng, axrng, axrng, -axrng)
in the original code the refresh process are done by continuosly calling the list in which the plot stored:
TBGL_CallList %listPoints
but in this canvas program the refresh are done using the instruction "Canvas_Redraw", i have inserted it in line 33 to give the impression of animation, any modifications are welcome.
7354


' originally MathArt by Stan Blank
' Ported to TBGL by Petr Schreiber
' ported to Canvas by zak, from thinbasic street, apartment 31415, nowhere planet


Uses "UI"


' Global variables
'-----------------------------------------------------------
Dim ScreenWidth As Long = 800
Dim ScreenHeight As Long = 600
Dim hWin As DWord '---Handle of the canvas window
Dim x,y,x1,y1,r As Double
Dim axrng As Double = 10
'-----------------------------------------------------------
' Main program
'-----------------------------------------------------------
hWin = Canvas_Window("mathArt _ press any key to exit", 1, 1, ScreenWidth, ScreenHeight )


Canvas_Attach(hWin, 0, %TRUE) ' <- double buffer

'---Init canvas
Canvas_Clear(%BLACK)
Canvas_Width(1)
For x = -axrng To axrng Step 0.04
For y = -axrng To axrng Step 0.04
r = Cos(x) + Sin(y)
x1 = x*25 + 300
y1 = y*25 + 280
Canvas_Color(Rgb(Cos(y*r)*255, Cos(x*y*r)*255, Sin(r*x)*255))
Canvas_SetPixel(x1, y1)

Next
Canvas_Redraw
Next

Canvas_Redraw

Canvas_WaitKey
Canvas_Window End

Petr Schreiber
25-07-2011, 08:36
Hi Zak,

thanks for the port. The good news is that TBGL_RenderMatrix2D has a friend in Canvas world too - it is called Canvas_Scale. With its help, you can specify custom coordinate system in similar way to TBGL.


' originally MathArt by Stan Blank
' Ported to TBGL by Petr Schreiber
' ported to Canvas by zak, from thinbasic street, apartment 31415, nowhere planet


Uses "UI"

'-----------------------------------------------------------
' Main program
'-----------------------------------------------------------
Function TBMain()

Dim ScreenWidth As Long = 500
Dim ScreenHeight As Long = 500
Dim hWin As DWord ' Handle of the canvas window
Dim x, y, r As Double
Dim axrng As Double = 10

hWin = Canvas_Window("mathArt - press any key to exit", 1, 1, ScreenWidth, ScreenHeight )
Canvas_Attach(hWin, 0, %TRUE) ' Double buffering enabled
Canvas_Scale(-axrng, -axrng, axrng, axrng) ' Custom coordinate system

'---Init canvas
Canvas_Clear(%BLACK)
Canvas_Width(1)
For x = -axrng To axrng Step 0.04
For y = -axrng To axrng Step 0.04
r = Cos(x) + Sin(y)
Canvas_Color(Rgb(Cos(y*r)*255, Cos(x*y*r)*255, Sin(r*x)*255))
Canvas_SetPixel(x, y)
Next
Canvas_Redraw
Next

Canvas_Redraw

Canvas_WaitKey
Canvas_Window End

End Function



Petr

zak
25-07-2011, 09:05
Thank you very much Petr for this, i don't know about Canvas_Scale, indeed i was wishing something like that available, this is unbelievable
and using canvas in this way is the best choice for who want to draw math functions and other plots without complexities.
best wishes
zak

matthew
25-07-2011, 12:07
When I run either of the examples in this thread on my computer all I see on the screen is a static image. Is that expected or am I supposed to be seeing some Colour Cycling (http://en.wikipedia.org/wiki/Color_cycling)?

zak
25-07-2011, 14:40
Hi matthew
the expected picture is the static picture as displayed above, but from the beginning of the draw it will display the draw process column by column untill it finished then it will stay static on the screen until pressing a key to exit
attached a video capture (mpg) of the second example by Petr using my web cam from the monitor screen from the moment i clicked run on the IDE.

matthew
25-07-2011, 17:18
Ah okay that's what I saw on my screen.