#include "myWin.h" #include "math.h" #define ORDER 5 // 次数 #define INIT_H 5 // Hの初期値 #define SX 20 // 画面左上端座標(XY共通とする) #define RSC 2 // 画面表示倍率の逆数 int cX, cY, ID, H; void pos(int X, int Y){ cX = X; cY = Y;} int ScXY(int X){ return SX + X/RSC; } void rLine(HDC hdc, int X, int Y){//相対描画 int nX = cX + X, nY = cY + Y; MoveToEx(hdc, ScXY(cX), ScXY(cY), NULL); LineTo (hdc, ScXY(nX), ScXY(nY)); cX=nX; cY=nY; } void URD(HDC hdc); void LUR(HDC hdc); void DLU(HDC hdc); void RDL(HDC hdc){ if(ID<=0) return; ID--; RDL(hdc); rLine(hdc, H, -H); URD(hdc); rLine(hdc, 0, -2*H); DLU(hdc); rLine(hdc, -H, -H); RDL(hdc); ID++; } void DLU(HDC hdc){ if(ID<=0) return; ID--; DLU(hdc); rLine(hdc, -H, -H); RDL(hdc); rLine(hdc, -2*H, 0); LUR(hdc); rLine(hdc, -H, H); DLU(hdc); ID++; } void LUR(HDC hdc){ if(ID<=0) return; ID--; LUR(hdc); rLine(hdc, -H, H); DLU(hdc); rLine(hdc, 0, 2*H); URD(hdc); rLine(hdc, H, H); LUR(hdc); ID++; } void URD(HDC hdc){ if(ID<=0) return; ID--; URD(hdc); rLine(hdc, H, H); LUR(hdc); rLine(hdc, 2*H, 0); RDL(hdc); rLine(hdc, H, -H); URD(hdc); ID++; } void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw, &ps); ID = ORDER; H = INIT_H; pos(H,0); URD(hdc); rLine(hdc, H, H); LUR(hdc); rLine(hdc,-H, H); DLU(hdc); rLine(hdc,-H,-H); RDL(hdc); rLine(hdc, H,-H); EndPaint(hw,&ps); } void procCreate(HWND hw, WPARAM wp,LPARAM lp){ SetWindowText(hw,TEXT("シェルピンスキ曲線(Sierpinski Curve)")); MoveWindow(hw,0,0,380,400,TRUE); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ; return 0; case WM_CREATE : procCreate(hw,wp,lp) ; return 0; case WM_PAINT : procPaint(hw,wp,lp) ; return 0; } return DefWindowProc(hw,msg,wp,lp); }