Lightening Bolts on C++
void line(double x1,double y1,double x2,double y2) {
Iw2DDrawLine(CIwSVec2((int)x1,(int)y1),CIwSVec2((int)x2,(int)y2));
}
void fracture(double ax,double ay,double bx,double by,int k) {
double bxax=(bx-ax)/5;
double byay=(by-ay)/4;
double cx=ax+bxax;
double cy=ay+byay;
double dx=bx-bxax;
double dy=by-byay;
double dsv=sqrt(bxax*bxax+byay*byay);
double ndc=atan2(dy-cy,dx-cx+0.01);
double ex,ey;
if (IwRand()%2) {
ex=cx+cos(-M_PI/3+ndc)*dsv;
ey=cy+sin(-M_PI/3+ndc)*dsv;
} else {
ex=cx+cos(M_PI/3+ndc)*dsv;
ey=cy+sin(M_PI/3+ndc)*dsv;
}
if (k>1) {
fracture (ax,ay,cx,cy,k-1);
fracture (cx,cy,ex,ey,k-1);
fracture (ex,ey,dx,dy,k-1);
fracture (dx,dy,bx,by,k-1);
} else {
line (ax,ay,cx,cy);
line (cx,cy,ex,ey);
line (ex,ey,dx,dy);
line (dx,dy,bx,by);
}
}
Iw2DDrawLine(CIwSVec2((int)x1,(int)y1),CIwSVec2((int)x2,(int)y2));
}
void fracture(double ax,double ay,double bx,double by,int k) {
double bxax=(bx-ax)/5;
double byay=(by-ay)/4;
double cx=ax+bxax;
double cy=ay+byay;
double dx=bx-bxax;
double dy=by-byay;
double dsv=sqrt(bxax*bxax+byay*byay);
double ndc=atan2(dy-cy,dx-cx+0.01);
double ex,ey;
if (IwRand()%2) {
ex=cx+cos(-M_PI/3+ndc)*dsv;
ey=cy+sin(-M_PI/3+ndc)*dsv;
} else {
ex=cx+cos(M_PI/3+ndc)*dsv;
ey=cy+sin(M_PI/3+ndc)*dsv;
}
if (k>1) {
fracture (ax,ay,cx,cy,k-1);
fracture (cx,cy,ex,ey,k-1);
fracture (ex,ey,dx,dy,k-1);
fracture (dx,dy,bx,by,k-1);
} else {
line (ax,ay,cx,cy);
line (cx,cy,ex,ey);
line (ex,ey,dx,dy);
line (dx,dy,bx,by);
}
}
Comments