最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++控制臺繪圖頭文件實(shí)例代碼

 更新時(shí)間:2023年01月24日 10:16:05   作者:xybf  
控制臺(console)是電腦的最基本交互接口,通常包括鍵盤(keyboard)和屏幕(screen),下面這篇文章主要給大家介紹了關(guān)于C++控制臺繪圖頭文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

簡介

這個(gè)頭文件是用來在C++控制臺繪圖的,借鑒了 pygame 的一些思想,提供了一些函數(shù)用來繪圖,可以有效的避免多次繪制重復(fù)圖像時(shí)屏閃等問題。

函數(shù)介紹

1、init(int x, int fz)

初始化屏幕, 將屏幕大小設(shè)定為 x , 字體大小為 fz 。

2、fill(concol color)

用來將整個(gè)屏幕填充為一定的顏色。

3、update()

注意!繪畫完后的圖像并不會立刻出現(xiàn)在控制臺上,使用這個(gè)函數(shù)來更新。

4、getmousepos(pos & p)

獲取鼠標(biāo)坐標(biāo)并存到 p 中

5、gt(int x, int y)

將控制臺光標(biāo)移到(x,y)的位置,x,y 從1開始

6、HideCursor()

隱藏控制臺光標(biāo)

7、settextcolor(concol color)

設(shè)置文字顏色

8、setbackcolor(concol color)

設(shè)置背景顏色

9、rect(int sx, int sy, int ex, int ey, concol color)

以(sx, sy)為左上端點(diǎn)、(ex, ey)為右下端點(diǎn),繪制顏色為color的矩形。

10、line(int sx, int sy, int ex, int ey, concol color)

繪制一條從(sx, sy)到(ex, ey)的線。

11、dot(int x, int y, concol color)

在(x,y)的位置畫一個(gè)點(diǎn)

先介紹到這里

里面更多的功能、函數(shù)會在以后的文章后詳細(xì)解釋。

示例代碼

我這里將頭文件命名為 Drawer.h ,如果用了別的名字保存頭文件,這里也要改過來。

功能是從(1,1)到鼠標(biāo)的位置畫線。

#include "Drawer.h"
using namespace std;
 
Pos mp; // Mouse_Pos
 
void game()
{
	while(1)
	{
		getmousepos(mp); // Get Mouse Pos
		fill(black); // Let Screen be black
		line(1, 1, mp.x, mp.y, green); // from (1, 1) to (mp.x, mp.y) draw Line
		//                        ^
		//                        |
		// You can change this to red, blue, white or more
		update(); // Update Screen
	}
}
 
int main(){
    system("mode con cols=102 lines=52");
    init(50, 16);// 50x50 Screen and font size is 16
	game();
}

示例圖片

截屏截不到鼠標(biāo)。。。

頭文件代碼

#include <windows.h>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#ifndef KEY_DOWN
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
#endif
#define clean 0x2
#define box 0x4
#define full 0x1
using namespace std;
 
enum concol {
	black = 0,
	dark_blue = 1,
	dark_green = 2,
	dark_aqua = 3, dark_cyan = 3,
	dark_red = 4,
	dark_purple = 5, dark_pink = 5, dark_magenta = 5,
	dark_yellow = 6,
	dark_white = 7,
	gray = 8,
	blue = 9,
	green = 10,
	cyan = 11,
	red = 12,
	purple = 13, pink = 13,
	yellow = 14,
	white = 15
};
 
struct pixel {
	concol color;
};
 
struct Pos {
	int x, y;
};
 
struct Pos3d
{
	int x, y, z ;
};
 
CONSOLE_CURSOR_INFO CursorInfo;
COORD _GoToPos;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
HWND hwnd = GetForegroundWindow();
int backcol, textcol;
pixel _LastScreen[1000][1000];
pixel _NewScreen[1000][1000];
int _ScreenSideLength;
int Py3dx = 0, Py3dy = 0 ;
int xzlen = 2, yzlen = 1 ;
int fontsize = 16;
 
inline void getmousepos(Pos &p) {
	POINT pt;
	GetCursorPos(&pt);
	ScreenToClient(hwnd, &pt);
	p.y = (pt.y / fontsize + 0.5);
	p.x = (pt.x / fontsize + 0.5);
}
 
inline void gt(short x, short y) {
	--x;
	--y;
	_GoToPos = {x * xzlen, y * yzlen};
	SetConsoleCursorPosition(hOut, _GoToPos);
}
 
