多线程绘制函数图像

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include <windows.h>

#define BASE_X	500

#define BASE_Y	500

#define COLOR	RGB(255,255,255)

void printPoint(HDC hdc,int x,int y)
{
	printf("X:%02d - Y:%02d\r",x,y);
	SetPixel(hdc, BASE_X + x , BASE_Y + y,COLOR);
}

void printLine(HDC hdc,int x0,int y0,int x1,int y1)
{
	while(x0 < x1 || y0 < y1)
	{
		SetPixel(hdc, x0 ,y0,COLOR);

		if( x0 < x1 )
		{
			++x0;
		}

		if( y0 < y1 )
		{
			++y0;
		}
	}
}

typedef struct
{
	void (*func)(double x,double times);

	double x;

	double times;
}FUNC;

DWORD WINAPI ThreadRouine(LPVOID threadParam)
{
	FUNC *pfunc = (FUNC*)threadParam;

	if(pfunc && pfunc->func)
	{
		pfunc->func(pfunc->x,pfunc->times);
	}

	return 0;
}

void beginTask(FUNC *func)
{
	HANDLE hThread = CreateThread(NULL,0,ThreadRouine,(LPVOID)func,0,NULL);

	CloseHandle(hThread);
}

void my_task(double x,double times)
{
	double i = 0;

	double ret = 0;

	HDC hdc = GetDC( GetConsoleWindow() );

	for(i = -1 * x; i < x; i +=0.01)
	{
		ret = times * sin(i);

		printPoint(hdc,i * 10,ret * 10);

		Sleep(1);
	}

	ReleaseDC(GetConsoleWindow(), hdc);
}

int main()
{
	int i = 0;

	HDC hdc = GetDC( GetConsoleWindow() );

	FUNC fun[] = 
	{
		{my_task,50,1},
		{my_task,50,2},
		{my_task,50,3},
		{my_task,50,4},
		{my_task,50,5},
		{my_task,50,6},
		{my_task,50,7},
		{my_task,50,8},
		{my_task,50,9},
		{my_task,50,10},
		{my_task,50,11},
		{my_task,50,12},
		{my_task,50,13},
		{my_task,50,14},
		{my_task,50,15},
		{my_task,50,16},
		{my_task,50,17},
		{my_task,50,18},
		{my_task,50,19},
		{my_task,50,20},
		{my_task,50,21},
		{my_task,50,22},
		{my_task,50,23},
		{my_task,50,24},
		{my_task,50,25},
		{my_task,50,26},
		{my_task,50,27},
		{my_task,50,28},
		{my_task,50,29},
		{my_task,50,30}
	};

	printLine(hdc,0,BASE_Y,10000,0);
	printLine(hdc,BASE_X,0,0,10000);

	ReleaseDC(GetConsoleWindow(), hdc);

	for(i = 0; i < sizeof(fun) / sizeof(fun[0]); ++i)
	{
		beginTask(&fun[i]);
	}

	while(1){ Sleep(1); }

	return 0;
}


多线程绘制函数图像_第1张图片

你可能感兴趣的:(多线程绘制函数图像)