Search This Blog

Most Viewed

Thursday, February 17, 2011

Projectile Motion Using VB.Net Graphics

  

  Till now we have been learnt enough about C/C++ Graphics. Now we lead to more advance graphics VB.Net.

  For graphics in VB.Net, basic things are same as in the C/C++ graphics. Following is demo of projectile motion.

  I am writing this blog on the basis of You have already know how to work with VB.net.

  All you to done is as follows:

1. Make a new windows form project using Visual studio in VB
2. Adjust the size of Form as according to you.
3. Add a PictureBox Tool to the form and name it as 'CurvePic'
4. Add a Button and name it as 'DrawBtn'
5. Add 2 TextBoxes and name them 'VelociyTxt' and 'AngleTxt'
6. Double click the form, a form_load subroutine will create.
7. Write below code in Form_load Subroutine


  Dim bcolor As Color
        bcolor = Color.FromArgb(red:=100, blue:=50, green:=150)

        Me.BackColor = bcolor
        TitleTxt.BackColor = bcolor
        VelocityLblTxt.BackColor = bcolor
        AngleLblTxt.BackColor = bcolor
        CurvePic.BackColor = Color.White

        VelocityTxt.Text = "10 m/sec"
        AngleTxt.Text = "0°"

        velocity = 10
        angle = 0 

8. On the top of code viewer write following code

   Imports System.Math

9. In the public class (just above the form_load) write
    
    Dim velocity, angle As Double

10. goto the designer and double click the "DrawBtn". A click event is created for the "DrawBtn". Write follwing code in this


        'Declaration for variables used in drawing
        Dim x, y, t As Double
        Dim acc, scl As Double
        Dim ox, oy As Integer
        Dim tllim, tulim As Double
        Dim posx, posy As Long
        Dim rad_angle As Double

        'Conversion from degree to radian  
        rad_angle = (2 * PI / 360) * angle

        'here our very known acc and scl for plotting
        acc = 0.01
        scl = 10

        'Computer Coordinates of origin
        ox = CInt(CurvePic.Width / 2)
        oy = CInt(CurvePic.Height / 2)

        'Projectile Motion is totally depend on time, so here are time parameter for motion
        tllim = -1000
        tulim = 1000


        'Define projectile as object of graphics class
        Dim projectile As Graphics

        'Define Our curve as bitmap image
        Dim curve As New Bitmap(Width:=CurvePic.Width, Height:=CurvePic.Height)

        'The projectile object will handle the curvepic object
        projectile = Graphics.FromHwnd(CurvePic().Handle)

        'Since the projectile is now handling the CurvePic so following code will make a white rectangle on the CurvePic, for deletion of previous curve.
        projectile.FillRectangle(Brushes.White, x:=0, y:=0, height:=CurvePic.Height, width:=CurvePic.Width)


        'Here is out time running
        For t = tllim To tulim Step acc


            'Parametric equation for projectile's horizontal and vertical motion
            x = velocity * Cos(rad_angle) * t
            y = velocity * Sin(rad_angle) - (1 / 2) * (9.8) * (t ^ 2)

            
            'Coordinate Conversion
            posx = CLng(ox + x * scl)
            posy = CLng(oy - y * scl)


            'Clipping of curve so it will not go further the drawing window here it is CurvePic
            If posx > 0 And posx < CurvePic.Width And posy > 0 And posy < CurvePic.Height Then


                'set the posx and posy pixel of curve bitmap as blue, which is out projectile's trajectory 
                curve.SetPixel(x:=posx, y:=posy, color:=Color.Blue)
                
                'Axis Plotting
                curve.SetPixel(x:=ox, y:=posy, color:=Color.Red)
                curve.SetPixel(x:=posx, y:=oy, color:=Color.Red)

                'This will Draw the Curve bitmap to the CurvePic so we able to see the movement of projectile
                projectile.DrawImageUnscaled(image:=curve, x:=0, y:=0)

            End If

        Next

        'Put the complete image onto the CurvePic so we can see the total projectile path
        CurvePic.Image = curve