inline void HideCursor()
{
	GetConsoleCursorInfo(hOut, &CursorInfo);
	CursorInfo.bVisible = false;
	SetConsoleCursorInfo(hOut, &CursorInfo);	
}
 
inline void settextcolor(concol textcolor) {
	textcol = textcolor;
	unsigned short wAttributes = ((unsigned int)backcol << 4) | (unsigned int)textcol;
	SetConsoleTextAttribute(hOut, wAttributes);
}
 
inline void setbackcolor(concol backcolor) {
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	backcol = backcolor;
	unsigned short wAttributes = ((unsigned int)backcol << 4) | (unsigned int)textcol;
	SetConsoleTextAttribute(hOut, wAttributes);
}
 
struct Button {
	Pos mp;
	const char* name;
	int len, qx, px, py;
	void rename(const char* ne) {
		name = ne;
		len = strlen(ne);
	}
	
	void setpos(int x, int y) {
		py = y;
		qx = (x - len / 2);
		px = (x + len / 2);
		gt(qx, y);
		printf(name);
	}
	bool check() {
		
		getmousepos(mp);
		if (mp.x <= px and mp.x >= qx and mp.y <= py and mp.y >= py - 2) {
			gt(qx - 2, py);
			printf(">>");
			gt(px, py);
			printf("<<");
			if (KEY_DOWN(MOUSE_MOVED)) return true;
		} else {
			gt(qx - 2, py);
			printf("  ");
			gt(px, py);
			printf("  ");
		}
		return false;
	}
};
 
 
inline void init(int x, int fz) {
	fontsize = fz;
	GetConsoleCursorInfo(hOut, &CursorInfo);
	CursorInfo.bVisible = false;
	SetConsoleCursorInfo(hOut, &CursorInfo);
	_ScreenSideLength = x;
}
 
inline void update() {
	for (int i = 1; i <= _ScreenSideLength ; ++i) {
		for (int j = 1; j <= _ScreenSideLength ; ++j) {
			if (_LastScreen[j][i].color != _NewScreen[j][i].color) {
				gt(j, i);
				setbackcolor(_NewScreen[j][i].color);
				for(int i=1;i<=yzlen;++i)
				{
					for(int j=1;j<=xzlen;++j)
						putchar(' ');
					puts("\n");
				}		
				_LastScreen[j][i] = _NewScreen[j][i];
			}
		}
	}
}
 
void fill(concol color) {
	for (int i = 1; i <= _ScreenSideLength; ++i) {
		for (int j = 1; j <= _ScreenSideLength; ++j) {
			if (_NewScreen[j][i].color != color) {
				_NewScreen[j][i].color = color;
			}
 
		}
	}
}
 
void rect(int sx, int sy, int ex, int ey, concol color) {
	if (ey < sy) swap(ey, sy);
	if (ex < sx) swap(ex, sx);
	for (int i = sy; i <= ey; ++i) {
		for (int j = sx; j <= ex; ++j) {
			_NewScreen[j][i].color = color;
		}
	}
}
 
void ChangePy(int x, int y)
{
	Py3dx = x;
	Py3dy = y;
}
 
inline void dot(int x, int y, concol color) {
	_NewScreen[x][y].color = color;
}
 
inline void line(int sx, int sy, int ex, int ey, concol color)
{
	int xlen = ex - sx ;
	int ylen = ey - sy ;
	int len = sqrt(pow(xlen, 2) + pow(ylen, 2)) ;
	for(double i=0; i<=len; i += 1)
	{
		int x = xlen * i / len ;
		int y = ylen * i / len ;
		_NewScreen[x + sx][y + sy].color = color ;
	}
}
 
inline Pos pos3t2(Pos3d pos)
{
	if(pos.z == 0)
	{
		return Pos {pos.x, pos.y} ;
	}
	return Pos{pos.x / (log10(pos.z)) + Py3dx, pos.y / (log10(pos.z)) + Py3dy} ;
}
 
inline void line3d(Pos3d a, Pos3d b, concol color)
{
	Pos a2 = pos3t2(a);
	Pos b2 = pos3t2(b);
//	line(a2.x, b2.y, a2.y, b2.x, color);
	line(a2.x, a2.y, b2.x, b2.y, color);
}
 
inline int dis3d(Pos3d a, Pos3d b)
{
	int x = abs(a.x - b.x);
	int y = abs(a.y - b.y);
	int z = abs(a.z - b.z);
	return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2)) ;
}
 
