集装箱优化设计代码.txt
资源名称:container.rar [点击查看]
上传用户:yy2793056
上传日期:2021-10-30
资源大小:1k
文件大小:4k
源码类别:
分形几何
开发平台:
Visual C++
- #include<stdio.h>
- #include<string.h>
- #include<conio.h>
- #include<iostream.h>
- void CDcmoView::OnDraw(CDC* pDC)
- {
- CDcmoDoc* pDoc=GetDocument();
- ASSERT_VALID(pDoc);
- //TODO:add draw code for native data
- //绘制集装箱平面区域
- Cpen pen1(PS_SOLID,1,RGB(0,0,0)),pen2(PS_SOLID,1,RGB(255,0,0)),*pOldPen;
- pOldpen=pDC->SelectObject(&pen1);
- pDC->Rectangel(10,10,610,410);
- pDC->TextOut(630,50,"集装箱区域大小:");
- pDC->TextOut(630,70,"600","400");
- CBrush br.*pOldBrush;
- br.CreateSolidBrush(RGB(128,0,128));
- PoldBrushupDC->SelectObject(&br);
- //用户点击绘图
- if(CDcmoView::i==1)
- {
- pOldpen=pDC->SelectObject(&pen2);
- //先尽量排列正方形
- double M=(600/ctrldlg.m_square);
- double N=(400/ctrldlg.m_square);
- int mm,nn;
- for(mm=0;mm<(int)M;++mm)
- {
- for(nn=0;nn<(int)N;++nn)
- {
- pDC->Rectangle(10+mm*ctrldlg.m_Square,10+nn*ctrldlg.m_Square,10+(mm+1)*ctrldlg.m_Square,10+(nn+1)*ctrldlg.m_Square);
- }
- }
- //在剩下的空间里再尽量排列三角形
- CPoint pts[3],ptt[3];
- int countTriangle=0;
- double pTmp=(this->ctrldlg.m_Tri2+this->ctrldlg.m_Tri3)/2;
- double Area=sqrt(pTmp*(pTmp-this->ctrldlg.m_Tri1)*(pTmp-this->ctrldlg.m_Tri2)*(pTmp-this->ctrldlg.m_Tri3));
- double h1=Area*2/this->ctrldlg.m_Tri1;
- double h2=Area*2/this->ctrldlg.m_Tri2;
- double h3=Area*2/this->ctrldlg.m_Tri3;
- double xLeave=600-this->ctrldlg.m_Square*(int)(M);
- double yLeave=400-this->ctrldlg.m_Square*(int)(N);
- double vv1=xLeave-(int)(xLeave/h1)*h1;
- double vv2=xLeave-(int)(xLeave/h2)*h2;
- double vv3=xLeave-(int)(xLeave/h3)*h3;
- double vv0=vv1<=vv2?vv1:vv2;
- vv0=vv0<=vv3?vv0:vv3;
- double LL,hh,MM,NN,BB;
- if(vv0==vv1)
- {
- hh=h1;
- LL=this->ctrldlg.m_Tri1;
- BB=ctrldlg.m_Tri2<ctrldlg.m_Tri3?ctrldlg.m_Tri2:ctrldlg.m_Tri3;
- }
- if(vv0==vv2)
- {
- hh=h2;
- LL=this->ctrldlg.m_Tri2;
- BB=ctrldlg.m_Tri1<ctrldlg.m_Tri3?ctrldlg.m_Tri1:ctrldlg.m_Tri3;
- }
- if(vv0==vv3)
- {
- hh=h3;
- LL=this->ctrldlg.m_Tri3;
- BB=ctrldlg.m_Tri1<ctrldlg.m_Tri2?ctrldlg.m_Tri1:ctrldlg.m_Tri2;
- }
- MM=xLeave/hh;
- NN=400/LL;
- double xx,yy,zz;
- xx=10+(int)M*this->ctrldlg.m_Square;
- yy=10;
- zz=sqrt(BB*BB-hh*hh);
- //正方形填充区右面区域
- for(mm=0;mm<(int)MM;++mm)
- {
- yy=10;
- for(nn=0;nn<(int)NN;++nn)
- {
- pts[0]=cpoint(xx,yy);
- pts[1]=cpoint(xx,yy+LL);
- pts[2]=cpoint(xx+hh,yy+zz);
- ptt[0]=cpoint(xx+hh,yy+zz+LL);
- ptt[1]=pts[1];
- ptt[2]=pts[2];
- {
- pDC->Polygon(pts.3);
- counTriangle++;
- }
- if(yy+zz+LL<=410.0)
- {
- pDC->Polygon(ptt.3);
- countTriangle++;
- }
- yy=yy+LL;
- }
- xx=xx+hh;
- }
- //正方形填充区下方区域
- vv1=yLeave-(int)(yLeave/h1)*h1;
- vv2=yLeave-(int)(yLeave/h2)*h2;
- vv3=yLeave-(int)(yLeave/h3)*h3;
- vv0=vv1<=vv2?vv1:vv2;
- vv0=vv0<=vv3?vv0:vv3;
- if(vv0==vv1)
- {
- hh=h1;
- LL=this->ctrldlg.m_Tri1;
- BB=ctrldlg.m_Tri2<ctrldlg.m_Tri3?ctrldlg.m_Tri2:ctrldlg.m_Tri3;
- }
- if(vv0==vv2)
- {
- hh=h2;
- LL=this->ctrldlg.m_Tri2;
- BB=ctrldlg.m_Tri1<ctrldlg.m_Tri3?ctrldlg.m_Tri1:ctrldlg.m_Tri3;
- }
- if(vv0==vv3)
- {
- hh=h3;
- LL=this->ctrldlg.m_Tri3;
- BB=ctrldlg.m_Tri1<ctrldlg.m_Tri2?ctrldlg.m_Tri1:ctrldlg.m_Tri2;
- }
- MM=yLeave/hh;
- NN=ctrldlg.m_Square*(int)M/LL;
- xx=10;
- yy=10+ctrldlg.m_Square*(int)N;
- zz=sqrt(BB*BB-hh*hh);
- for(mm=0;mm<(int)MM;++mm)
- {
- xx=10;
- for(nn=0;nn<(int)NN;++nn)
- {
- pts[0]=Cpoint(xx,yy);
- pts[1]=Cpoint(xx+LL,yy);
- pts[2]=Cpoint(xx+zz,yy+hh);
- ptt[0]=Cpoint(xx+zz+LL,yy+hh);
- ptt[1]=pts[1];
- ptt[2]=pts[2];
- {
- pDC->Polygon(pts.3);
- counTriangle++;
- }
- if(xx+zz+LL<=ctrldlg.m_Square*(int)M+10.0)
- {
- pDC->Polygon(ptt.3);
- countTriangle++;
- }
- xx=xx+LL;
- }
- yy=yy+hh;
- }
- double LYL; pDC->selectobject(pOldpen);
- CString str;
- str.Format("正方形:%d"个,(int)M*(int)N);
- pDC->TextOut(630,150);
- str.Format("三角形:%d"个,countTriangle);
- pDC->TextOut(630,180);
- LYL=((int)M*(int)N*ctrldlg.m_square+countTriangle*Area)/(600*400);
- str.Format("利用率:%f",LYL);
- pDC->TextOut(630,210,str);
- }
- pDC->SelectObject(pOldpen);
- pDC->SelectObject(pOldBrush);
- pen1.DeleteObject();
- pen2.DeleteObject();
- br.DeleteObject();
- }