PDA

View Full Version : canvas sine curve



zak
12-08-2010, 09:15
Hi
drawing using canvas is a pleasure, since it is simple, and the refreshing screen done without the user to concern about it, believe me some languages do the refrersh by continious executing of the drawing code, and if you want to do the refresh yourself using api, god help you.
i have posted before about ploting sine curve slowly, and it is done using tbgl , and tbgl+ui, but using canvas alone is simpler: it is like plotting using quick basic but more advanced. you can simply convert all vb6 and quickbasic plotting codes to thinbasic.
the following code will plot the sine curve
Canvas_SetPixel(x*30+300, y*80+300) for plotting pixels,
Canvas_Line( (x1, y1), (x1, y1) , Rgb(255, 0, 0) ) for plotting with thickness defined by a previous "Canvas_Width(10)" .
i have used the canvas new feature like:
a = Canvas_WaitKey(27) to wait pressing ESC to exit, the 27 is the asc of ESC you can use 32 to exit by pressing space bar
to plot without animation just delete the first "Canvas_Redraw" and keep the second one

Uses "UI"
Uses "math"
' Global variables
'-----------------------------------------------------------
Dim ScreenWidth As Long = 800
Dim ScreenHeight As Long = 600
Dim hWin As DWord '---Handle of the canvas window
Dim a As Long
Dim x,y,x1,y1 As Double

'-----------------------------------------------------------
' Main program
'-----------------------------------------------------------
hWin = Canvas_Window("test Canvas sine curve: press ESC to exit", 1, 1, ScreenWidth, ScreenHeight )

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

'---Init canvas
Canvas_Clear(%BLACK)
Canvas_Width(10)

For x=-9 To 15 Step 0.05

y = Sin(x)
Canvas_Color(Rgb(0, 255, 0))
'plotting the thin sine curve points:
Canvas_SetPixel(x*30+300, y*80+300)
x1 = x*30+300: y1 = y*80+150
'plotting the thick sine curve points:
Canvas_Line( (x1, y1), (x1, y1) , Rgb(255, 0, 0) )
Canvas_Redraw
Next x
Canvas_Redraw


a = Canvas_WaitKey(27)

Canvas_Window End

ErosOlmi
12-08-2010, 11:51
Thanks zak.
Always nice code I can put into official examples.

zak
19-08-2010, 18:00
hello
i was not aware of the Canvas_Scale. Petr confirmed its existence and thanks for him for suggesting the feature "floating point values would be accepted" here http://community.thinbasic.com/index.php?topic=2425.0
so i repost the above example using this feature.
x in sin(x) in thinbasic in radians.
Canvas_Scale making your own world, it is better than mapping manualy your world to real canvas, it is done here automatically. you just guess the values produced by your functions or data, then design the Canvas_Scale(x1,y1,x2,y2) to suit that data.

#MINVERSION 1.8.6.0
Uses "UI"
Uses "math"
' Global variables
'-----------------------------------------------------------
Dim ScreenWidth As Long = 800
Dim ScreenHeight As Long = 600
Dim hWin As DWord '---Handle of the canvas window
Dim a As Long
Dim x,y,x1,y1,width As Double
width = 30

'-----------------------------------------------------------
' Main program
'-----------------------------------------------------------
hWin = Canvas_Window("Canvas sine curve with Canvas_Scale: press ESC to exit", 1, 1, ScreenWidth, ScreenHeight )

Canvas_Attach(hWin, 0, %TRUE) ' <- double buffer
Canvas_Scale(-4*Pi, -3, 4*Pi, 3)

'---Init canvas
Canvas_Clear(%BLACK)

For x=-4*Pi To 4*Pi Step 0.05

y = Sin(x)
Canvas_Color(Rgb(0, 255, 0))
'plotting the thin sine curve points:
Canvas_SetPixel(x, y)
'plotting the thick sine curve points:
x1 = x: y1 = y - 1.5
Canvas_Width(width)
Sleep(2)
Canvas_Line( (x1, y1), (x1, y1) , Rgb(255, 0, 0) )
width = width - 0.06
Canvas_Redraw
Next x
Canvas_Redraw


a = Canvas_WaitKey(27)

Canvas_Window End




PS: added #MINVERSION 1.8.6.0 as suggested by Petr

Lionheart008
19-08-2010, 18:32
thanks for this nice example zak :)

but I have noticed an error, see my picture, with this line

a = canvas_waitkey(27)

best regards, frank

zak
19-08-2010, 18:40
hi frank
it is running okay on my system winxp thinbasic 1.8.6,
i don't know why this error, make sure you have installed the latest thinbasic since this feature canvas_waitkey(...) introduced recently
regards

Petr Schreiber
19-08-2010, 19:00
Frank,

it is new feature in the latest Beta to have params for Canvas_Waitkey.

Zak, when surfing on the latest release wave, you might consider adding on top of your script:


#MINVERSION 1.8.6.0


This way ThinBASIC checks first, if proper version is installed.


Petr

Lionheart008
19-08-2010, 21:05
zak, petr, haven't updated to new beta, that was my little problem. :oops:
example is working fine here. servus, frank