#include "myWin.h" #include "math.h" #define BITMX 550 #define BITMY 450 static HDC hbuf; static HBITMAP hBM; void clearBitmap(int width, int height){ SelectObject(hbuf,GetStockObject(NULL_PEN)); PatBlt(hbuf,0,0,width,height,WHITENESS); } void initBitmap(HWND hw, int width, int height){ HDC hdc=GetDC(hw); hBM=CreateCompatibleBitmap(hdc,width,height); hbuf=CreateCompatibleDC(hdc);SelectObject(hbuf,hBM); ReleaseDC(hw, hdc); } double Gamma(double r){ // ガンマ関数 if(abs(r) < 0.00001) return 1.7E+308; // r=0 のとき発散 if(r < 2 ){ // r<2のとき計算式を変える int n = 1000; double G = pow((double)n, r) / r; for(int k = 1; k<=n; k++) G *= (double)k / (r + (double)k); return G; } double DX = 0.1, s = 0,X1, X2 = 0, Y1, Y2, E1, E2 = exp(-X2); while(E2 > 0.000000000001){ //定義による積分 E1 = E2; X1 = X2; X2 = X2 + DX; E2 = exp(-X2); Y1 = 0; if(X1 > 0.001 || r > 1 ) Y1 = E1 * pow(X1 , (r - 1)); Y2 = E2 * pow(X2, (r - 1)); s += (Y1 + Y2) * DX / 2; } return s; } void dspText(int X, int Y, TCHAR str[]){ HFONT hf=CreateFont(8,0,0,0,FW_NORMAL,FALSE, FALSE, FALSE, SHIFTJIS_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH|FF_MODERN, NULL); SelectObject(hbuf,hf); SetTextColor(hbuf,0); SetBkMode(hbuf,TRANSPARENT); TextOut(hbuf,X, Y ,str,lstrlen(str)); } double XST=20, YST=350,SCX=100, SCY=10; void moveTo(double X1, double Y1){ MoveToEx(hbuf,(int)(XST + X1 *SCX+0.5),(int)(YST - Y1 *SCY+0.5),NULL); } void lineTo(double X2, double Y2,int width, COLORREF C){ HPEN hpen=CreatePen(PS_SOLID,width, C); SelectObject(hbuf,hpen); LineTo (hbuf,(int)(XST + X2 *SCX+0.5),(int)(YST - Y2 *SCY+0.5)); DeleteObject(hpen); } void drawLine(double X1, double Y1, double X2, double Y2,int width, COLORREF C){ moveTo(X1,Y1);lineTo(X2,Y2,width,C); } void drawFrame(){ int C;TCHAR str[256]; for(int y=0;y<=30; y++){ C=0xAAAAAA; if(y%5==0) { C=0; wsprintf(str, TEXT("%2d"), y); dspText(8,(int)(YST-SCY*y-4),str); } drawLine(0.0, (double)y, 5, (double)y, 1, C); } double x=0; for(int i=0; i<=50; i++, x+=0.1){ C=0xAAAAAA; if(i%5==0) { C=0; if(i%10==0){ wsprintf(str, TEXT("%2d"), i/10); dspText((int)(XST+x*SCX-4),(int)YST+4,str); } } drawLine(x, 0, x, 30, 1, C); } } void drawGamma(){ moveTo(0.04,Gamma(0.04)); for(double r=0.05;r<=5; r+=0.01)lineTo(r, Gamma(r),2,0xFF); } void procPaint(HWND hw, WPARAM wp,LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw, &ps); BitBlt(hdc,0,0,BITMX,BITMY,hbuf,0,0,SRCCOPY); EndPaint(hw,&ps); } void procCreate(HWND hw, WPARAM wp,LPARAM lp){ SetWindowText(hw,TEXT("Γ関数のグラフを描く")); MoveWindow(hw,0,0,BITMX,BITMY,TRUE); initBitmap(hw,BITMX,BITMY); clearBitmap(BITMX,BITMY); drawFrame();drawGamma(); 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); }