easyX(基本绘图和文字绘制)

简单图形的函数

circle - 圆,ellipes - 椭圆,pie - 扇形,polygon - 画多边形, rectangle - 矩形,roundrect - 圆角矩形,line - 画线,putpixel - 点

以圆为样例进行绘制

创建窗口

initgraph(长,高,flag);

第三个参数可以是创建一个输入窗口 initgraph(640, 480, SHOWCONSOLE);

输入数字的时候要用scanf_s;

 

背景颜色的绘制

首先要用:setbkcolor(颜色)来定义要绘制的背景颜色

然后用:cleandevice()来清屏

边框的样式

setlinestyle(高度,宽度, 字体);

例:setlinestyle(PS_SOLID, 5);   (PS_SOLID表示的是实线,选择PS_SOLID然后点击右键,进入转换到定义,可出现线条的种类)

边框颜色和填充颜色 

   // 填充颜色
    setfillcolor(YELLOW);
    //边框颜色
    setlinecolor(BLUE);

(VS里面有十六种原色)

圆的类型

   //x,y坐标和圆的半径(无填充)
    circle(10, 10, 50);
    //有边框填充
    fillcircle(100, 100, 50);
    //无边框填充
    solidcircle(200, 200, 50);

 绘制文字

设置文字颜色

settextcolor(颜色);

放置字符

outtextxy(x, y, '字符');

如果第三个参数为字符串,如果直接运行,会导致参数错误;

那么有三种解决方案:

1.在字符串前面加上大写的L;

outtextxy(x,y,L"字符串");

2.用TEXT(字符串)或者  _T();

outtextxy(x,y,TEXT("字符串"));

3.项目->属性->高级->字符集->多字节字符集

设置文字样式

settextstyle(大小, 0, "字体");  

例如:settextstyle(50, 0, "楷体");

计算长度与宽度

   //宽度
textwidth(arr)         arr是字符数组
    //长度
textheight(arr) 

将字符居中对齐

  

//宽度
	int width =300/2 - textwidth(arr)/2;
	//长度
	int height = 50/2-textheight(arr) / 2;
	outtextxy(width+200, height+50, arr);


   

代码段 

#include<stdio.h>

#include<graphics.h>

int main()
{
	//创建窗口
	initgraph(640, 480, SHOWCONSOLE);
	/*circle - 圆,ellipes - 椭圆,pie - 扇形,polygon - 画多边形
	rectangle - 矩形,roundrect - 圆角矩形,line - 画线,putpixel - 点*/
	//圆
	//设置背景颜色
	setbkcolor(GREEN);
	//清屏,实际上就是填充覆盖
	cleardevice();
	//边框样式(高度,宽度,字体)
	setlinestyle(PS_SOLID, 5);
	// 填充颜色
	setfillcolor(YELLOW);
	//边框颜色
	setlinecolor(BLUE);
	//x,y坐标和圆的半径(无填充)
	circle(10, 10, 50);
	//有边框填充
	fillcircle(100, 100, 50);
	//无边框填充
	solidcircle(200, 200, 50);

	//绘制文字
	//设置文字颜色
	settextcolor(RED);
	//设置文字样式,字体..

	settextstyle(20, 0, "楷体");
	//设置背景模式
	//trans parent 透明
	setbkmode(TRANSPARENT);
	settextcolor(RGB(0, 200, 99));
	outtextxy(50, 50, "我是太子!");

	//把文字居中
	//设置了一个矩形框,(左上方的x,y,右上方的x,y)
	fillrectangle(200, 50, 500, 100);
	settextcolor(RGB(200, 0, 0));
	char arr[] = "我是太子!";
	//字符串长度
	//宽度
	int width =300/2 - textwidth(arr)/2;
	//长度
	int height = 50/2-textheight(arr) / 2;
	outtextxy(width+200, height+50, arr);


	getchar();

	
	closegraph();

	return 0;
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以通过以下步骤使用EasyX读取坐标点文件并绘制图形: 1. 打开文件并读取坐标点数据,例如txt文件每行格式为"x y",可以使用ifstream和getline函数来读取数据。 ```c++ ifstream f("data.txt"); string line; while (getline(f, line)) { istringstream iss(line); int x, y; iss >> x >> y; // 存储坐标数据 } f.close(); ``` 2. 创建绘图窗口并设置绘图参数,例如背景色、坐标轴等。 ```c++ initgraph(width, height); setbkcolor(WHITE); setlinecolor(BLACK); setlinestyle(PS_SOLID, 1); settextstyle(16, 0, _T("宋体")); ``` 3. 绘制坐标轴及其他辅助线,例如x轴和y轴。 ```c++ line(0, height / 2, width, height / 2); // x轴 line(width / 2, 0, width / 2, height); // y轴 ``` 4. 遍历坐标点数据并将其转换为屏幕坐标,然后使用line函数或者circle函数绘制图形。 ```c++ for (int i = 0; i < points.size(); i++) { int x = points[i].x + width / 2; int y = height / 2 - points[i].y; circle(x, y, 2); } ``` 5. 关闭绘图窗口。 ```c++ closegraph(); ``` 完整代码如下: ```c++ #include <graphics.h> #include <fstream> #include <sstream> #include <vector> using namespace std; struct Point { int x; int y; }; int main() { int width = 600; int height = 600; vector<Point> points; ifstream f("data.txt"); string line; while (getline(f, line)) { istringstream iss(line); int x, y; iss >> x >> y; points.push_back({ x, y }); } f.close(); initgraph(width, height); setbkcolor(WHITE); setlinecolor(BLACK); setlinestyle(PS_SOLID, 1); settextstyle(16, 0, _T("宋体")); line(0, height / 2, width, height / 2); line(width / 2, 0, width / 2, height); for (int i = 0; i < points.size(); i++) { int x = points[i].x + width / 2; int y = height / 2 - points[i].y; circle(x, y, 2); } closegraph(); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值