i found another like coding called "dynmolecular" in pudn
create molecular randomly
m_x=(int)(rand()*1.0/RAND_MAX*400);
m_y=(int)(rand()*1.0/RAND_MAX*400);m_Velocity.x = (int)(rand()*1.0/RAND_MAX*10);
m_Velocity.y = (int)(rand()*1.0/RAND_MAX*10);
and do not consider possible of not being able to co-existing:
totally it support 1000 moleculars inserted by above function.
display is updated by a timer, period of updateing is 50ms.
displaying is done within "OnPaint() " ;
most important part is the collide, it simplely exchange the velocity without any consideration of the molecular 's place adjusting since he regard molecular as a rommless one;
temp = m_Velocity.x;
m_Velocity.x = Molecular->m_Velocity.x;
Molecular->m_Velocity.x = temp;
m_Velocity.y = Molecular->m_Velocity.y;
Molecular->m_Velocity.y = temp;
but he did use object list to store molecular: typedef CTypedPtrList<CObList,CMolecular*> ...;
then put all molecular into a list, this list is for each vertical pixel lines: from 0 to 399 totally 400 lines of moleculars
good thing include:
CDEC::Ellipse() will automatically draw a circle not need do it myself
carefully drawing with a new brush and brush:
CDC* pDC = GetDC();
RECT rect;
GetClientRect(&rect);
CBrush Brush(RGB(0,255,255));
pDC->FillRect(&rect,&Brush);
CBrush redBrush;
redBrush.CreateSolidBrush(RGB(255,0,0));
CBrush* pOldBrush = pDC->SelectObject(&redBrush);
for (int i = 0;i<HEIGHT;i++)
{
POSITION pos = TraceList[i].GetHeadPosition();
while (pos != NULL)
{
CMolecular* pMolecular = TraceList[i].GetNext(pos);
int x = (int)(pMolecular->m_x*1.0/WIDTH*(rect.right-rect.left));
int y = (int)(pMolecular->m_y*1.0/HEIGHT*(rect.bottom-rect.top));
pDC->Ellipse(x-2,y-2,x+2,y+2);
}
}
pDC->SelectObject(pOldBrush);
ReleaseDC(pDC);
and since list operation is under control then it is re-organizing all moleculars again upon timer event:
if (!pObj->m_MoveFlag) pObj->Move(1);