/*
Q------W
 \    /
  A--S
*/
inline void rect3d(Pos3d q, Pos3d w, Pos3d a, Pos3d s, concol color)
{
	int qlen = dis3d(q, a);
	int plen = dis3d(w, s);
	int len = max(qlen, plen);
	for(double i=1;i<=len; i+=0.1)
	{
		Pos3d qpos = Pos3d {abs(q.x-a.x)*i/len + q.x, abs(q.y-a.y)*i/len + q.y, abs(q.z-a.z)*i/len + q.z} ;
		Pos3d ppos = Pos3d {abs(w.x-s.x)*i/len + w.x, abs(w.y-s.y)*i/len + w.y, abs(w.z-s.z)*i/len + w.z} ;
		line3d(qpos, ppos, color);
	}
}
 
struct cube
{
	
	Pos3d pos ;
	int xlen, ylen, zlen ;
	
	void init(int xp, int yp, int zp, int xl, int yl, int zl)
	{
		pos = Pos3d {xp - Py3dx, yp - Py3dy, zp};
		xlen = xl;
		ylen = yl;
		zlen = zl;
	}
	
	inline Pos3d getdotpos(int doti)
	{
		switch(doti)
		{
			case 8: return Pos3d {pos.x + xlen, pos.y + ylen, pos.z + zlen} ;
			case 7: return Pos3d {pos.x, pos.y + ylen, pos.z + zlen} ;
			case 6: return Pos3d {pos.x + xlen, pos.y, pos.z + zlen} ;
			case 4: return Pos3d {pos.x + xlen, pos.y + ylen, pos.z} ;
			case 5: return Pos3d {pos.x, pos.y, pos.z + zlen} ;
			case 3: return Pos3d {pos.x, pos.y + ylen, pos.z} ;
			case 2: return Pos3d {pos.x + xlen, pos.y, pos.z} ;
			case 1: return Pos3d {pos.x, pos.y, pos.z} ;
		}
		return {0, 0, 0}; //Else
	}
	/*
	    5----------6
	   /|         /|
	  / |        / |
	 /  |       /  |
	1----------2   |
	|   7------|---8
ylen|  /       |  /
	| /        | /zlen
	|/  xlen   |/
	3----------4
	^
	|
	O
	*/	
	inline void draw(concol color, int mode)
	{
		if(mode & full)
		{
		rect3d(getdotpos(5), getdotpos(7), getdotpos(6), getdotpos(8), green);
		rect3d(getdotpos(1), getdotpos(3), getdotpos(5), getdotpos(7), red);
		rect3d(getdotpos(5), getdotpos(1), getdotpos(6), getdotpos(2), blue);
		rect3d(getdotpos(7), getdotpos(3), getdotpos(8), getdotpos(4), pink);
		rect3d(getdotpos(2), getdotpos(4), getdotpos(6), getdotpos(8), yellow);
		rect3d(getdotpos(1), getdotpos(3), getdotpos(2), getdotpos(4), white);			
		}
		if(mode & clean)
		{
		line3d(getdotpos(1), getdotpos(2), color) ;
		line3d(getdotpos(1), getdotpos(3), color) ;
		line3d(getdotpos(1), getdotpos(5), color) ;
		line3d(getdotpos(2), getdotpos(6), color) ;
		line3d(getdotpos(2), getdotpos(4), color) ;
		line3d(getdotpos(3), getdotpos(4), color) ;
		line3d(getdotpos(3), getdotpos(7), color) ;
		line3d(getdotpos(4), getdotpos(8), color) ;
		line3d(getdotpos(5), getdotpos(6), color) ;
		line3d(getdotpos(5), getdotpos(7), color) ;
		line3d(getdotpos(6), getdotpos(8), color) ;
		line3d(getdotpos(7), getdotpos(8), color) ;
		}
		if(mode & box)
		{
			line3d(getdotpos(1), getdotpos(4), color) ;
			line3d(getdotpos(1), getdotpos(7), color) ;
			line3d(getdotpos(2), getdotpos(3), color) ;
			line3d(getdotpos(2), getdotpos(8), color) ;
			line3d(getdotpos(3), getdotpos(5), color) ;
			line3d(getdotpos(3), getdotpos(8), color) ;
			line3d(getdotpos(4), getdotpos(7), color) ;
			line3d(getdotpos(4), getdotpos(6), color) ;
			line3d(getdotpos(5), getdotpos(2), color) ;
			line3d(getdotpos(5), getdotpos(8), color) ;
			line3d(getdotpos(6), getdotpos(1), color) ;
			line3d(getdotpos(6), getdotpos(7), color) ;				
		}
	}
};

