⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 花纹.txt

📁 结合所学习的计算机图形学知识,画出平面花纹的图像
💻 TXT
字号:
// huawen.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include "math.h"
#include "stdio.h"  
#include "stdlib.h" 


#define MAX_LOADSTRING 100
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8
#define Pi  3.1415926
#define OK 1
#define ERROR 0
#define n 20

double x[n],y[n];
double XL=100,XR=500,YB=50,YT=450;
double X=400,Y=150;//圆心
double R=200;


// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_HUAWEN, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_HUAWEN);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
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			= LoadIcon(hInstance, (LPCTSTR)IDI_HUAWEN);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_HUAWEN;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//

//子函数
void encode(double xx,double yy,int *c){
	int CODE=0;
	if(xx<XL)CODE=CODE|LEFT;
	else if (xx>XR)CODE=CODE|RIGHT;
	if(yy<YB)CODE=CODE|BOTTOM;
	else if(yy>YT)CODE=CODE|TOP;
	*c=CODE;
}

//计算多边形的顶点坐标并记录
void keyin(){
int i;
double arf;//边的斜角
double dert;//斜角增量

dert=2*Pi/n;
for(i=0;i<n;i++){
arf=i*dert;
x[i]=X+R*cos(arf);
y[i]=Y+R*sin(arf);
}
}

int anlyse(double *X1,double *Y1,double *X2,double *Y2){
	int code,code1,code2;
	double xx,yy;
	double x1,y1,x2,y2;
	x1=*X1;y1=*Y1;x2=*X2;y2=*Y2;
	encode(x1,y1,&code1);
	encode(x2,y2,&code2);
	if((code1&code2)!=0)return ERROR;
		while(code1!=0||code2!=0)
				{
					code=code1;
					if(code1==0)code=code2;
					if((LEFT&code)!=0)//与左边界相交
					{
						xx=XL;
						yy=y1+(y2-y1)*(XL-x1)/(x2-x1);
					}
					else if((RIGHT&code)!=0)//与右边界相交
					{
						xx=XR;
						yy=y1+(y2-y1)*(XR-x1)/(x2-x1);
					}
					else if((BOTTOM&code)!=0)//与下边界相交
					{
						yy=YB;
						xx=x1+(x2-x1)*(YB-y1)/(y2-y1);
					}
					else if((TOP&code)!=0)//与上边界相交
					{
						yy=YT;
						xx=x1+(x2-x1)*(YT-y1)/(y2-y1);
					}

					if(code==code1)
					{
						x1=xx;y1=yy;encode(xx,yy,&code1);
					}
					else
					{
						x2=xx;y2=yy;encode(xx,yy,&code2);
					}
					if((code1&code2)!=0)return ERROR;
				}//while
		*X1=x1;*Y1=y1;*X2=x2;*Y2=y2;
		return OK;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int i,j,result;
	double x1,y1,x2,y2;
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

	switch (message) 
	{
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_ABOUT:
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				   break;
				case IDM_EXIT:
				   DestroyWindow(hWnd);
				   break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			keyin();

			MoveToEx(hdc,int(XL),int(YB),NULL);
			LineTo(hdc,int(XR),int(YB));
			MoveToEx(hdc,int(XR),int(YB),NULL);
			LineTo(hdc,int(XR),int(YT));
			MoveToEx(hdc,int(XR),int(YT),NULL);
			LineTo(hdc,int(XL),int(YT));
			MoveToEx(hdc,int(XL),int(YT),NULL);
			LineTo(hdc,int(XL),int(YB));
			for(i=0;i<n;i++)
			for(j=i+1;j<n;j++)
			{
				x1=x[i];y1=y[i];
				x2=x[j];y2=y[j];

				result=anlyse(&x1,&y1,&x2,&y2);

				if(result==OK){
				MoveToEx(hdc,int(x1),int(y1),NULL);
				LineTo(hdc,int(x2),int(y2));		
				}//if

			}//for

			RECT rt;
			GetClientRect(hWnd, &rt);
			//DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
			EndPaint(hWnd, &ps);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;

		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
			{
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
			break;
	}
    return FALSE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -