1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Visual Basics programming help required

  1. Nov 11, 2005 #1
    I've posted this in the homework section since it is an assinment.

    I am required to write a program in VB than creats N number of 'particles' in a random position within a caresian co-ordinate system. The x and y axis are to go from -100 to 100 and be bounded i.e if a particle's x or y component reach 100 or -100 then it`s angle of incidence must equal it angle of reflection.

    Now I`ve started off by writing a simplified program for a single particle that just bounces around in it's 'box' heres the code so you can see what i`ve done.

    Code (Text):

    Option Explicit
    Dim x, y, speed_x, speed_y As Double
    Dim i, j, delta_t, t As Long

    Private Sub Command1_Click()
    [COLOR="SeaGreen"]'defines the dimentions of the box[/COLOR]
    Picture1.Scale (-100, 100)-(100, -100)

    [COLOR="seagreen"]'declare variables[/COLOR]
    speed_x = Rnd * 10
    speed_y = Rnd * 10

    delta_t = 1

    [COLOR="seagreen"]'make the circle move[/COLOR]
    For t = 1 To 200 Step delta_t
    x = x + speed_x
    y = y + speed_y
    If x > 95 Or x < -95 Then speed_x = -speed_x
    If y > 95 Or y < -95 Then speed_y = -speed_y
    Call plot(x, y)
    Call Delay(t)
    Picture1.Cls
    Next t
    End Sub

    [COLOR="seagreen"]'create a purple circle in a subroutine called plot (x,y)[/COLOR]
    Sub plot(x, y)
    Picture1.FillStyle = 0
    Picture1.FillColor = RGB(255, 0, 255)
    Picture1.Circle (x, y), 5, RGB(255, 0, 255)
    End Sub

    [COLOR="seagreen"]'pause circle with a subroutine called delay (t)[/COLOR]
    Sub Delay(t)
    For i = 1 To 1000000
    Next i
    End Sub
     
    The first problem i have is i can`t randdomly generate the inital (x, y) co-ordinates, remebersinf the Rnd command creates a positive number between 0 and 1. Scaling this isn`t a problem, but getting it to appear anywhere other than (x, y) for X>0 and y>0, is causing me a head ache.

    Once i can work than out i don`t know how to generate more than one circle in a picture box?

    PLEASE, PLEASE HELP :cry:
     
  2. jcsd
  3. Nov 11, 2005 #2

    -Job-

    User Avatar
    Science Advisor

    Don't you have to call Randomize() before you generate a random number?
     
  4. Nov 11, 2005 #3

    I don`t know. I`ve told about the Rnd command but nothing else and unfortunatly i don`t have any litreture to refer to.

    Could you explain the Randomize() function please.

    Edit: Ignore the aboce. I`ve just tried the Randomize () command with the Rnd Command and it seems to work but how do i randomize my inital (x,y) co-ordinated and then keep them?

    DO i simple say
    Randomize (x,y)
    x= Rnd*10
    y= Rnd*10
    and add this before the fr loop?
     
    Last edited: Nov 11, 2005
  5. Nov 11, 2005 #4

    Pyrrhus

    User Avatar
    Homework Helper

    Yes, you have to call Randomize() first, because it initiates the random generator.
     
  6. Nov 11, 2005 #5
    Option Explicit
    Dim x, y, speed_x, speed_y As Double
    Dim i, j, delta_t, t As Long
    Private Sub Command1_Click()
    'defines the dimentions of the box
    Randomize Timer
    Picture1.Scale (-100, 100)-(100, -100)
    'declare variables
    speed_x = Rnd * 10
    speed_y = Rnd * 10
    delta_t = 1
    'make the circle move
    x = (Rnd * 200) - 100
    y = (Rnd * 200) - 100
    For t = 1 To 200 Step delta_t
    x = x + speed_x
    y = y + speed_y
    If x > 95 Or x < -95 Then speed_x = -speed_x
    If y > 95 Or y < -95 Then speed_y = -speed_y
    Call plot(x, y)
    Call Delay(t)
    Picture1.Cls
    Next t
    End Sub
    'create a purple circle in a subroutine called plot (x,y)
    Sub plot(x, y)
    Picture1.FillStyle = 0
    Picture1.FillColor = RGB(255, 0, 255)
    Picture1.Circle (x, y), 5, RGB(255, 0, 255)
    DoEvents
    End Sub
    'pause circle with a subroutine called delay (t)
    Sub Delay(t)
    For i = 1 To 1000000
    Next i
    End Sub
     
  7. Nov 11, 2005 #6
    Thank you all very much.

    The randomize timer starts the generation process. and then every time you use Rnd you get a new number, now i get it.

    Could i ask one more question?

    How do you generate more than one circle?
     
  8. Nov 11, 2005 #7
    Code (Text):
    Option Explicit
    Dim x(10), y(10), speed_x(10), speed_y(10) As Double
    Dim p, i, j, delta_t, t As Long
    Dim NumPart As Long
    Private Sub Command1_Click()
    'defines the dimentions of the box
    NumPart = 5
    Randomize Timer
    Picture1.Scale (-100, 100)-(100, -100)
    'declare variables
    For p = 1 To NumPart
    speed_x(p) = Rnd * 10
    speed_y(p) = Rnd * 10
    delta_t = 1
    'make the circle move
    x(p) = (Rnd * 200) - 100
    y(p) = (Rnd * 200) - 100
    Next p
    For t = 1 To 200 Step delta_t
    For p = 1 To NumPart
    x(p) = x(p) + speed_x(p)
    y(p) = y(p) + speed_y(p)
    If x(p) > 95 Or x(p) < -95 Then speed_x(p) = -speed_x(p)
    If y(p) > 95 Or y(p) < -95 Then speed_y(p) = -speed_y(p)
    Call plot(x(p), y(p))
    Next p
    Call Delay(t)
    Picture1.Cls
    Next t
    End Sub
    'create a purple circle in a subroutine called plot (x,y)
    Sub plot(x, y)
    Picture1.FillStyle = 0
    Picture1.FillColor = RGB(255, 0, 255)
    Picture1.Circle (x, y), 5, RGB(255, 0, 255)
    DoEvents
    End Sub
    'pause circle with a subroutine called delay (t)
    Sub Delay(t)
    For i = 1 To 1000000
    Next i
    End Sub
    Set the NumPart variable the number of particles..
     
  9. Nov 12, 2005 #8
    Burnsys thanx again but implimenting your changes doesn`t seem to work!

    I can see what you've tried to do, creating a function p of x,y but i think i need to creat an array to hold the initial position vectors and then use this array to 'update' the movement of the ball and a second array to hold the velocities of the particles.

    My problem now is that vb wants my array, eg. Mat_A(i, j), to equall something and i`m at aloss as to what it should equal, any hints?
     
  10. Nov 14, 2005 #9
    right, i tested it and it works fine, just paste the entire code and it should work-


    The initial position vector is set here:

    x(p) = (Rnd * 200) - 100
    y(p) = (Rnd * 200) - 100

    The velocity vector is set here:

    speed_x(p) = Rnd * 10
    speed_y(p) = Rnd * 10

    And here is the movements of the balls:

    x(p) = x(p) + speed_x(p)
    y(p) = y(p) + speed_y(p)


    What is the literal error in vb? what is the error description? and in what line?
    Try pasting the code in a new proyect. it should work
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Visual Basics programming help required
  1. Major Help Required (Replies: 4)

  2. Help required (Replies: 1)

Loading...