#include "myWin.h" #include "math.h" #include "stdio.h" #include "stdlib.h" #define BITX 500 #define BITY 500 static HDC hbuf; static HBITMAP hBM; void initBitMap(HWND hw){ HDC hdc=GetDC(hw); hBM=CreateCompatibleBitmap(hdc,BITX,BITY); hbuf=CreateCompatibleDC(hdc);SelectObject(hbuf,hBM); SelectObject(hbuf,GetStockObject(NULL_PEN)); ReleaseDC(hw, hdc); } void clearBitmap(){ PatBlt(hbuf,0,0,BITX,BITY,BLACKNESS); } #define PI 3.1415926535897932385 #define SEP1(X) ((X>>16) & 0xFF) #define SEP2(X) ((X>>8) & 0xFF) #define SEP3(X) (X & 0xFF) double a, b, dt, g, dg, XS, YS, ZS; double screenX(double X, double Y){ return 5 * (X - 0.6 * Y) + 200;} double screenY(double Y, double Z){ return 350 - 5 * (Z - 0.6 * Y);} void drawLine(int X1, int Y1, int X2, int Y2, COLORREF C){ HPEN p=CreatePen(PS_SOLID,1,C);SelectObject(hbuf, p); MoveToEx(hbuf,X1,Y1,NULL); LineTo(hbuf, X2,Y2); DeleteObject(p); } void Line3D(double X1, double Y1,double Z1, double X2, double Y2, double Z2, COLORREF C){ int XS1 = (int)(screenX(X1, Y1) + 0.5); int YS1 = (int)(screenY(Y1, Z1) + 0.5); int XS2 = (int)(screenX(X2, Y2) + 0.5); int YS2 = (int)(screenY(Y2, Z2) + 0.5); drawLine(XS1, YS1, XS2, YS2, C); } void Point3D(double X1, double Y1, double Z1, COLORREF C){ int XS1 = (int)(screenX(X1, Y1) + 0.5); int YS1 = (int)(screenY(Y1, Z1) + 0.5); SetPixel(hbuf, XS1, YS1, C); } void Coord3D(){ Line3D(-30, 0, 0, 50, 0, 0, 0xFF); Line3D(0, -30, 0, 0, 50, 0, 0x7700); Line3D(0, 0, -30, 0, 0, 50, 0x770000); } void initData(){ a = 10; b = 3; dt = 0.004 ; g = 0; dg = 0.1; XS = 30; YS = 20; ZS = 30; } /*int check=0;//check=1‚Ì‚Ĉ‚Ğ‚PŽüŠú•Ş‚ÌŒvŽZŒ‹‰Ê‚Ċ•`‰ĉ double k=0.1,B=12, DT=0.008,DB=0.01; int NSEP=800; */ void strDbl(double A, TCHAR str[]){ char tmp[256];int i; sprintf(tmp, "%7.4lf", A); for(i=0; tmp[i]!=0; i++) str[i]=tmp[i]; str[i]=0; } void strDsp(TCHAR name[], double A, TCHAR str[]){ int i; for(i=0; name[i]!=0; i++) str[i]=name[i]; strDbl(A, &str[i]); } void dspValue(){ TCHAR str[256]; TCHAR Msg[]=TEXT("‰İ—΁FXY•½–Ê‚Ö‚Ì“Š‰e,") TEXT("ÂF:XZ•½–Ê‚Ö‚Ì“Š‰e, ‰İF:3DÀ•W‚̈ʒu"); HFONT hf=CreateFont(12,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); TEXTMETRIC tm;GetTextMetrics(hbuf, &tm);int h=tm.tmHeight; SetTextColor(hbuf,0xFFFF); SetBkMode(hbuf,TRANSPARENT); strDsp(TEXT("g = "), g, str); TextOut(hbuf,20, 20 ,str,lstrlen(str)); TextOut(hbuf,20, 40 ,Msg,lstrlen(Msg)); DeleteObject(hf); } void cmpIter(){ clearBitmap(); dspValue(); double X= XS, Y = YS, Z = ZS; double XN, YN, ZN; Coord3D(); for(int i=0;i<6000;i++){ XN = X + dt * a * (Y - X); YN = Y + dt * (-X * Z + g * X - Y); ZN = Z + dt * (X * Y - b * Z); Line3D(X, Y, 0, XN, YN, 0, 0xFF00); Line3D(X, 0, Z, XN, 0, ZN, 0xFF0000); Line3D(X, Y, Z, XN, YN, ZN, 0xFFFF); X = XN; Y = YN; Z = ZN; } } void procLButtonDown(HWND hw, WPARAM wp, LPARAM lp){ cmpIter(); InvalidateRect(hw,NULL,FALSE); } void procCreate(HWND hw, WPARAM wp, LPARAM lp){ MoveWindow(hw,0,0,BITX,BITY,NULL); SetWindowText(hw,TEXT("Lorenz Model")); initBitMap(hw); initData(); SetTimer(hw,1,10,NULL); procLButtonDown(hw,wp,lp); } void procTimer(HWND hw, WPARAM wp, LPARAM lp){ if(abs(g-30)<0.00001)KillTimer(hw,1); else{ g = g + dg ; procLButtonDown(hw,wp,lp); } } void procPaint(HWND hw, WPARAM wp, LPARAM lp){ PAINTSTRUCT ps; HDC hdc=BeginPaint(hw, &ps); BitBlt(hdc,0,0,BITX,BITY,hbuf,0,0,SRCCOPY); EndPaint(hw,&ps); } 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_TIMER : procTimer(hw,wp,lp) ; return 0; case WM_LBUTTONDOWN : procLButtonDown(hw,wp,lp) ; return 0; case WM_PAINT : procPaint (hw, wp, lp); return 0; } return DefWindowProc(hw, msg, wp, lp); }