總結(jié)

到此這篇關(guān)于C++控制臺繪圖頭文件的文章就介紹到這了,更多相關(guān)C++控制臺繪圖頭文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Qt Quick Designer灰色或者禁用的解決

    Qt Quick Designer灰色或者禁用的解決

    本文主要介紹了Qt Quick Designer灰色或者禁用的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • C語言中free函數(shù)的使用詳解

    C語言中free函數(shù)的使用詳解

    free函數(shù)是釋放之前某一次malloc函數(shù)申請的空間,而且只是釋放空間,并不改變指針的值。下面我們就來詳細(xì)探討下
    2017-05-05
  • C++實(shí)踐數(shù)組作數(shù)據(jù)成員的參考

    C++實(shí)踐數(shù)組作數(shù)據(jù)成員的參考

    今天小編就為大家分享一篇關(guān)于C++實(shí)踐數(shù)組作數(shù)據(jù)成員的參考,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • C++實(shí)現(xiàn)設(shè)計(jì)模式之裝飾者模式詳解

    C++實(shí)現(xiàn)設(shè)計(jì)模式之裝飾者模式詳解

    這篇文章主要介紹了C++設(shè)計(jì)模式之裝飾模式,裝飾模式能夠?qū)崿F(xiàn)動態(tài)的為對象添加功能,是從一個(gè)對象外部來給對象添加功能,需要的朋友可以參考下
    2021-09-09
  • C/C++中時(shí)間庫函數(shù)的使用詳解

    C/C++中時(shí)間庫函數(shù)的使用詳解

    這篇文章主要為大家詳細(xì)介紹了C/C++中的時(shí)間相關(guān)知識總結(jié),例如時(shí)間庫函數(shù)的使用以及獲取本地時(shí)間的不同方法,文中的示例代碼講解詳細(xì),需要的可以參考一下
    2022-11-11
  • C++判斷矩形相交的方法

    C++判斷矩形相交的方法

    這篇文章主要介紹了C++判斷矩形相交的方法,涉及C++針對平面坐標(biāo)數(shù)學(xué)運(yùn)算的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • C++概念重載、覆蓋、隱藏的使用說明

    C++概念重載、覆蓋、隱藏的使用說明

    本篇文章介紹了,在C++中概念重載、覆蓋、隱藏的使用分析說明。需要的朋友參考下
    2013-05-05
  • Qt6.0+vs2019環(huán)境配置的實(shí)現(xiàn)教程

    Qt6.0+vs2019環(huán)境配置的實(shí)現(xiàn)教程

    這篇文章主要介紹了Qt6.0+vs2019環(huán)境配置的實(shí)現(xiàn)教程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • c語言二進(jìn)制數(shù)按位輸出示例

    c語言二進(jìn)制數(shù)按位輸出示例

    這篇文章主要介紹了c語言二進(jìn)制數(shù)按位輸出示例,需要的朋友可以參考下
    2014-03-03
  • C語言利用goto語句設(shè)計(jì)實(shí)現(xiàn)一個(gè)關(guān)機(jī)程序

    C語言利用goto語句設(shè)計(jì)實(shí)現(xiàn)一個(gè)關(guān)機(jī)程序

    今天給大家分享一個(gè)非常有趣的知識——用goto語句編寫一個(gè)關(guān)機(jī)小程序。主要用到了shutdown命令語句、goto語句、strcmp函數(shù)等知識點(diǎn),感興趣的可以了解一下
    2023-01-01

最新評論

禹城市| 青川县| 安义县| 辉县市| 伊金霍洛旗| 太仆寺旗| 武夷山市| 河北区| 黄大仙区| 康平县| 景宁| 弥勒县| 昆山市| 巫溪县| 邳州市| 雅江县| 文化| 申扎县| 沙雅县| 清涧县| 逊克县| 靖江市| 增城市| 玉田县| 冀州市| 西乡县| 怀远县| 尖扎县| 桦南县| 金溪县| 宝应县| 修文县| 高阳县| 乌拉特中旗| 陵川县| 泾源县| 吴川市| 高要市| 湟中县| 光山县| 农安县|