- UID
- 2
- 精华
- 积分
- 7736
- 威望
- 点
- 宅币
- 个
- 贡献
- 次
- 宅之契约
- 份
- 最后登录
- 1970-1-1
- 在线时间
- 小时
|
寒假写的代码第一个是模拟泊松运动
poison.rar
(24.15 KB, 下载次数: 4)
核心代码:- BOOL CCollisionaDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- // TODO: Add extra initialization here
- ShowWindow(SW_MAXIMIZE);
- CClientDC tempdc(this);
- MYDC.CreateCompatibleDC(&tempdc);
- RECT rt;
- GetClientRect(&rt);
- bmp.CreateCompatibleBitmap(&tempdc,rt.right,rt.bottom);
- MYDC.SelectObject(&bmp);
- CBrush brush;
- brush.CreateSolidBrush(RGB(255,255,255));
- MYDC.FillRect(&rt,&brush);
- srand(time(NULL));
- //初始化小球
- for(int i=0;i<BALLNUM;i++)
- {
- ball[i].x=rand()%rt.right;
- ball[i].y=rand()%rt.bottom;
- ball[i].deltax=rand()%11-5;
- ball[i].deltax+=(ball[i].deltax>0)?1:-1;
- ball[i].deltay=rand()%11-5;
- ball[i].deltay+=(ball[i].deltay>0)?1:-1;
- ball[i].deltay=1;
- ball[i].xcount=0;
- ball[i].ycount=0;
- ball[i].radius=rand()%MAXRADIU+40;
- ball[i].color=RGB(rand()&0xff,rand()&0xff,rand()&0xff);
- ball[i].iscollision=FALSE;
- }
- // ball[0].x=200;
- // ball[1].x=200;
- // ball[0].y=100;
- // ball[1].y=500;
- // ball[0].deltax=100;
- // ball[1].deltax=100;
- // ball[0].deltay=1;
- // ball[1].deltay=2;
- // ball[0].xcount=0;
- // ball[0].ycount=0;
- // ball[1].xcount=0;
- // ball[1].ycount=0;
- // ball[0].radius=20;
- // ball[1].radius=20;
- // ball[0].color=RGB(255,0,0);
- // ball[1].color=RGB(0,255,0);
- // ball[0].iscollision=FALSE;
- // ball[1].iscollision=FALSE;
- SetTimer(0,10,NULL);
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CCollisionaDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CCollisionaDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- RECT rt;
- GetClientRect(&rt);
- CBrush nulbrush(RGB(255,255,255));
- MYDC.FillRect(&rt,&nulbrush);
- CClientDC dc(this);
- for(int i=0;i<BALLNUM;i++)
- {
- CBrush brush(ball[i].color);
- MYDC.SelectObject(&brush);
- MYDC.Ellipse(ball[i].x-ball[i].radius,ball[i].y-ball[i].radius,ball[i].x+ball[i].radius,ball[i].y+ball[i].radius);
- brush.DeleteObject();
- }
- dc.BitBlt(0,0,rt.right,rt.bottom,&MYDC,0,0,SRCCOPY);
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CCollisionaDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CCollisionaDlg::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- RECT rt;
- GetClientRect(&rt);
- int i,j;
- //做移动
- for(i=0;i<BALLNUM;i++)
- {
- ball[i].xcount++;
- ball[i].ycount++;
- if(ball[i].deltax > 0)
- {
- if(ball[i].xcount >= ball[i].deltax)
- {
- ball[i].x+=2;
- ball[i].xcount=0;
- }
- }
- else
- {
- if(ball[i].xcount >= -ball[i].deltax)
- {
- ball[i].x-=2;
- ball[i].xcount=0;
- }
- }
- if(ball[i].deltay > 0)
- {
- if(ball[i].ycount >= ball[i].deltay)
- {
- ball[i].y+=2;
- ball[i].ycount=0;
- }
- }
- else
- {
- if(ball[i].ycount >= -ball[i].deltay)
- {
- ball[i].y-=2;
- ball[i].ycount=0;
- }
- }
- //判断边界
- #define MARGIN 0
- if(ball[i].x-ball[i].radius < MARGIN)
- {
- ball[i].x=MARGIN+ball[i].radius;
- if(ball[i].deltax<0)
- ball[i].deltax=-ball[i].deltax;
- }
- else if(ball[i].x+ball[i].radius > rt.right-MARGIN)
- {
- ball[i].x=rt.right-MARGIN-ball[i].radius;
- if(ball[i].deltax>0)
- ball[i].deltax=-ball[i].deltax;
- }
- if(ball[i].y-ball[i].radius < MARGIN)
- {
- ball[i].y=MARGIN+ball[i].radius;
- if(ball[i].deltay<0)
- ball[i].deltay=-ball[i].deltay;
- }
- else if(ball[i].y+ball[i].radius > rt.bottom-MARGIN)
- {
- ball[i].y=rt.bottom-MARGIN-ball[i].radius;
- if(ball[i].deltay>0)
- ball[i].deltay=-ball[i].deltay;
- }
- }
- //判断碰撞
- for(i=0;i < BALLNUM;i++)
- {
- int count=0;
- for(j=0;j != i && j < BALLNUM;j++)
- {
- if((ball[i].x-ball[j].x)*(ball[i].x-ball[j].x)+(ball[i].y-ball[j].y)*(ball[i].y-ball[j].y) <= (ball[i].radius+ball[j].radius)*(ball[i].radius+ball[j].radius))
- { //碰撞以后利用水平方向以及垂直方向动量守恒定律和能量守恒定律
- //f1'=(R1*R1*R1*f2+2*R2*R2*R2*f1-R2*R2*R2*f2)/[f1*f2*(R1*R1*R1+R2*R2*R2)]
- //f2'=(2*R1*R1*R1*f2-R1*R1*R1*f1+2*R2*R2*R2*f1)/[f1*f2*(R1*R1*R1+R2*R2*R2)]
- if(!ball[i].iscollision || !ball[j].iscollision)
- {
- int r31=ball[i].radius*ball[i].radius*ball[i].radius;//R1*R1*R1
- int r32=ball[j].radius*ball[j].radius*ball[j].radius;//R2*R2*R2
- double newxcount1=(ball[i].deltax*ball[j].deltax*(r31+r32))/float(r31*ball[j].deltax+2*r32*ball[i].deltax-r32*ball[j].deltax);
- double newycount1=(ball[i].deltay*ball[j].deltay*(r31+r32))/float(r31*ball[j].deltay+2*r32*ball[i].deltay-r32*ball[j].deltay);
- double newxcount2=(ball[i].deltax*ball[j].deltax*(r31+r32))/float(2*r31*ball[j].deltax-r31*ball[i].deltax+r32*ball[i].deltax);
- double newycount2=(ball[i].deltay*ball[j].deltay*(r31+r32))/float(2*r31*ball[j].deltay-r31*ball[i].deltay+r32*ball[i].deltay);
- if(newxcount1 > 100)
- {
- ball[i].deltax=100;
- }
- else if(newxcount1 >= 1)
- {
- ball[i].deltax=newxcount1;
- }
- else if(newxcount1 >= 0)
- {
- ball[i].deltax=1;
- }
- else if(newxcount1 > -1)
- {
- ball[i].deltax=-1;
- }
- else if(newxcount1 >= -100)
- {
- ball[i].deltax=newxcount1;
- }
- else
- {
- ball[i].deltax=-100;
- }
- if(newycount1 > 100)
- {
- ball[i].deltay=100;
- }
- else if(newycount1 >= 1)
- {
- ball[i].deltay=newycount1;
- }
- else if(newycount1 >= 0)
- {
- ball[i].deltay=1;
- }
- else if(newycount1 > -1)
- {
- ball[i].deltay=-1;
- }
- else if(newycount1 >= -100)
- {
- ball[i].deltay=newycount1;
- }
- else
- {
- ball[i].deltay=-100;
- }
- if(newxcount2 > 100)
- {
- ball[j].deltax=100;
- }
- else if(newxcount2 >= 1)
- {
- ball[j].deltax=newxcount2;
- }
- else if(newxcount2 >= 0)
- {
- ball[j].deltax=1;
- }
- else if(newxcount2 > -1)
- {
- ball[j].deltax=-1;
- }
- else if(newxcount2 >= -100)
- {
- ball[j].deltax=newxcount2;
- }
- else
- {
- ball[j].deltax=-100;
- }
- if(newycount2 > 100)
- {
- ball[j].deltay=100;
- }
- else if(newycount2 >= 1)
- {
- ball[j].deltay=newycount2;
- }
- else if(newycount2 >= 0)
- {
- ball[j].deltay=1;
- }
- else if(newycount2 > -1)
- {
- ball[j].deltay=-1;
- }
- else if(newycount2 >= -100)
- {
- ball[j].deltay=newycount2;
- }
- else
- {
- ball[j].deltay=-100;
- }
- ball[i].iscollision=TRUE;
- ball[j].iscollision=TRUE;
- }
- break;
- }
- else
- {
- count++;
- }
- }
- if(count < BALLNUM-1)
- ball[i].iscollision=TRUE;
- else
- ball[i].iscollision=FALSE;
- }
- Invalidate(FALSE);
- CDialog::OnTimer(nIDEvent);
- }
复制代码
第二个是模拟碰撞
collisiona.rar
(25.8 KB, 下载次数: 2)
这个有bug,0xAA55可以帮我看看
核心代码:
- BOOL CPosonDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- // TODO: Add extra initialization here
- ShowWindow(SW_MAXIMIZE);
- CClientDC tempdc(this);
- MYDC.CreateCompatibleDC(&tempdc);
- RECT rt;
- GetClientRect(&rt);
- bmp.CreateCompatibleBitmap(&MYDC,rt.right,rt.bottom);
- MYDC.SelectObject(&bmp);
- CBrush brush(RGB(255,255,255));
- MYDC.FillRect(&rt,&brush);
- for(int i=0;i<100;i++)
- {
- pt[i].x=rand()%rt.right;
- pt[i].y=rand()%rt.bottom;
- }
- SetTimer(0,1,NULL);
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CPosonDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CPosonDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- //CDialog::OnPaint();
- RECT rt;
- GetClientRect(&rt);
- CBrush nulbrush(RGB(255,255,255));
- MYDC.FillRect(&rt,&nulbrush);
- CPaintDC dc(this);
- CBrush brush(RGB(0,0,0));
- MYDC.SelectObject(&brush);
- for(int i=0;i<100;i++)
- {
- MYDC.Ellipse(pt[i].x-5,pt[i].y-5,pt[i].x+5,pt[i].y+5);
- }
- dc.BitBlt(0,0,rt.right,rt.bottom,&MYDC,0,0,SRCCOPY);
- brush.DeleteObject();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CPosonDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CPosonDlg::OnClose()
- {
- // TODO: Add your message handler code here and/or call default
- bmp.DeleteObject();
- CDialog::OnClose();
- }
- void CPosonDlg::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- RECT rt;
- GetClientRect(&rt);
- for(int i=0;i<100;i++)
- {
- int len=rand()%10;
- switch(rand()%4)
- {
- case 0:
- pt[i].y-=len;
- if(pt[i].y < 0)
- {
- pt[i].y=rand()%rt.bottom;
- }
- break;
- case 1:
- pt[i].y+=len;
- if(pt[i].y >= rt.bottom)
- {
- pt[i].y=rand()%rt.bottom;
- }
- break;
- case 2:
- pt[i].x+=len;
- if(pt[i].x >= rt.right)
- {
- pt[i].x=rand()%rt.right;
- }
- break;
- case 3:
- pt[i].x-=len;
- if(pt[i].x < 0)
- {
- pt[i].x=rand()%rt.right;
- }
- break;
- }
- }
- Invalidate(FALSE);
- CDialog::OnTimer(nIDEvent);
- }
复制代码 |
|