Visual Basics programming help required

zanazzi78
Messages
115
Reaction score
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' here's the code so you can see what i`ve done.

Code:
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
Picture1.Scale (-100, 100)-(100, -100)

'declare variables
speed_x = Rnd * 10
speed_y = Rnd * 10

delta_t = 1

'make the circle move
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)
End Sub

'pause circle with a subroutine called delay (t)
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:
 
Physics news on Phys.org
Don't you have to call Randomize() before you generate a random number?
 
-Job- said:
Don't you have to call Randomize() before you generate a random number?


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:
Yes, you have to call Randomize() first, because it initiates the random generator.
 
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
 
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?
 
Code:
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..
 
Burnsys thanks 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?
 
zanazzi78 said:
Burnsys thanks 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?

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
 

Similar threads

Replies
1
Views
3K
Replies
4
Views
2K
Replies
21
Views
4K
2
Replies
60
Views
11K
Replies
1
Views
2K
3
Replies
100
Views
11K
Back
Top