Results 1 to 3 of 3

Thread: Example: Section 5.4 (page 41), Combined with Exercise 1 (page 44)

  1. #1
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Example: Section 5.4 (page 41), Combined with Exercise 1 (page 44)

    This one is pretty complex because it does more than one plot. Seemed like a good candidate to try the new tbgl canvas commands. Great job Petr, really neat how it works!
    Stan, this can be simplified, I just wanted to work my way through it in an organized manner. I will try to make another more optimized version using the powerful eval command.

    That plot for exercise 1 c on page 44 is very strange. I put the zoom feature just for that, still not sure what is happening
    [code=thinbasic]' Example 5.4 Combined with Exercise 1 on page 44
    ' Will use a canvas and dialog for multiple plots
    ' This is not the most optimal way to code this example
    ' I wanted to show it in a manner to make it easy to see what is happening
    ' There is a powerful eval command that would make this much better and tighter
    ' I am sure there will be opportunities to show that in other examples

    ' From Stan Blank's Book:
    ' "Python Programming in OpenGL
    ' "A Graphical Approach to Programming

    ' Converted by Kent Sarikaya
    ' Last modified: February 26, 2010


    Uses "UI", "TBGL"

    ' Use constants for ID's
    Begin Const
    %lCanvas = %WM_USER + 1
    %bClose

    ' Plot buttons
    %b1 ' y = x^2
    %b1a ' y = x^2 - 2
    %b1b ' y = x^3 - 3x - 1
    %b1c ' y = x^4 - 5x^3 + x^2 - 3x - 1
    %b1d ' y = sin(x)
    %b1e ' y = sin(3x)
    %b1f ' y = sin(x/3)
    %b1g ' y = cos(x)

    %bzDefault
    %bzFar
    %bzVeryFar

    %lZoom

    End Const

    Dim plot As String ="b1" ' Default plot
    Global zoom As Long = 5

    Function TBMAIN()

    Global hDlg As DWord

    Dialog New 0, "Function Plotter",-1,-1, 410, 480, _
    %WS_POPUP Or %WS_VISIBLE Or _
    %WS_CLIPCHILDREN Or %WS_CAPTION Or _
    %WS_SYSMENU Or %WS_MINIMIZEBOX Or %WS_MAXIMIZEBOX Or %WS_THICKFRAME, 0 To hDlg

    ' The canvas is in a label control
    Control Add Label, hDlg, %lCanvas, "", 5, 5, 400, 400
    Control Set Color hDlg, %lCanvas, %BLACK, %WHITE
    Control Set Resize hDlg, %lCanvas, 1, 1, 1, 1

    ' These are the buttons
    Control Add Button, hDlg, %bClose, "Close", 345, 461, 60, 14
    Control Set Resize hDlg, %bClose, 0, 1, 0, 1

    Control Add Button, hDlg, %b1, "y=x^2", 5, 410, 60, 14
    Control Set Resize hDlg, %b1, 0, 1, 0, 1

    Control Add Button, hDlg, %b1a, "a) y=x^2-2", 70, 410, 60, 14
    Control Set Resize hDlg, %b1a, 0, 1, 0, 1

    Control Add Button, hDlg, %b1b, "b) y=x^3-3x-1", 135, 410, 60, 14
    Control Set Resize hDlg, %b1b, 0, 1, 0, 1

    Control Add Button, hDlg, %b1c, "c) y=x^4-5x^3+x^2-3x-1", 200, 410, 80, 14
    Control Set Resize hDlg, %b1c, 0, 1, 0, 1

    Control Add Button, hDlg, %b1d, "d) y=sin(x)", 5, 429, 60, 14
    Control Set Resize hDlg, %b1d, 0, 1, 0, 1

    Control Add Button, hDlg, %b1e, "e) y=sin(3x)", 70, 429, 60, 14
    Control Set Resize hDlg, %b1e, 0, 1, 0, 1

    Control Add Button, hDlg, %b1f, "f) y=sin(x/3)", 135, 429, 60, 14
    Control Set Resize hDlg, %b1f, 0, 1, 0, 1

    Control Add Button, hDlg, %b1g, "g) y=cos(x)", 200, 429, 60, 14
    Control Set Resize hDlg, %b1g, 0, 1, 0, 1

    Control Add Button, hDlg, %bzDefault, "Zoom Default", 5, 447, 60, 14
    Control Set Resize hDlg, %bzDefault, 0, 1, 0, 1

    Control Add Button, hDlg, %bzFar, "Zoom Far", 70, 447, 60, 14
    Control Set Resize hDlg, %bzFar, 0, 1, 0, 1

    Control Add Button, hDlg, %bzVeryFar, "Zoom Very Far", 135, 447, 60, 14
    Control Set Resize hDlg, %bzVeryFar, 0, 1, 0, 1

    Control Add Label, hDlg, %lZoom, "Zoom = "+Str$(zoom), 200, 450, 60, 14
    Control Set Resize hDlg, %lZoom, 1, 1, 1, 1

    Dialog Set Minsize hDlg, 320, 230
    Dialog Show Modal hDlg, Call dlgCallback

    End Function


    CallBack Function dlgCallback()
    Static hCtrl As DWord

    Select Case CBMSG

    Case %WM_INITDIALOG
    Control Handle CBHNDL, %lCanvas To hCtrl

    ' Init OpenGL
    TBGL_BindCanvas(hCtrl)
    TBGL_RenderMatrix2D( -5, 5, -5, 5 )
    ' Set background from default black to white
    TBGL_BackColor(255,255,255)

    Case %WM_SIZE, %WM_SIZING
    TBGL_UpdateCanvasProportions(hCtrl)
    PlotFunc(hCtrl)


    Case %WM_CLOSE
    TBGL_ReleaseCanvas(hCtrl)

    Case %WM_COMMAND
    'Case buttons pressed
    Select Case CBCTL

    Case %bClose
    If CBCTLMSG = %BN_CLICKED Then Dialog End CBHNDL

    Case %b1
    plot = "b1"
    Case %b1a
    plot = "b1a"
    Case %b1b
    plot = "b1b"
    Case %b1c
    plot = "b1c"
    Case %b1d
    plot = "b1d"
    Case %b1e
    plot = "b1e"
    Case %b1f
    plot = "b1f"
    Case %b1g
    plot = "b1g"

    Case %bzDefault
    zoom = 5
    Control Set Text hDlg, %lZoom, "Zoom = "+Str$(zoom)
    Case %bzFar
    zoom = 50
    Control Set Text hDlg, %lZoom, "Zoom = "+Str$(zoom)
    Case %bzVeryFar
    zoom = 150
    Control Set Text hDlg, %lZoom, "Zoom = "+Str$(zoom)

    End Select

    End Select

    PlotFunc(hCtrl)
    End Function

    Function PlotFunc( hCtrl As DWord )
    Select Case plot
    Case "b1"
    PlotB1(hCtrl)
    Case "b1a"
    PlotB1a(hCtrl)
    Case "b1b"
    PlotB1b(hCtrl)
    Case "b1c"
    PlotB1c(hCtrl)
    Case "b1d"
    PlotB1d(hCtrl)
    Case "b1e"
    PlotB1e(hCtrl)
    Case "b1f"
    PlotB1f(hCtrl)
    Case "b1g"
    PlotB1g(hCtrl)
    End Select
    End Function

    Function PlotB1( hCtrl As DWord )
    If TBGL_CanvasBound(hCtrl) Then
    Local x,y As Single
    TBGL_ClearFrame
    TBGL_Camera(0, 0, zoom, 0, 0, 0)
    TBGL_Color(0,0,0)
    TBGL_PointSize 3
    For x = -zoom To zoom Step 0.01
    y = x*x
    TBGL_BeginPoly(%GL_POINTS)
    TBGL_Vertex(x,y)
    TBGL_EndPoly
    Next
    ShowAxis(hCtrl)
    TBGL_DrawFrame
    End If
    End Function

    Function PlotB1a( hCtrl As DWord )
    If TBGL_CanvasBound(hCtrl) Then
    Local x,y As Single
    TBGL_ClearFrame
    TBGL_Camera(0, 0, zoom, 0, 0, 0)
    TBGL_Color(0,0,0)
    TBGL_PointSize 3
    For x = -zoom To zoom Step 0.01
    y = (x^2)-2
    TBGL_BeginPoly(%GL_POINTS)
    TBGL_Vertex(x,y)
    TBGL_EndPoly
    Next
    ShowAxis(hCtrl)
    TBGL_DrawFrame
    End If
    End Function

    Function PlotB1b( hCtrl As DWord )
    If TBGL_CanvasBound(hCtrl) Then
    Local x,y As Single
    TBGL_ClearFrame
    TBGL_Camera(0, 0, zoom, 0, 0, 0)
    TBGL_Color(0,0,0)
    TBGL_PointSize 3
    For x = -zoom To zoom Step 0.01
    y = ((x^3)-(3*x))-1
    TBGL_BeginPoly(%GL_POINTS)
    TBGL_Vertex(x,y)
    TBGL_EndPoly
    Next
    ShowAxis(hCtrl)
    TBGL_DrawFrame
    End If
    End Function

    Function PlotB1c( hCtrl As DWord )
    If TBGL_CanvasBound(hCtrl) Then
    Local x,y As Single
    TBGL_ClearFrame
    TBGL_Camera(0, 0, zoom, 0, 0, 0)
    TBGL_Color(0,0,0)
    TBGL_PointSize 3
    For x = -zoom To zoom Step 0.01
    y = x^4 - 5*x^3 + x^2 - 3*x -1
    TBGL_BeginPoly(%GL_POINTS)
    TBGL_Vertex(x,y)
    TBGL_EndPoly
    Next
    ShowAxis(hCtrl)
    TBGL_DrawFrame
    End If
    End Function

    Function PlotB1d( hCtrl As DWord )
    If TBGL_CanvasBound(hCtrl) Then
    Local x,y As Single
    TBGL_ClearFrame
    TBGL_Camera(0, 0, zoom, 0, 0, 0)
    TBGL_Color(0,0,0)
    TBGL_PointSize 3
    For x = -zoom To zoom Step 0.01
    y = Sin(x)
    TBGL_BeginPoly(%GL_POINTS)
    TBGL_Vertex(x,y)
    TBGL_EndPoly
    Next
    ShowAxis(hCtrl)
    TBGL_DrawFrame
    End If
    End Function

    Function PlotB1e( hCtrl As DWord )
    If TBGL_CanvasBound(hCtrl) Then
    Local x,y As Single
    TBGL_ClearFrame
    TBGL_Camera(0, 0, zoom, 0, 0, 0)
    TBGL_Color(0,0,0)
    TBGL_PointSize 3
    For x = -zoom To zoom Step 0.01
    y = Sin(3*x)
    TBGL_BeginPoly(%GL_POINTS)
    TBGL_Vertex(x,y)
    TBGL_EndPoly
    Next
    ShowAxis(hCtrl)
    TBGL_DrawFrame
    End If
    End Function

    Function PlotB1f( hCtrl As DWord )
    If TBGL_CanvasBound(hCtrl) Then
    Local x,y As Single
    TBGL_ClearFrame
    TBGL_Camera(0, 0, zoom, 0, 0, 0)
    TBGL_Color(0,0,0)
    TBGL_PointSize 3
    For x = -zoom To zoom Step 0.01
    y = Sin(x/3)
    TBGL_BeginPoly(%GL_POINTS)
    TBGL_Vertex(x,y)
    TBGL_EndPoly
    Next
    ShowAxis(hCtrl)
    TBGL_DrawFrame
    End If
    End Function

    Function PlotB1g( hCtrl As DWord )
    If TBGL_CanvasBound(hCtrl) Then
    Local x,y As Single
    TBGL_ClearFrame
    TBGL_Camera(0, 0, zoom, 0, 0, 0)
    TBGL_Color(0,0,0)
    TBGL_PointSize 3
    For x = -zoom To zoom Step 0.01
    y = Cos(x)
    TBGL_BeginPoly(%GL_POINTS)
    TBGL_Vertex(x,y)
    TBGL_EndPoly
    Next
    ShowAxis(hCtrl)
    TBGL_DrawFrame
    End If
    End Function

    Function ShowAxis( hCtrl As DWord )
    If TBGL_CanvasBound(hCtrl) Then
    TBGL_Color(255,0,0)
    TBGL_BeginPoly(%GL_LINES)
    TBGL_Vertex(-zoom,0)
    TBGL_Vertex(zoom,0)
    TBGL_EndPoly
    TBGL_Color(0,255,0)
    TBGL_BeginPoly(%GL_LINES)
    TBGL_Vertex(0,zoom)
    TBGL_Vertex(0,-zoom)
    TBGL_EndPoly
    End If
    End Function

    ' This is a mixture of code ideas from section 5.2
    ' Starting from page 41 to 44[/code]
    Attached Files Attached Files
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  2. #2
    Member sblank's Avatar
    Join Date
    Feb 2010
    Location
    Wayne City, Illinois
    Posts
    80
    Rep Power
    23

    Re: Example: Section 5.4 (page 41), Combined with Exercise 1 (page 44)

    Outstanding job, Kent...

    Exercise 1c on page 44 is a bit strange... the range of the function is huge for a small domain. I've attached a pdf of the function plot using Mathematica. You can see the domain {-5, 5} and the range is very large.

    This was one of those exercises where I would have the students scratching their heads a bit... and hopefully together we could explore the plot. So it was by design, but perhaps not the best of exercises? Your plot is correct, though... and this kind of exercise would lead to discussions about methods of rendering the plot differently.

    I like the zoom feature!

    Cheers,

    Stan


    Quote Originally Posted by kent sarikaya
    This one is pretty complex because it does more than one plot. Seemed like a good candidate to try the new tbgl canvas commands. Great job Petr, really neat how it works!
    Stan, this can be simplified, I just wanted to work my way through it in an organized manner. I will try to make another more optimized version using the powerful eval command.

    That plot for exercise 1 c on page 44 is very strange. I put the zoom feature just for that, still not sure what is happening
    Attached Images Attached Images

  3. #3
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Re: Example: Section 5.4 (page 41), Combined with Exercise 1 (page 44)

    Thanks Stan, I like that equation. It will be fun to keep checking further plotting programs with it. Now I have an idea of what it should look like.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

Similar Threads

  1. Example: Section 8.2 (page 261), A little Gravity!
    By Michael Clease in forum ThinBASIC programming in OpenGL/TBGL
    Replies: 1
    Last Post: 27-02-2010, 03:59

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

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