level 5
void floodfill_3d(pt_3d * pt1,COLORREF color){
floodfill(cent_x+pt1->y*sqrt(3.0)/2-pt1->x*sqrt(3.0)/2,cent_y-pt1->z+pt1->y/2+pt1->x/2,color);
}
pt_3d operator+(pt_3d a,pt_3d b){
pt_3d c;
c.x=a.x+b.x;
c.y=a.y+b.y;
c.z=a.z+b.z;
return c;
}
pt_3d operator-(pt_3d a,pt_3d b){
pt_3d c;
c.x=a.x-b.x;
c.y=a.y-b.y;
c.z=a.z-b.z;
return c;
}
pt_3d operator/(pt_3d a,float b){
pt_3d c;
c.x=a.x/b;
c.y=a.y/b;
c.z=a.z/b;
return c;
}
pt_3d operator*(pt_3d a,float b){
pt_3d c;
c.x=a.x*b;
c.y=a.y*b;
c.z=a.z*b;
return c;
}
class cube{
public:
pt_3d **pt_8;
COLORREF face[6];//6面色彩
int index,index2;//不可见点
LINESTYLE *pstyle_line;
cube(pt_3d &pt1,pt_3d & pt2,pt_3d &pt3,pt_3d &pt4){//传输参数为一个面的3点和另一个面1个点
this->pt_8=new pt_3d *[8];
//定义3个向量求
pt_3d a=new pt_3d(0,0,0);
a=pt3-pt1;
pt_3d b=new pt_3d(0,0,0);
b=pt4-pt1;
this->pt_8[0]=new pt_3d(pt1);
this->pt_8[1]=new pt_3d(pt2);
this->pt_8[2]=new pt_3d(pt2);
*(this->pt_8[2])=*(this->pt_8[2])+a;
this->pt_8[3]=new pt_3d(pt3);
for (int i=0;i<4;i++)
{
this->pt_8[i+4]=new pt_3d(this->pt_8[i]);
*(this->pt_8[i+4])=*(this->pt_8[i+4])+b;
}
index=0;
face[0]=WHITE;face[1]=ORANGE;face[2]=BLUE;
face[3]=RED;face[4]=GREEN;face[5]=YELLOW;
this->pstyle_line=new LINESTYLE();
this->pstyle_line->style=PS_SOLID;
this->pstyle_line->thickness=3;
float all=0,tem;
for (int i=0;i<8;i++)
{
tem=pt_8[i]->x+pt_8[i]->y+pt_8[i]->z;
if (tem>all)
{
all=tem;
index2=i;
}
}
index=(index2+4)%8;
index=(index+2)%4+(int)(index/4)*4;
}
void draw_line(int ok){
setlinestyle(pstyle_line);
if (ok)
setlinecolor(LINE_COLOR);
else
setlinecolor(BACK_COLOR);
//setrop2(R2_XORPEN);
for (int i=0;i<4;i++)
{
if(i!=index&&(1+i)%4!=index)
line_3d(pt_8[0+i],pt_8[(1+i)%4]);
if(4+i!=index&&(1+i)%4+4!=index)
line_3d(pt_8[4+i],pt_8[(1+i)%4+4]);
if(i!=index&&4+i!=index)
line_3d(pt_8[0+i],pt_8[4+i]);
}
}
void draw_face(int ok){
int top_index=(index+4)%8;
top_index=(top_index+2)%4+(int)(top_index/4)*4;//求得距离视角最近的点
int p=0;//位记录需要涂的面
pt_3d tmp_pt;
switch(top_index)
{
case 0:
p|=0x13;break;
case 1:
p|=0x07;break;
case 2:
p|=0x0d;break;
case 3:
p|=0x19;break;
case 4:
p|=0x32;break;
case 5:
p|=0x26;break;
case 6:
p|=0x2c;break;
case 7:
p|=0x38;break;
default:return;
break;
}
/* 六个面对角
0--2 1--3
0--5 1--4
1--6 2--5
2--7 3--6
0--7 3--4
4--6 5--7
*/
if ((p&1)==1)
{
tmp_pt=*(this->pt_8[2])+*(this->pt_8[0]);
tmp_pt=tmp_pt/2;
if(ok)
setfillcolor(face[0]);
else
setfillcolor(BACK_COLOR);
floodfill_3d(&tmp_pt,LINE_COLOR);
}
if ((p&2)==2)
{
tmp_pt=*(this->pt_8[5])+*(this->pt_8[0]);
tmp_pt=tmp_pt/2;
if(ok)
setfillcolor(face[1]);
else
setfillcolor(BACK_COLOR);
floodfill_3d(&tmp_pt,LINE_COLOR);
}
if ((p&4)==4)
{
tmp_pt=*(this->pt_8[1])+*(this->pt_8[6]);
tmp_pt=tmp_pt/2;
if(ok)
setfillcolor(face[2]);
else
setfillcolor(BACK_COLOR);
floodfill_3d(&tmp_pt,LINE_COLOR);
}
if ((p&8)==8)
{
tmp_pt=*(this->pt_8[2])+*(this->pt_8[7]);
tmp_pt=tmp_pt/2;
if(ok)
setfillcolor(face[3]);
else
setfillcolor(BACK_COLOR);
floodfill_3d(&tmp_pt,LINE_COLOR);
}
if ((p&16)==16)
{
tmp_pt=*(this->pt_8[7])+*(this->pt_8[0]);
tmp_pt=tmp_pt/2;
if(ok)
setfillcolor(face[4]);
else
setfillcolor(BACK_COLOR);
floodfill_3d(&tmp_pt,LINE_COLOR);
}
if ((p&32)==32)
{
tmp_pt=*(this->pt_8[4])+*(this->pt_8[6]);
tmp_pt=tmp_pt/2;
if(ok)
setfillcolor(face[5]);
else
setfillcolor(BACK_COLOR);
floodfill_3d(&tmp_pt,LINE_COLOR);
}
}
void draw_circle(int radius,COLORREF color)
{
setfillcolor(color);
for(int i=0;i<8;i++)
{
solidcircle_3d(pt_8[i],radius,color);
}
}
void rotate(float rx,float ry,float rz){
for (int i=0;i<8;i++)
{
pt_8[i]->rotate(rx,ry,rz);
}
float all=0,tem;
for (int i=0;i<8;i++)
{
tem=pt_8[i]->x+pt_8[i]->y+pt_8[i]->z;
if (tem>all)
{
all=tem;
index2=i;
}
}
index=(index2+4)%8;
index=(index+2)%4+(int)(index/4)*4;
}
void size(float f)
{
for (int i=0;i<8;i++)
{
*(this->pt_8[i])=*(this->pt_8[i])*f;
}
}
};
void axes_init()
{
setlinecolor(LINE_COLOR);
cent_x=WIDTH/2;cent_y=HEIGHT/2;
//line(cent_x,cent_y,cent_x,0);
//line(cent_x,cent_y,cent_x-cent_y/2*sqrt(3.0),HEIGHT);
//line(cent_x,cent_y,cent_x+cent_y/2*sqrt(3.0),HEIGHT);
}
2016年03月25日 17点03分



