another brownian motion - analysis

本文介绍了一个名为“dynmolecular”的分子模拟程序,该程序能够随机创建分子,并通过简单碰撞交换速度来模拟它们的行为。支持同时模拟1000个分子,使用了对象列表存储分子,并在定时更新显示中实现了分子的位置绘制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;


temp = m_Velocity.y;
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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值