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

📄 graphics.cpp

📁 这是一个C++绘图的程序,使用与C++的初学者,大家请参考并提意见
💻 CPP
字号:
#include "graphics.h"

console_dc::console_dc(int height,int width)
{
	if(width>console_width||width<=0)
		_width=console_width;
	else 
		_width=width;
	if(height>console_height||height<=0)
		_height=console_height;
	else
		_height=height;
    memset(mem,' ',console_height*console_width);
	point_type='*';
}

void console_dc::set_point_type(const char Ptype)
{
	point_type=Ptype;
}

void console_dc::draw_a_point(const point& pt)
{
	if(pt.x<0||pt.x>=_width||pt.y<0||pt.y>=_height)
		return;
	mem[pt.y][pt.x]=point_type;
}

void console_dc::clear()
{
	memset(mem,' ',console_height*console_width);
}

bool console_dc::test_type(const point& pt,const char Ptype)
{//如果point超出边界,也返回true
	if(pt.x<0||pt.x>=_width||pt.y<0||pt.y>=_height)
		return true;
	if(mem[pt.y][pt.x]==Ptype)
		return true;
	return false;
}

void console_dc::refresh()
{
	system("cls");
	for(int j=0;j<_height;j++)
	{
		for(int i=0;i<_width;i++)
		{
			cout<<mem[j][i];
		}
		cout<<endl;
	}
}

graphics::graphics(DC & dc)
{
	pdc=&dc;
}

void graphics::output_string(const point& pt,const char* str)
{
	int length=strlen(str);
	for(int i=0;i<length;i++)
	{
		pdc->set_point_type(str[i]);
		pdc->draw_a_point(point(pt.y,pt.x+i));
	}
	pdc->refresh();
}

void graphics::rect(const point& topleft,const point& bottomright)
{
	int i;
	for(i=topleft.x;i<=bottomright.x;i++)	{
		pdc->draw_a_point(point(topleft.y,i)) ;
		pdc->draw_a_point(point(bottomright.y,i)) ;
	}
	for(i=topleft.y+1;i<bottomright.y;i++)	{
		pdc->draw_a_point(point(i,topleft.x));
		pdc->draw_a_point(point(i,bottomright.x));
	}	
	pdc->refresh();
}

void graphics::line(const point& start_point,const point& end_point)
{
	int i,proximate_point;
	float slope,precise_point;

	slope=(start_point.y-end_point.y+0.0)/(start_point.x-end_point.x);    

	for(i=start_point.x;i<=end_point.x;i++)//当start_point.x<=end_point.x时
	{
		precise_point=start_point.y+slope*(i-start_point.x);
		proximate_point=(int)(precise_point*10)%10/5+(int)precise_point;//对precise_point进行四舍五入
		pdc->draw_a_point(point(proximate_point,i));	
	}
	for(i=start_point.x;i>=end_point.x;i--)//当start_point.x>=end_point.x时
	{
		precise_point=start_point.y+slope*(i-start_point.x);
		proximate_point=(int)(precise_point*10)%10/5+(int)precise_point;//对precise_point进行四舍五入
		pdc->draw_a_point(point(proximate_point,i));	
	}
    for(i=start_point.y;i<=end_point.y;i++)//当start_point.y<=end_point.y时
	{
		precise_point=start_point.x+(i-start_point.y)/slope;
		proximate_point=(int)(precise_point*10)%10/5+(int)precise_point;
		pdc->draw_a_point(point(i,proximate_point));	
	}
	for(i=start_point.y;i>=end_point.y;i--)//当start_point.y>=end_point.y时
	{
		precise_point=start_point.x+(i-start_point.y)/slope;
		proximate_point=(int)(precise_point*10)%10/5+(int)precise_point;
		pdc->draw_a_point(point(i,proximate_point));	
	}
	pdc->refresh();
}

void graphics::fill(const point& seed_point, POINT_TYPE border_char, POINT_TYPE filled_char)
{
	point *current_point=new point;
	stack <point> pt_set;
	pdc->set_point_type(filled_char);
	pdc->draw_a_point(*current_point);
	pt_set.push(seed_point);
	while(!pt_set.empty())
	{
		*current_point=pt_set.top();
		pt_set.pop();
		if(!pdc->test_type(point(current_point->y+1,current_point->x),border_char)&&
			!pdc->test_type(point(current_point->y+1,current_point->x),filled_char))
		{//the point under the current_point
			pdc->draw_a_point(point(current_point->y+1,current_point->x));
			pt_set.push(point(current_point->y+1,current_point->x));
		}
		if(!pdc->test_type(point(current_point->y-1,current_point->x),border_char)&&
			!pdc->test_type(point(current_point->y-1,current_point->x),filled_char))
		{//the point above the current_point
			pdc->draw_a_point(point(current_point->y-1,current_point->x));
			pt_set.push(point(current_point->y-1,current_point->x));
		}
		if(!pdc->test_type(point(current_point->y,current_point->x+1),border_char)&&
			!pdc->test_type(point(current_point->y,current_point->x+1),filled_char))
		{//the right point of the current_point
			pdc->draw_a_point(point(current_point->y,current_point->x+1));
			pt_set.push(point(current_point->y,current_point->x+1));
		}
		if(!pdc->test_type(point(current_point->y,current_point->x-1),border_char)&&
			!pdc->test_type(point(current_point->y,current_point->x-1),filled_char))
		{//the left point of the current_point
			pdc->draw_a_point(point(current_point->y,current_point->x-1));
			pt_set.push(point(current_point->y,current_point->x-1));
		}
	}
	delete current_point;
	pdc->refresh();
}

void graphics::set_point_type(POINT_TYPE border_char)
{
	pdc->set_point_type(border_char);
}

void graphics::change_dc(DC & dc)
{
	delete pdc;
	*pdc=dc;
}

void graphics::clear()
{
	pdc->clear();	
	pdc->refresh();	
}

⌨️ 快捷键说明

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