2 pzr1988 pzr1988 于 2013.07.08 21:10 提问

找到一段代码不知道什么意思,求解释代码

#include
#define DIST(X,Y,A,B) DA=(X)-(A);DB=(Y)-(B);D=std::sqrt(DA*DA+DB*DB);C=std::max(1.0-(D/H)*(D/H)*(D/H),0.0)*100.0f;
double X=25.0f,Y=25.0f,R=0.1f,H=0.5f,RADC=0.45f,D=0.99f,G=-9.81f;
int NRX=ceil(X/H),NRY=ceil(Y/H);
std::vector FIRST;
std::vectorstd::pair<std::pair<std::pair<double,double,std::pair >,int> > PARTICLE;
void step(double dt,int i){
FIRST.assign(NRX*NRY,-1);
for(size_t i=0;i PARTICLE[i].second=-1;
double D,DA,DB,C;
DIST(PARTICLE[i].first.first.first,PARTICLE[i].first.first.second,X/2.0,Y/2.0)
if(D > std::min(X,Y)*RADC)PARTICLE[i].first.first=std::make_pair(DA*std::min(X,Y)*RADC/D+X/2.0,DB*std::min(X,Y)*RADC/D+Y/2.0);
int& f=FIRST[std::floor(PARTICLE[i].first.first.first/H)*NRY+std::floor(PARTICLE[i].first.first.second/H)];
PARTICLE[i].second=f;
f=i;
}
for(size_t i=0;i PARTICLE[i].first.second.second+=G*dt;
int HX=std::floor(PARTICLE[i].first.first.first/H),HY=std::floor(PARTICLE[i].first.first.second/H);
for(int XX=std::max(HX-1,0);XX for(int YY=std::max(HY-1,0);YY for(int OFF=FIRST[XX*NRY+YY];OFF!=-1;OFF=PARTICLE[OFF].second){
double D,DA,DB,C;
DIST(PARTICLE[i].first.first.first,PARTICLE[i].first.first.second,PARTICLE[OFF].first.first.first,PARTICLE[OFF].first.first.second)
if(OFF != i && C > 0.0){PARTICLE[i].first.second.first+=DA*C*dt/D;PARTICLE[i].first.second.second+=DB*C*dt/D;}
}
}
for(size_t i=0;i PARTICLE[i].first.second.first*=D;
PARTICLE[i].first.second.second*=D;
PARTICLE[i].first.first.first+=PARTICLE[i].first.second.first*dt;
PARTICLE[i].first.first.second+=PARTICLE[i].first.second.second*dt;
}
}
void init(){
for(double XX=0;XX for(double YY=0;YY double D,DA,DB,C;
DIST(XX,YY,X/2.0,Y/2.0);
if(D PARTICLE.push_back(std::make_pair(std::make_pair(std::make_pair(XX,YY),std::make_pair(0.0,0.0)),-1));
}
}
}
#include
int main()
{
init();
HWND myconsole = GetConsoleWindow();
HDC mydc = GetDC(myconsole);
SelectObject(mydc,CreateSolidBrush(RGB(0,0,0)));
for(int i=0;i<100000;i++){
step(0.002f,i);
if(i%100==0){
Rectangle(mydc,0,0,X*10,Y*10);
for(size_t i=0;i<PARTICLE.size();i++)SetPixel(mydc,PARTICLE[i].first.first.first*10,10*Y-PARTICLE[i].first.first.second*10,RGB(255,255,255));
}
}
ReleaseDC(myconsole, mydc);
system("pause");
}
这段代码什么意思?

2个回答

pzr1988
pzr1988   2013.07.09 09:37
#include <vector>
#define DIST(X,Y,A,B) DA=(X)-(A);DB=(Y)-(B);D=std::sqrt(DA*DA+DB*DB);C=std::max(1.0-(D/H)*(D/H)*(D/H),0.0)*100.0f;
double X=25.0f,Y=25.0f,R=0.1f,H=0.5f,RADC=0.45f,D=0.99f,G=-9.81f;
int NRX=ceil(X/H),NRY=ceil(Y/H);
std::vector<int> FIRST;
std::vector<std::pair<std::pair<std::pair<double,double>,std::pair<double,double> >,int> > PARTICLE;
void step(double dt,int i){
FIRST.assign(NRX*NRY,-1);
#pragma omp parallel for
for(size_t i=0;i<PARTICLE.size();i++){
    PARTICLE[i].second=-1;
    double D,DA,DB,C;
    DIST(PARTICLE[i].first.first.first,PARTICLE[i].first.first.second,X/2.0,Y/2.0)
    if(D > std::min(X,Y)*RADC)PARTICLE[i].first.first=std::make_pair(DA*std::min(X,Y)*RADC/D+X/2.0,DB*std::min(X,Y)*RADC/D+Y/2.0);
    int& f=FIRST[std::floor(PARTICLE[i].first.first.first/H)*NRY+std::floor(PARTICLE[i].first.first.second/H)];
    PARTICLE[i].second=f;
    f=i;
}
#pragma omp parallel for
for(size_t i=0;i<PARTICLE.size();i++){
    PARTICLE[i].first.second.second+=G*dt;
    int HX=std::floor(PARTICLE[i].first.first.first/H),HY=std::floor(PARTICLE[i].first.first.second/H);
    for(int XX=std::max(HX-1,0);XX<=std::min(HX+1,NRX-1);XX++)
    for(int YY=std::max(HY-1,0);YY<=std::min(HY+1,NRY-1);YY++)
    for(int OFF=FIRST[XX*NRY+YY];OFF!=-1;OFF=PARTICLE[OFF].second){
        double D,DA,DB,C;
        DIST(PARTICLE[i].first.first.first,PARTICLE[i].first.first.second,PARTICLE[OFF].first.first.first,PARTICLE[OFF].first.first.second)
        if(OFF != i && C > 0.0){PARTICLE[i].first.second.first+=DA*C*dt/D;PARTICLE[i].first.second.second+=DB*C*dt/D;}
    }
}
#pragma omp parallel for
for(size_t i=0;i<PARTICLE.size();i++){
    PARTICLE[i].first.second.first*=D;
    PARTICLE[i].first.second.second*=D;
    PARTICLE[i].first.first.first+=PARTICLE[i].first.second.first*dt;
    PARTICLE[i].first.first.second+=PARTICLE[i].first.second.second*dt;
}
}
void init(){
for(double XX=0;XX<X;XX+=H*0.5f)
for(double YY=0;YY<Y;YY+=H*0.5f){
    double D,DA,DB,C;
    DIST(XX,YY,X/2.0,Y/2.0);
    if(D < std::min(X,Y)*RADC*0.5f){
        PARTICLE.push_back(std::make_pair(std::make_pair(std::make_pair(XX,YY),std::make_pair(0.0,0.0)),-1));
    }
}
}
#include <Windows.h>
int main()
{
init();
HWND myconsole = GetConsoleWindow();
HDC mydc = GetDC(myconsole);
SelectObject(mydc,CreateSolidBrush(RGB(0,0,0)));
for(int i=0;i<100000;i++){
    step(0.002f,i);
    if(i%100==0){
        Rectangle(mydc,0,0,X*10,Y*10);
        for(size_t i=0;i<PARTICLE.size();i++)SetPixel(mydc,PARTICLE[i].first.first.first*10,10*Y-PARTICLE[i].first.first.second*10,RGB(255,255,255));
    }
}
ReleaseDC(myconsole, mydc);
system("pause");
}
John_ToString
John_ToString   2015.12.05 16:35

不知道你想问的是什么....

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!