Koch分形图绘制程序源代码.docx
- 文档编号:4944473
- 上传时间:2022-12-12
- 格式:DOCX
- 页数:25
- 大小:215.25KB
Koch分形图绘制程序源代码.docx
《Koch分形图绘制程序源代码.docx》由会员分享,可在线阅读,更多相关《Koch分形图绘制程序源代码.docx(25页珍藏版)》请在冰豆网上搜索。
Koch分形图绘制程序源代码
//winmain.cpp
/*********************************************\
**
*Koch分形-by柳少锋*
*2010/11/10*
**
\*********************************************/
#include
#include
#include"resource.h"
#include"SettingDlg.h"
#defineCRBKRGB(150,150,255)
#definePI3.14159265
classCPtVec
{
public:
CPtVec():
max_size(512),size(0){ptBuffer=newPOINT[max_size];}
~CPtVec(){deleteptBuffer;}
boolPushBack(POINTpt);
boolInsert(POINTpt,inti);
boolInsert(PPOINTpt,inti,intk);
boolDelete(inti);
boolDelete(inti,intk);
voidEmpty(){delete[]ptBuffer;max_size=512;size=0;ptBuffer=newPOINT[max_size];}
intFind(POINT&pt);
PPOINTGetBuf(){returnptBuffer;}
intGetSize(){returnsize;}
POINT&operator[](inti){returnptBuffer[i];}
private:
boolReset();
PPOINTptBuffer;
intmax_size;
intsize;
};
//定义FILL为0为无填充分形,为1则为有填充分形
#defineFILL1
LRESULTWINAPIWndProc(HWNDhwnd,UINTuMsg,WPARAMwParam,LPARAMlParam);
voidFract(CPtVec&ptVec,intk);
boolSnow(HDChdc,POINTpt1,POINTpt2);
voidSnow(HDChdc,CPtVec&ptVec);
wchar_tszAppName[]=TEXT("Koch雪花-by漂泊的风");
HINSTANCEhInst;
HWNDhwndApp;
HBRUSHhBkBrush;
intWINAPIWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,
LPSTRlpCmdLine,intnShowCmd)
{
hInst=hInstance;
hBkBrush=CreateSolidBrush(CRBK);
WNDCLASSwc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hbrBackground=hBkBrush;
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hIcon=LoadIcon(hInst,szAppName);
wc.hInstance=hInst;
wc.lpfnWndProc=WndProc;
wc.lpszClassName=(LPCWSTR)szAppName;
wc.lpszMenuName=NULL;
wc.style=CS_HREDRAW|CS_VREDRAW|CS_OWNDC;
RegisterClass(&wc);
intcx=GetSystemMetrics(SM_CXFULLSCREEN);
intcy=GetSystemMetrics(SM_CYFULLSCREEN);
if(cy>cx)
cy=cx;
else
cx=cy;
hwndApp=CreateWindow((LPCWSTR)szAppName,(LPCWSTR)szAppName,
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
200,0,cx,cy,
NULL,NULL,
hInst,NULL);
HACCELhAccel=LoadAccelerators(hInst,(LPCWSTR)TEXT("accel"));
MSGmsg;
while(GetMessage(&msg,NULL,0,0))
{
if(!
TranslateAccelerator(hwndApp,hAccel,&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
DeleteObject(hBkBrush);
returnmsg.wParam;
}
#defineIDM_SETTING111
#defineWM_REFRACT(WM_USER+10)
#defineWM_SETCOLOR(WM_USER+11)
#defineWM_DRAW(WM_USER+12)
#defineWM_SETFILL(WM_USER+13)
LRESULTWINAPIWndProc(HWNDhwnd,UINTuMsg,WPARAMwParam,LPARAMlParam)
{
staticPOINTpt[3];
staticCPtVecptVec;
staticintcxClient,cyClient,cxImage,cyImage;
staticintxOrg=0,yOrg=0;
staticCOLORREFcrEdge=RGB(255,0,0),crFill=RGB(255,120,0);
staticRECTselRect;
staticBOOLbLBtnDown;
staticboolbFill,bFlag;
HBRUSHhBrush;
HDChdc;
HPENhPen;
switch(uMsg)
{
caseWM_CREATE:
HMENUhSysMenu;
hSysMenu=GetSystemMenu(hwnd,FALSE);
AppendMenu(hSysMenu,MF_STRING,IDM_SETTING,(LPCWSTR)TEXT("设置颜色\tS"));
RECTrc;
GetClientRect(hwnd,&rc);
cxImage=rc.right;
cyImage=rc.bottom;
SendMessage(hwnd,WM_REFRACT,0,0);
bFill=true;
return0;
caseWM_SETFILL:
bFill=wParam;
bFlag=lParam;
return0;
caseWM_SETCOLOR:
crEdge=(COLORREF)wParam;
crFill=(COLORREF)lParam;
SendMessage(hwnd,WM_PAINT,0,0);
UpdateWindow(hwnd);
return0;
caseWM_REFRACT:
doubleL;
if(cxImage>cyImage)
L=cyImage;
else
L=cxImage;
L/=2.0;L-=2;
#defineSTEPPI*2.0/3.0
inti;i=0;
doubleang;ang=PI/6.0;
while(i<3)
{
pt[i].x=cxImage/2+L*cos(ang);
pt[i].y=cyImage/2+L*sin(ang);
i++;
ang+=STEP;
}
#undefSTEP
ptVec.Empty();
ptVec.PushBack(pt[0]);
ptVec.PushBack(pt[2]);
ptVec.PushBack(pt[1]);
ptVec.PushBack(pt[0]);
intk;
Fract(ptVec,0);
k=ptVec.Find(pt[2]);
Fract(ptVec,k);
k=ptVec.Find(pt[1]);
Fract(ptVec,k);
return0;
caseWM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
return0;
caseWM_RBUTTONUP:
if(cxImage!
=cxClient&&cyImage!
=cyClient)
{
cxImage=cxClient;
cyImage=cyClient;
xOrg=0;
yOrg=0;
SendMessage(hwnd,WM_REFRACT,0,0);
InvalidateRect(hwnd,NULL,TRUE);
}
return0;
caseWM_LBUTTONDOWN:
SetCapture(hwnd);
bLBtnDown=TRUE;
selRect.right=selRect.left=LOWORD(lParam);
selRect.bottom=selRect.top=HIWORD(lParam);
return0;
caseWM_MOUSEMOVE:
if(bLBtnDown)
{
hdc=GetDC(hwnd);
HBRUSHhbrush;
hbrush=(HBRUSH)SelectObject(hdc,GetStockObject(NULL_BRUSH));
SetViewportOrgEx(hdc,0,0,NULL);
intiDrawMode;
iDrawMode=SetROP2(hdc,R2_NOT);
if(selRect.right!
=selRect.left&&selRect.bottom!
=selRect.top)
{
Rectangle(hdc,selRect.left,selRect.top,selRect.right,selRect.bottom);
}
doublexyRate=(double)cxClient/(double)cyClient;
intcx,cy;
cx=LOWORD(lParam)-selRect.left;
cy=HIWORD(lParam)-selRect.top;
if(double(abs(cx))/double(abs(cy))>xyRate)
{
selRect.right=selRect.left+cx;
selRect.bottom=selRect.top+cx/xyRate;
}
else
{
selRect.right=selRect.left+cy*xyRate;
selRect.bottom=selRect.top+cy;
}
if(selRect.right!
=selRect.left&&selRect.bottom!
=selRect.top)
{
Rectangle(hdc,selRect.left,selRect.top,selRect.right,selRect.bottom);
}
SetROP2(hdc,iDrawMode);
SelectObject(hdc,hbrush);
SetViewportOrgEx(hdc,xOrg,yOrg,NULL);
ReleaseDC(hwnd,hdc);
}
return0;
caseWM_LBUTTONUP:
if(bLBtnDown)
{
ReleaseCapture();
bLBtnDown=FALSE;
if(selRect.right==selRect.left||selRect.bottom==selRect.top)
return0;
doublexAmp,yAmp;
xAmp=(double)cxClient/(double)(selRect.right-selRect.left);
yAmp=(double)cyClient/(double)(selRect.bottom-selRect.top);
if(xAmp xAmp=yAmp; else yAmp=xAmp; #defineMAX_AMP20 xOrg+=selRect.left; yOrg+=selRect.top; if(cxImage { cxImage=(double)cxImage*xAmp; cyImage=(double)cyImage*yAmp; xOrg*=xAmp; yOrg*=yAmp; SendMessage(hwnd,WM_REFRACT,0,0); } InvalidateRect(hwnd,NULL,TRUE); } return0; caseWM_PAINT: PAINTSTRUCTps; hdc=BeginPaint(hwnd,&ps); bFlag=false; SendMessage(hwnd,WM_DRAW,crEdge,crFill); EndPaint(hwnd,&ps); return0; caseWM_DRAW: hdc=GetDC(hwnd); if(bFlag&&! bFill) { SetViewportOrgEx(hdc,0,0,NULL); SelectObject(hdc,GetStockObject(NULL_PEN)); SelectObject(hdc,hBkBrush); Rectangle(hdc,0,0,cxClient,cyClient); } SetViewportOrgEx(hdc,-xOrg,-yOrg,NULL); hPen=CreatePen(PS_SOLID,1,wParam); hBrush=CreateSolidBrush(lParam); SelectObject(hdc,hPen); SelectObject(hdc,hBrush); if(! bFill) { Snow(hdc,pt[2],pt[1]); Snow(hdc,pt[0],pt[2]); Snow(hdc,pt[1],pt[0]);//无填充分形 } else Snow(hdc,ptVec);//有填充分形 ReleaseDC(hwnd,hdc); DeleteObject(hPen); DeleteObject(hBrush); return0; caseWM_SYSCOMMAND: switch(LOWORD(wParam)) { caseIDM_SETTING: CSettingDlgsdlg(crEdge,crFill,bFill); if(IDOK==sdlg.DoModal(hwnd)) { crEdge=sdlg.GetCrEdge(); crFill=sdlg.GetCrFill(); bFill=sdlg.bFill; } elseif(crEdge! =sdlg.GetCrEdge()||crFill! =sdlg.GetCrFill()||bFill! =sdlg.bFill) InvalidateRect(hwnd,NULL,FALSE); return0; } break; caseWM_DESTROY: PostQuitMessage(0); return0; } returnDefWindowProc(hwnd,uMsg,wParam,lParam); } boolCPtVec: : PushBack(POINTpt) { if(size>=max_size) { if(! Reset()) returnfalse; } ptBuffer[size].x=pt.x; ptBuffer[size++].y=pt.y; returntrue; } boolCPtVec: : Insert(POINTpt,inti) { if(size>=max_size) { if(! Reset()) returnfalse; } for(intj=size;j>i;j++) { ptBuffer[j].x=ptBuffer[j-1].x; ptBuffer[j].y=ptBuffer[j-1].y; } ptBuffer[i].x=pt.x; ptBuffer[i].y=pt.y; size++; returntrue; } boolCPtVec: : Insert(PPOINTpt,inti,intk) { intnewSize=size+k; while(newSize>=max_size) if(! Reset()) returnfalse; intj; for(j=newSize-1;j>=i+k;j--) { ptBuffer[j].x=ptBuffer[j-k].x; ptBuffer[j].y=ptBuffer[j-k].y; } while(j>=i) { ptBuffer[j]=pt[j-i]; j--; } size=newSize; returntrue; } boolCPtVec: : Reset() { max_size<<=1; if(max_size<0) returnfalse; PPOINTptNew=newPOINT[max_size]; if(! ptNew) returnfalse; for(inti=0;i { ptNew[i].x=ptBuffer[i].x; ptNew[i].y=ptBuffer[i].y; } delete[]ptBuffer; ptBuffer=ptNew; returntrue; } boolCPtVec: : Delete(inti) { returnDelete(i,1); } boolCPtVec: : Delete(inti,intk) { if(i+k>size) returnfalse; intj; for(j=i;j { ptBuffer[j].x=ptBuffer[j+k].x; ptBuffer[j].y=ptBuffer[j+k].y; } size-=k; returntrue; } intCPtVec: : Find(POINT&pt) { inti; for(i=size-1;i>=0;i--) { if(pt.x==ptBuffer[i].x&&pt.y==ptBuffer[i].y) break; } returni; } voidFract(CPtVec&ptVec,intk) { POINT&pt1=ptVec[k]; POINT&pt2=ptVec[k+1]; intcx=pt2.x-pt1.x; intcy=pt2.y-pt1.y; intLen=sqrt(double(cx*cx+cy*cy)); if(Len<3) return; POINTpt[3]; pt[0].x=pt1.x+cx/3;pt[0].y=pt1.y+cy/3; pt[2].x=pt[0].x+cx/3;pt[2].y=pt[0].y+cy/3; doubleang; if(cx) { ang=atan(double(cy)/double(cx)); if(cx<0) ang+=PI; } elseif(cy>0) ang=PI/2; else ang=-PI/2; ang+=PI/3; Len/=3; pt[1].x=pt[0].x+(int)Len*cos(ang); pt[1].y=pt[0].y+(int)Len*sin(ang); ptVec.Insert(pt,k+1,3); Fract(ptVec,k); k=ptVec.Find(pt[0
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Koch 分形图 绘制 程序 源代码
![提示](https://static.bdocx.com/images/bang_tan.gif)