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

📄 draw_moun.cpp

📁 分形几何被曼德勃罗喻为是大自然的几何学。这个代码
💻 CPP
字号:
// draw_moun.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include <time.h>
#include <stdlib.h>
#include <list>
#define MAX_LOADSTRING 100
using namespace std;
// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text




struct v_point 
{
	int x;
	int y; //x,y坐标
};
struct vertex
{
	v_point point;
	float z; //对于每个点有三个坐标,z表示在y方向上的上升坐标
};
list<vertex> lst_vertex;
//the count of iterations.
#define PLYS 7
static int g_plys=PLYS;
static HDC s_hdc; 
static HWND s_hwnd;

//funcions for drawing the triangles.
template<typename T> T midpoint(T x,T y);
vertex fraline(vertex vertex1,vertex vertex2);
void drawtriangle(v_point A,v_point B,v_point C);
void startdraw(v_point A,v_point B,v_point C,int plys);
void fratriangle(vertex A,vertex B,vertex C,int plys);
int sig();//return 1 or -1

// 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;
	//seed the random number generator
	srand((unsigned int)time(NULL));
	#define Rnd(num) ((float)rand())/RAND_MAX*num
	#define Sig	(sig())//return 1 or -1
	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_DRAW_MOUN, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

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

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

	// 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_DRAW_MOUN);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_DRAW_MOUN;
	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
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
	s_hwnd=hWnd;
	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);
			// TODO: Add any drawing code here...
			RECT rt;
			GetClientRect(hWnd, &rt);
			s_hdc=hdc;
			vertex v1,v2,v3;
			//x,y,z
//注意:在下面设置山的位置
			v1.point.x=100; 
			v1.point.y=300;
			v2.point.x=500;
			v2.point.y=200;
			v3.point.x=200;
			v3.point.y=500;
//注意:在下面设置山的参数
			v1.z=6;
			v2.z=2;
			v3.z=60;
			lst_vertex.push_back(v1);
			lst_vertex.push_back(v2);
			lst_vertex.push_back(v3);

			fratriangle(v1,v2,v3,PLYS);
			g_plys=PLYS;
			//for debugs.
	
			startdraw(v1.point,v2.point,v3.point,PLYS);
			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;
}

//make the middle point of point1 and point2.
template<typename T>
T midpoint(T x,T y)
{
	return (T)(x+y)/2;
}

vertex fraline(vertex vertex1,vertex vertex2)
{
	int mid_x=midpoint(vertex1.point.x , vertex2.point.x);
	list<vertex>::iterator ite;
	vertex node;
	//iterate the list for vetex.and find the point if exists.
	for(ite=lst_vertex.begin();ite!=lst_vertex.end();++ite)
	{
		if(ite->point.x==mid_x)
			return *ite;
	}
	
	node.point.x=mid_x;
	node.point.y=midpoint(vertex1.point.y,vertex2.point.y);
	//need the z posision.so the parameters must be vertex type.
	node.z=midpoint(vertex1.z,vertex2.z)+Rnd(4);
	//lst_vertex.push_front(node);
	lst_vertex.push_back(node);
	return node;
}
//vertexs are the three vertexs of the triangle.
void fratriangle(vertex A,vertex B,vertex C,int plys)
{	
	if(plys<=1) return;
	vertex AB,BC,CA;	
        AB=fraline(A,B);
	BC=fraline(B,C);
	CA=fraline(C,A);

	--plys;
	fratriangle(CA,BC,C,plys);
	fratriangle(AB,B,BC,plys);
	fratriangle(BC,CA,AB,plys);
	fratriangle(A,AB,CA,plys);
	
}


void startdraw(v_point A,v_point B,v_point C,int plys)
{
	v_point AB,BC,CA;
	if(plys==1)
		drawtriangle(A,B,C);
	else
	{
		AB.x=midpoint(A.x,B.x);
		AB.y=midpoint(A.y,B.y);
		BC.x=midpoint(B.x,C.x);
		BC.y=midpoint(B.y,C.y);
		CA.x=midpoint(C.x,A.x);
		CA.y=midpoint(C.y,A.y);
		--plys;
		startdraw(CA,BC,C,plys);
		startdraw(AB,B,BC,plys);
		startdraw(BC,CA,AB,plys);
		startdraw(A,AB,CA,plys);
		
	}
	
}

//draw the triangles.
void drawtriangle(v_point A,v_point B,v_point C)
{
	list<vertex>::iterator ite;
	int a_x=A.x;
	int b_x=B.x;
	int c_x=C.x;
	int a_z;
	int b_z;
        int c_z;
	v_point point[3];
	//i must come back to improve this algorithm.
	for(ite=lst_vertex.begin();ite!=lst_vertex.end();++ite)
	{
		if(ite->point.x==a_x)
			a_z=ite->z;
		else if(ite->point.x==b_x)
			b_z=ite->z;
		else if(ite->point.x==c_x)
			c_z=ite->z;
	}

	point[0].x=a_x;
	point[0].y=A.y+a_z;
	point[1].x=b_x;
	point[1].y=B.y+b_z;
	point[2].x=c_x;
	point[2].y=C.y+c_z;
	//NOTE:to improve the efficiency,we should create a brush array that is loaded on
	//the intialization of the program.
	static int loop=100; 
	HBRUSH br;
	HPEN pen;
	
	float rnd=Rnd(loop);

//注意:在下面修改山的颜色
	COLORREF cr=RGB(90+rnd,150+rnd,120+rnd);
	loop=loop-1;
	if(loop<1)
		loop=100;
	
//	int r=80+(int)(l*(rand-0.5)*60);
	br=CreateSolidBrush(cr);
	pen=CreatePen(PS_NULL,1,0);
	SelectObject(s_hdc,br);
	SelectObject(s_hdc,pen);
	Polygon(s_hdc,(POINT*)point,3);

	return;
}
int sig()
{
	if(Rnd(1)>0.5)
		return 1;
	else 
		return -1;
}

⌨️ 快捷键说明

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