Results 1 to 7 of 7

Thread: canvas sine curve

  1. #1

    canvas sine curve

    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
    
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	sinee.PNG 
Views:	160 
Size:	16.4 KB 
ID:	6769  

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    50
    Posts
    8,128
    Blog Entries
    2
    Rep Power
    10

    Re: canvas sine curve

    Thanks zak.
    Always nice code I can put into official examples.
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  3. #3

    Re: canvas sine curve

    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
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	sine.PNG 
Views:	90 
Size:	29.3 KB 
ID:	6779  

  4. #4
    Senior Member Lionheart008's Avatar
    Join Date
    Sep 2008
    Location
    Germany, Bad Sooden-Allendorf
    Age
    44
    Posts
    914
    Rep Power
    101

    Re: canvas sine curve

    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
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	error-waitkey.png 
Views:	107 
Size:	66.0 KB 
ID:	6781  
    you can't always get what you want, but if you try sometimes you might find, you get what you need

  5. #5

    Re: canvas sine curve

    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

  6. #6
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,778
    Blog Entries
    3
    Rep Power
    691

    Re: canvas sine curve

    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:
    [code=thinbasic]
    #MINVERSION 1.8.6.0
    [/code]

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


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  7. #7
    Senior Member Lionheart008's Avatar
    Join Date
    Sep 2008
    Location
    Germany, Bad Sooden-Allendorf
    Age
    44
    Posts
    914
    Rep Power
    101

    Re: canvas sine curve

    zak, petr, haven't updated to new beta, that was my little problem.
    example is working fine here. servus, frank
    you can't always get what you want, but if you try sometimes you might find, you get what you need

Similar Threads

  1. Replies: 6
    Last Post: 16-06-2010, 06:40

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •