- Jan 8, 2009
- 405
- 1
- 0
Hi,
I'm struggling with my programming assignment. We have to use animation and graphics in C# to demonstrate a scientific principle. I was hoping to do either conservation of momentum or Brownian motion, and either would need 2d "balls" moving, bouncing off the "walls" and colliding with each other...so similar code.
I decided to create a Particle class, and use a for loop to instantiate an array of n Particles, each with random x and y starting values.
This part seems to have worked. At least it compiles.
However, I'm not able to access parameters of my Particles array in the way in which I thought I would be able to:
e.g.

Could anybody help me please?
See full code below:
Thanks in advance,
Pete
I'm struggling with my programming assignment. We have to use animation and graphics in C# to demonstrate a scientific principle. I was hoping to do either conservation of momentum or Brownian motion, and either would need 2d "balls" moving, bouncing off the "walls" and colliding with each other...so similar code.
I decided to create a Particle class, and use a for loop to instantiate an array of n Particles, each with random x and y starting values.
This part seems to have worked. At least it compiles.
However, I'm not able to access parameters of my Particles array in the way in which I thought I would be able to:
e.g.
Code:
//particlesArray is 8 by 1000 array of doubles
//numParticles is previously declined public int and equals 100;
int i;
int xValue;
int yValue;
xValue=Convert.ToInt32(particlesArray[i].x); //this part doesn't work
yValue=Convert.ToInt32(particlesArray[i].y); //this part doesn't work either
// I get the error "The name particlesArray does not exist in the current context."
Could anybody help me please?
See full code below:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace ParticleBox
{
public partial class ParticleBoxForm : Form
{
public ParticleBoxForm()
{
InitializeComponent();
}
int numParticles = 100;
private Thread trd;
Graphics g = null;
Brush myBlackBrush = new SolidBrush(Color.Black);
int count;
public void initializeParticlesArray()
{
int i;
Random xRandom = new Random();
Random yRandom = new Random();
Random dXRandom = new Random();
Random dYRandom = new Random();
int G; // used for testing only, see below.
Particle[] particlesArray = new Particle[numParticles];
for (i = 0; i < numParticles; i++)
{
particlesArray[i] = new Particle(1.0, 1.0, xRandom.Next(0, particlePictureBox.Width), yRandom.Next(0, particlePictureBox.Height), dXRandom.Next(-1, 1), dYRandom.Next(-1, 1), 0.0, 0.0);
G = Convert.ToInt32(particlesArray[0].x); // this is here as a test only. It appears to work (no error or red underlines etc.)
}
}
private void ThreadTask()
{
int i;
initializeParticlesArray();
while (true)
{
g = particlePictureBox.CreateGraphics();
g.Clear(Color.White);
for (i = 0; i < numParticles; i++)
{
DrawCircle(Convert.ToInt32(particlesArray[i].X), Convert.ToInt32(particlesArray[i].Y), Convert.ToInt32(particlesArray[i].size));
///////////////////////////////////////////
//Why can't my array be seen by DrawCircle?
Thread.Sleep(10);
}
}
}
public void DrawCircle(int x, int y, int size)
{
Rectangle myRectangle = new Rectangle(x, y, size, size);
g.FillPie(myBlackBrush, myRectangle, 0, 360);
}
private void startButton1_Click(object sender, EventArgs e)
{
startButton1.Text = "Start";
trd = new Thread(new ThreadStart(this.ThreadTask));
trd.IsBackground = true;
trd.Start();
}
private void particlePictureBox_Paint(object sender, PaintEventArgs e)
{
}
private void stopButton1_Click(object sender, EventArgs e)
{
if (trd.IsAlive)
{
trd.Abort();
}
else
return;
}
private void pauseButton1_Click(object sender, EventArgs e)
{
if (trd.IsAlive)
{
trd.Suspend();
startButton1.Text = "Resume";
}
else
return;
}
private void numParticlesComboBox1_TextUpdate(object sender, EventArgs e)
{
numParticles = Convert.ToInt32(numParticlesComboBox1.Text);
}
public class Particle
{
public double size = 1.0, mass = 1.0, x = 0.0, y = 0.0, dX = 1.0, dY = 0.0, dPrevX = 0.0, dPrevY = 0.0;
Random myRandom = new Random();
bool moving = true;
private Random xRandom;
private Random yRandom;
//bool collide = false; //use for colision handling
public Particle(double pSize, double pMass, double pX, double pY, double dPX, double dPY, double dPrevPX, double dPrevPY)
{
mass = pMass;
size = pSize;
x = pX;
y = pY;
dX = dPX;
dY = dPY;
dPrevX = dPrevPX;
dPrevY = dPrevPY;
}
}
/*public double xMove()
{
if (moving == true)
{
x += dX;
if (x < 0 || x > 600)
{
x *= -1;
}
return x;
}
else
return x;
}
public double yMove()
{
if (moving == true)
{
y += dY;
if (y < 0 || y > 270)
{
y *= -1;
}
return y;
}
else
return y;
}
*/
}
}
Thanks in advance,
Pete
Last edited:
