In this lab you will create a simulated atom smasher. This machine produces two types of particles Alfa and Beta (not Alpha particles or Beta particles from physics). During each machine time cycle up to 4 particles are produced. The number of particles produced is randomly decided. Seven out of every ten particles produced is an Alfa particle. The actual number of each type produced is randomly decided (with the ratio maintained). Alpha particles last for 5 machine cycles while Beta particles last 10 machine cycles.
At the end of each cycle the particles that have decayed should explode. An example of exploding point is given in the simulation program. You can see an explanation by clicking here. Your program should be based on this program with the addition of an array of pointers to particles. You should plot the entire set of active particles at the end of each explosion and each cyle.
Use an abstract factory (and concrete factories) to generate the particles using the following:
PPtr ParticleWindow::addParticle(int ctime,int x, int y)
{
int whichOne = (random(10) > 6);
Particle *types[]={&Alfa(),&Beta()};
return Particle::genParticle(types[whichOne],ctime,x,y);
}
Here is the simulate routine called from the simulation menu
void ParticleWindow::cmRunSimulation ()
{
// INSERT>> Your code here.
for(int ctime = 0;ctime < 50;ctime++)
{
int howMany=random(5);// add up to 4 particles per cycle
for(int i = 0;i < howMany;i++)
particles.insert(addParticle(ctime,random(600),random(300)));
particles.update(this,ctime);// update the life of each particle
Invalidate();
}
}
void Particle::explode(TDC *dc, int dist)
{
dc->SetPixel(where,TColor::LtRed);
for (int k = 0;k<13*dist/*10*dist*/;k+=dist)
for (int i = 0;i< 3+k;i+=3*dist+1)
{
TColor bColor;
if ((i%4)==0)
bColor = TColor::LtRed;
else if ((i%4)==2)
bColor = TColor::LtYellow;
else if ((i%4)== 3)
bColor = TColor::White;
else
bColor = TColor::LtGray;
TBrush brush(bColor);
dc->SelectObject(brush);
TPoint topLeft = where;
topLeft.Offset(-i,-i);
TPoint bottomRight = where;
bottomRight.Offset(i,i);
dc->Ellipse(topLeft,bottomRight);
}
}
void ParticleList::update(TWindow *w,int ctime)
{
int i =0;
TDC *DragDC = new TClientDC(*w);
while(i < data.numberInArray()) {
if (data[i]->check(ctime))
{
data[i]->show(DragDC);
i++;
}
else
{
data[i]->explode(DragDC);
data.deleteItemAt(i);
// w->Invalidate();
}
w->Invalidate();
}
delete DragDC;
}
To see the header for Particle click here.
Scenarios are due one week before the lab.