Visual Basics programming help required

In summary, the conversation was about creating a program in VB to generate a specified number of particles in a random position within a Cartesian coordinate system. The code for a single particle bouncing within a "box" was provided, but the issue of randomly generating initial coordinates and creating multiple circles in a picture box was discussed. The solution involved using the Randomize() function to initiate the random generator, setting the initial position and velocity vectors, and updating the movement of the particles using these vectors. The conversation also touched on using arrays to store the position and velocity values. A specific error was not mentioned.
  • #1
zanazzi78
115
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()
[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:
 
Physics news on Phys.org
  • #2
Don't you have to call Randomize() before you generate a random number?
 
  • #3
-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:
  • #4
Yes, you have to call Randomize() first, because it initiates the random generator.
 
  • #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
 
  • #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?
 
  • #7
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..
 
  • #8
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?
 
  • #9
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
 

1. What is Visual Basics programming?

Visual Basics programming is a coding language used to create software applications and programs for the Windows operating system. It is a user-friendly and visual-based language, making it easier for beginners to learn and use.

2. Why do I need help with Visual Basics programming?

Learning a new programming language can be challenging, especially if you are new to coding. Getting help with Visual Basics programming can save you time and frustration by providing guidance and support while you learn the language.

3. What are some common challenges in Visual Basics programming?

Some common challenges in Visual Basics programming include understanding the syntax and logic of the language, learning how to use the various tools and features, and troubleshooting errors in code.

4. Where can I find resources for Visual Basics programming help?

There are many resources available for Visual Basics programming help, such as online tutorials, forums, and books. You can also consider taking a course or hiring a tutor for personalized assistance.

5. How can I improve my skills in Visual Basics programming?

The best way to improve your skills in Visual Basics programming is to practice regularly and challenge yourself with new projects. You can also learn from other experienced programmers, participate in coding challenges, and stay updated on new developments in the language.

Similar threads

  • Calculus and Beyond Homework Help
Replies
4
Views
1K
  • Advanced Physics Homework Help
Replies
1
Views
2K
  • Advanced Physics Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
615
  • Calculus and Beyond Homework Help
Replies
6
Views
952
  • Programming and Computer Science
Replies
2
Views
895
  • Introductory Physics Homework Help
Replies
21
Views
2K
  • Math Proof Training and Practice
2
Replies
60
Views
8K
  • Programming and Computer Science
Replies
4
Views
954
  • Math Proof Training and Practice
3
Replies
100
Views
7K
Back
Top