#include "myWin.h" #include "math.h" #define BWIDTH 1000 #define BHEIGHT 1000 #define MaxAng 60 #define MaxDsp 90 #define PI 3.14159265358979 static HDC hBuff; static HBITMAP hBM; void drawLine(double X1, double Y1, double X2, double Y2, int width, unsigned int C) { HPEN hpen=CreatePen(PS_SOLID,width,C); SelectObject(hBuff, hpen); MoveToEx(hBuff,(int)(X1+0.5), (int)(Y1+0.5), NULL); LineTo (hBuff,(int) (X2+0.5), (int)(Y2+0.5)); DeleteObject(hpen); } void clearBitmap(int width, int height){ SelectObject(hBuff,GetStockObject(NULL_PEN)); PatBlt(hBuff,0,0,width,height,BLACKNESS); } // ビットマップ作成とメモリデバイスコンテキスト作成 void initBitMap(HWND hw, int width, int height){ HDC hdc=GetDC(hw); hBM=CreateCompatibleBitmap(hdc,width,height); hBuff=CreateCompatibleDC(hdc);SelectObject(hBuff,hBM); ReleaseDC(hw, hdc); } static double curX, curY, Theta; int CL, LW; void t_pos(double X, double Y){ curX = X; curY = Y;} void t_deg (double T){ Theta = T;} void t_turn(double T){ Theta += T;} void setColor(int ID){ if(ID>60) CL=RGB(8*(ID-60),4*(ID-60)+125, 8*(ID-60)); else if(ID>30) CL=RGB(8*(60-ID),0,8*(30-ID)); else CL=RGB(8*(30-ID)+255,0,4*(ID-60)+125); } void t_forward(double length){ double TH = Theta * PI / 180; double X = curX+length*cos(TH); double Y = curY+length*sin(TH); drawLine((int)(curX), (int)(200 - curY), int(X), int(200 - Y), LW, CL); curX=X; curY=Y; } void koch(int n, double TH, double length){ if(n<=0) { t_forward(length); return; } double R1,R2; int nn=n -1;double len=length/3; R1=len; R2=len*(1-2*cos(TH*PI/180)); if(R2<0){ R2=0; R1=len/(2*cos(TH*PI/180)); } koch(nn,TH,len);t_turn( TH); koch(nn,TH,R1);t_turn(-TH); //if(R2>0) koch(nn,TH,R2); koch(nn,TH,R2);t_turn(-TH); koch(nn,TH,R1); t_turn(TH); koch(nn,TH,len); } void extKoch(HWND hw){ LW=4; for(int i=MaxDsp;i>=0;i--){ t_pos(10,-5); t_deg(0); setColor(i); koch(7, (double)i, 400); InvalidateRect(hw,NULL,TRUE); } CL=0xFFFFFF;LW=2; t_pos(10,-5); t_deg(0); koch(5, 30, 400); CL=0x77FFFF;LW=2; t_pos(10,-5); t_deg(0); koch(5, 60, 400); } void procPaint(HWND hw, WPARAM wp, LPARAM lp){// ビットマップを画面に表示 PAINTSTRUCT ps; HDC hdc=BeginPaint(hw, &ps); BitBlt(hdc,0,0,BWIDTH,BHEIGHT,hBuff,0,0,SRCCOPY); EndPaint(hw,&ps); } void procCreate(HWND hw, WPARAM wp,LPARAM lp){ SetWindowText(hw,TEXT("拡張コッホ曲線")); MoveWindow(hw,0,0,440,260,TRUE); initBitMap(hw,BWIDTH, BHEIGHT); clearBitmap(BWIDTH, BHEIGHT); extKoch(hw); //t_pos(0,0); t_deg(0); koch(5, 200); InvalidateRect(hw,NULL,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); }