easyX 绘制坐标系

.h

#pragma once
#include<graphics.h>

class Graph {
public:
	//颜色
	static unsigned long int COLOR_BLUE;//蓝色
	static unsigned long int COLOR_BLACK;//黑色
	static unsigned long int COLOR_RED;//红色
	static unsigned long int COLOR_GREEN;//绿色

/******
 0 初始化类
 ******/
	Graph(int windowWidth, int windowHigh); //创建一个画图窗口
	void drawCoordinateAxis(int xLimit, int yLimit, int xStep, int yStep); //画坐标轴
	
 /******
 1 画点类
 ******/
	void drawPoint(int x, int y, int yLimit); //画点
	void setPointColor(unsigned long int color); //设置点颜色


private:
	int windowHigh, windowWidth, windowPadding;//窗口高、宽、边界厚度 
	int lineWidth;//线宽
	unsigned long int lineColor, textColor, pointColor;//线、文本、点颜色
	int xScale, yScale;//比例尺
	double baseLengthX, baseLengthY;//两个坐标轴刻度的图上距离
};

.cpp

#include "MMW_ConfigureDlg.h"
#include<easyx.h>
#include<conio.h>
#include "stdio.h"

unsigned long int Graph::COLOR_BLUE = BLUE;
unsigned long int Graph::COLOR_BLACK = BLACK;
unsigned long int Graph::COLOR_RED = RED;
unsigned long int Graph::COLOR_GREEN = GREEN;

Graph::Graph(int windowWidth, int windowHigh) {
	this->windowWidth = windowWidth;
	this->windowHigh = windowHigh;
	windowPadding = 50;

	//直线默认属性
	lineColor = BLACK;
	textColor = BLACK;
	lineWidth = 2;

	initgraph(windowWidth, windowHigh); // 初始化窗口
	setbkcolor(WHITE);
	cleardevice();
}

/*
	xLimit		X坐标轴代表的实际长度
	yLimit		y坐标轴代表的实际长度
	xStep		X轴两个刻度间的实际长度
	yStep		y轴两个刻度间的实际长度
	xLimit / xStep  X坐标轴被分成多少段
	yLimit / yStep  y坐标轴被分成多少段
*/
void Graph::drawCoordinateAxis(int xLimit, int yLimit, int xStep, int yStep) {
	xScale = (windowWidth - 2 * windowPadding) / xLimit; //比例尺
	yScale = (windowHigh - 2 * windowPadding) / yLimit;

	baseLengthX = (windowWidth - 2 * windowPadding) / (xLimit / xStep);//图上两个刻度间距
	baseLengthY = (windowHigh - 2 * windowPadding) / (yLimit / yStep);

	setlinecolor(lineColor);
	setlinestyle(PS_SOLID | PS_JOIN_BEVEL, lineWidth);
	settextcolor(BLACK);

	line(windowPadding, windowHigh - windowPadding, 
		 windowPadding + xLimit / xStep * baseLengthX, windowHigh - windowPadding);//横轴
	line(windowPadding + xLimit / xStep * baseLengthX / 2, windowHigh - windowPadding,
		windowPadding + xLimit / xStep * baseLengthX / 2,
		windowHigh - (windowPadding + yLimit / yStep * baseLengthY));//竖轴
		//windowPadding);
	
#if(1)
	/*横轴刻度绘制*/
	for (int i = 0; i <= xLimit; i += xStep) {
		TCHAR c[20];
		_stprintf_s(c, _T("%d"), i-xLimit/2);
		outtextxy(windowPadding + i * xScale, windowHigh - (windowPadding - 10), c);
	}

	/*竖轴刻度绘制*/
	for (int i = yStep; i <= yLimit; i += yStep) {
		TCHAR c[20];
		_stprintf_s(c, _T("%d"), i);
		outtextxy(windowPadding + xLimit / xStep * baseLengthX / 2 + 5, 
				  windowHigh - (windowPadding + i * yScale), c);
	}
#endif
}

void Graph::drawPoint(int x, int y, int yLimit)
{
	setfillcolor(pointColor);
	fillcircle(windowPadding + x * xScale + xLimit * xScale / 2, 
			   windowHigh - (windowPadding + y * yScale), 3);
}

void Graph::setPointColor(unsigned long int color)
{
	pointColor = color;
	setfillcolor(color);
	setlinecolor(color);
}

main.cpp

//#include<stdio.h>
#include "MMW_ConfigureDlg.h"
//#include <iostream>
//#include "stdlib.h"
//#include "process.h"
#include "conio.h"

void test01() {
	Graph graph = Graph(800, 640);
	graph.drawCoordinateAxis(20, 20, 2, 1);//结合窗口合理设置(´-﹏-`;)
	graph.setPointColor(Graph::COLOR_RED);//设置点颜色

	for (int i = 0; i < 4; i++) {
		graph.drawPoint(i, i,20);
		Sleep(200);
	}
}

int main(){
	test01();
	
	_getch();
	//Sleep(3000);
	//system("pause");
	//std::cin.get();
	//getchar();
	return 0;
}

运行效果:
在这里插入图片描述

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
可以通过以下步骤使用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; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值