11. Now make four events as shown below

Private Sub VelocityTxt_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles VelocityTxt.MouseEnter
        VelocityTxt.Text = velocity

    End Sub

    Private Sub VelocityTxt_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles VelocityTxt.MouseLeave
        velocity = VelocityTxt.Text

        VelocityTxt.Text = VelocityTxt.Text + " m/sec"
    End Sub

    Private Sub AngleTxt_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles AngleTxt.MouseEnter
        AngleTxt.Text = angle

    End Sub

    Private Sub AngleTxt_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles AngleTxt.MouseLeave
        AngleTxt.Text = AngleTxt.Text + "°"
    End Sub

12. That is all. Run the program and you will get full trajectory of projectile.

After making whole code the code must look like:

Imports System.Math

Public Class projectile

    Dim velocity, angle As Double

    Private Sub projectile_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        MsgBox("Thanks For Using This Demo" + Chr(13) + "Pushpkant Yadav")

    End Sub

    Private Sub projectile_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim bcolor As Color
        bcolor = Color.FromArgb(red:=100, blue:=50, green:=150)

        Me.BackColor = bcolor
        TitleTxt.BackColor = bcolor
        VelocityLblTxt.BackColor = bcolor
        AngleLblTxt.BackColor = bcolor
        CurvePic.BackColor = Color.White

        VelocityTxt.Text = "10 m/sec"
        AngleTxt.Text = "0°"

        velocity = 10
        angle = 0



    End Sub

    Private Sub DrawBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DrawBtn.Click


        Dim x, y, t As Double
        Dim acc, scl As Double
        Dim ox, oy As Integer
        Dim tllim, tulim As Double
        Dim posx, posy As Long

        Dim rad_angle As Double

        rad_angle = (2 * PI / 360) * angle

        acc = 0.01
        scl = 10

        ox = CInt(CurvePic.Width / 2)
        oy = CInt(CurvePic.Height / 2)

        tllim = -1000
        tulim = 1000



        Dim projectile As Graphics

        Dim curve As New Bitmap(Width:=CurvePic.Width, Height:=CurvePic.Height)

        projectile = Graphics.FromHwnd(CurvePic().Handle)
        projectile.FillRectangle(Brushes.White, x:=0, y:=0, height:=CurvePic.Height, width:=CurvePic.Width)


        For t = tllim To tulim Step acc

            x = velocity * Cos(rad_angle) * t
            y = velocity * Sin(rad_angle) - (1 / 2) * (9.8) * (t ^ 2)

            posx = CLng(ox + x * scl)
            posy = CLng(oy - y * scl)

            If posx > 0 And posx < CurvePic.Width And posy > 0 And posy < CurvePic.Height Then


                curve.SetPixel(x:=posx, y:=posy, color:=Color.Blue)
                curve.SetPixel(x:=ox, y:=posy, color:=Color.Red)
                curve.SetPixel(x:=posx, y:=oy, color:=Color.Red)

                projectile.DrawImageUnscaled(image:=curve, x:=0, y:=0)

            End If

        Next
        CurvePic.Image = curve

    End Sub



    Private Sub VelocityTxt_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles VelocityTxt.MouseEnter
        VelocityTxt.Text = velocity

    End Sub

    Private Sub VelocityTxt_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles VelocityTxt.MouseLeave
        velocity = VelocityTxt.Text

        VelocityTxt.Text = VelocityTxt.Text + " m/sec"
    End Sub

    Private Sub AngleTxt_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles AngleTxt.MouseEnter
        AngleTxt.Text = angle

    End Sub

    Private Sub AngleTxt_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles AngleTxt.MouseLeave
        AngleTxt.Text = AngleTxt.Text + "°"
    End Sub

End Class

if everything was correctly done, there is no error in this code.

No comments:

Post a Comment

If the contents is insufficient or if there any error, please write here....


Suggestion are welcome:

Popular Posts