首页 >> 大全

Visual C++游戏编程基础之多边形绘制

2023-10-08 大全 29 作者:考证青年

一、解析几何知识

以(0,0)为圆心,半径为100的圆,它的内接五边形就是它均分五等分的点连起来,坐标(x, y)分别为(100*sin(72°), 100*cos(72°))、(100*sin(72°*2), 100*cos(72°*2))……

二、重要函数介绍

1.BOOL (,CONST POINT*lppt,)

:使用当前画笔描绘一系列线段

_如何用编程制作多边形_编程画多边形

hdc:进行绘图的设备场景

lppt:CONST POINT结构体指针,用来表示X,Y坐标

:表示要连接几个点

2.BOOL (HDC hdc, CONST POINT * apt, DWORD cpt);

:使用目前位置作为开始点,并将目前位置设定为最后一根线的终点;如果不设置起点,默认是(0,0)为开始点

如何用编程制作多边形__编程画多边形

3.BOOL (HDC HDC, CONST POINT *, INT );

:使用当前画刷填充多边形

三、代码实现

#include "stdafx.h"
#include "stdio.h"HINSTANCE hInst;
HPEN hPen;
HBRUSH hBru;
POINT poly1[5],poly2[5],poly3[5];ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
void				MyPaint(HDC hdc);
void                MyPaint1(HDC hdc,LPARAM lparam);int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR     lpCmdLine,int       nCmdShow)
{MSG msg;MyRegisterClass(hInstance);if (!InitInstance (hInstance, nCmdShow)) {return FALSE;}while (GetMessage(&msg, NULL, 0, 0)) {TranslateMessage(&msg);DispatchMessage(&msg);}return msg.wParam;
}ATOM MyRegisterClass(HINSTANCE hInstance)
{WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX); wcex.style			= CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc	= (WNDPROC)WndProc;wcex.cbClsExtra		= 0;wcex.cbWndExtra		= 0;wcex.hInstance		= hInstance;wcex.hIcon			= NULL;wcex.hCursor		= NULL;wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName	= NULL;wcex.lpszClassName	= "canvas";wcex.hIconSm		= NULL;return RegisterClassEx(&wcex);
}BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{HWND hWnd;HDC hdc;int i;const double pi = 3.1415926535;hInst = hInstance;hWnd = CreateWindow("canvas", "绘图窗口" , WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);if (!hWnd){return FALSE;}MoveWindow(hWnd,10,10,600,450,true);ShowWindow(hWnd, nCmdShow);UpdateWindow(hWnd);for(i=0;i<=4;i++){	poly1[i].x = 100 + 100 * sin(i*72*pi/180);//初始化顶点数组poly1[i].y = 100 + 100 * cos(i*72*pi/180);poly2[i].x = poly1[i].x + 300;poly2[i].y = poly1[i].y;poly3[i].x = poly1[i].x + 180;poly3[i].y = poly1[i].y + 200;}hPen = CreatePen(PS_SOLID,5,RGB(255,0,0));hBru = CreateHatchBrush(HS_BDIAGONAL,RGB(0,255,0));hdc = GetDC(hWnd);MyPaint(hdc);ReleaseDC(hWnd,hdc);return TRUE;
}void MyPaint(HDC hdc)
{SelectObject(hdc,hPen);SelectObject(hdc,hBru);Polyline(hdc,poly1,5);PolylineTo(hdc,poly2,5);Polygon(hdc,poly3,5);
}void MyPaint1(HDC hdc,LPARAM lParam)
{int x,y;char str[20] = "";x = LOWORD(lParam);		y = HIWORD(lParam);		SetTextColor(hdc,RGB(255,0,0));TextOut(hdc,10,300,"鼠标坐标",strlen("鼠标坐标"));sprintf(str,"X坐标%d    ",x);//将格式化的数据写入字符串TextOut(hdc,30,340,str,strlen(str));//在(30,30)处显示字符串sprintf(str,"Y坐标%d    ",y);TextOut(hdc,30,360,str,strlen(str));
}LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{PAINTSTRUCT ps;HDC hdc;switch (message) {case WM_PAINT:						hdc = BeginPaint(hWnd, &ps);MyPaint(hdc);EndPaint(hWnd, &ps);break;case WM_MOUSEMOVE:					hdc = GetDC(hWnd);MyPaint1(hdc,lParam);ReleaseDC(hWnd,hdc);break;case WM_DESTROY:					DeleteObject(hPen);DeleteObject(hBru);PostQuitMessage(0);break;default:							return DefWindowProc(hWnd, message, wParam, lParam);}return 0;
}

四、效果

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了