/* * 绘图 * * SetPiexl(HDC, int, int ,COLORREF); * GetPiexl(HDC, int, int); * * MoveToEx(HDC, int, int, LPPOINT); * * LineTo(HDC, int, int); 绘制直线 * * Polyline 绘制折线 * PolylineTo * * GetCurrentPositionEx(HDC, LPPOINT); * * GetClientRect(HWND, LPRECT); */ #include "stdafx.h" #include "HelloWnd.h" #include <math.h> // 绘制网格 void DrawGrid(HDC hdc) { RECT rt; GetClientRect(hwnd, &rt); for(int x=0; x < rt.right; x+=100) { MoveToEx(hdc, x, 0, NULL); LineTo(hdc, x, rt.bottom); } for(int y=0; y < rt.bottom; y+=100) { MoveToEx(hdc,0,y,NULL); LineTo(hdc, rt.right, y); } } // 绘制矩形 void DrawRectangle(HDC hdc) { POINT apt[5] = {100, 100, 200, 100, 200, 200, 100, 200, 100, 100}; MoveToEx(hdc, apt[0].x, apt[0].y, NULL); for(int i=1; i<5;i++) { LineTo(hdc, apt[i].x, apt[i].y); } } // 绘制折线 void DrawPolyline(HDC hdc) { POINT apt[5] = {100, 100, 200, 100, 200, 200, 100, 200, 100, 100}; Polyline(hdc, apt, 5); } void DrawPolylineTo(HDC hdc) { POINT apt[5] = {100, 100, 200, 100, 200, 200, 100, 200, 100, 100}; MoveToEx(hdc, apt[0].x, apt[0].y, NULL); PolylineTo(hdc, apt+1, 4); } #define NUM 1000 #define PI 3.1415926535 // 绘制正弦波 void DrawSinewave(HDC hdc, int cx, int cy) { POINT apt[NUM]; MoveToEx(hdc, 0, cy/2, NULL); LineTo(hdc, cx, cy/2); /* sin(x),cos(x)等,这里的x都是弧度,而不是直接的角度。 因此,在计算对应角度的三角函数时,需要先将角度转换成弧度再计算。 弧度和角度的转换公式是: 角度=弧度*180.0f/PI 弧度=角度*PI/180.0f sin(30) = 1/2 sin(45) = sqrt(2)/2 sin(60) = sqrt(3)/2 sin(90) = 1 */ double b = 0; for(int i=0;i<NUM;++i) { b = i * PI / 180; /* 把角度转化为弧度 */ apt[i].y = sin(b) * cy / 2 + cy / 2; apt[i].x = (cx / NUM) * i; } Polyline(hdc, apt, NUM); }