C/C++ EasyX 立方体与超立方体的投影 与 伸缩和旋转变换

首先需要一个能计算矩阵的工具,自建或者去网上寻找相关的库,这里因为涉及的都是简单的矩阵运算,使用我自建的库也够了。

自建的矩阵库,适用于简单的矩阵运算
链接

第三方矩阵库相关文章
链接


绘图相关

头文件
//头文件:UI_set.h
#ifndef _H_UI_SET
#define _H_UI_SET

//画框
#define WIDTH 800//画面宽度
#define HEIGHT 640//画面宽度
//背景色
#define BACKGROUND_COLOR_RED 80
#define BACKGROUND_COLOR_GREEN 80
#define BACKGROUND_COLOR_BLUE 80
//背景模式
#define BACKGROUND_MODE 200 //透明
//#define BACKGROUND_MODE "OPAQUE" //不透明
//字体
#define FONTSIZE 10
#define FONTTYPE "宋体"
//绘图原点
#define _X WIDTH/2
#define _Y HEIGHT/2

typedef struct axis{
   
	int x;//转换后坐标/画布上的坐标
	int y;
	int _x;//转换前坐标/原点在画面中心的直角坐标系上的坐标
	int _y;
}axis;
void InitEasyx(void);//初始化绘图环境
axis AXIS(double x,double y);//转换坐标

#endif
//draw.h
#ifndef _DRAW
#define _DRAW

#define _r 3
//绘制点【默认带坐标显示,白色】
void drawPoint(axis xy,bool display = true, int *color = NULL);
//绘制线【默认带坐标显示,白色】
void drawLine(axis xy1, axis xy2, int *color = NULL);
//绘制提示信息
void drawTip(wchar_t *st, int *color = NULL);

//绘制

#endif
源文件
//initEasyX.cpp
#include <graphics.h>  
#include "UI_set.h"
/* 画布初始化 */
void InitEasyx()
{
   
	initgraph(WIDTH, HEIGHT);//新建画布
	setbkcolor(RGB(BACKGROUND_COLOR_RED,BACKGROUND_COLOR_GREEN,BACKGROUND_COLOR_BLUE));//设置背景色
	cleardevice();
	setbkmode(BACKGROUND_MODE);
	settextstyle(FONTSIZE, 0, _T(FONTTYPE)); // 设置文字大小、字体
}

//转换坐标
axis AXIS(double x,double y){
   
	axis xy;
	x = int(x);
	y = int(y);
	xy._x = x;
	xy._y = y;
	xy.x = _X+x;
	xy.y = _Y-y;
	return xy;
}
/*
draw.cpp
用于顶点、线段、文字消息的绘制
*/
#include<graphics.h>
#include"UI_set.h"
#include"draw.h"

// drawPoint(AXIS(0,0));
void drawPoint(axis xy,bool display, int *color){
   
	if(color!=NULL){
   
		settextcolor(RGB(color[0],color[1],color[2]));
		setfillcolor(RGB(color[0],color[1],color[2]));
	}else{
   
		settextcolor(RGB(255,255,255));
		setfillcolor(RGB(255,255,255));
	}
	solidcircle(xy.x,xy.y,_r);
	if(display){
   
		TCHAR s[20];
		_stprintf(s, _T("(%d,%d)"),xy._x,xy._y);
		outtextxy(xy.x+_r,xy.y+_r,s);
	}
}
// drawLine(AXIS(0,0),AXIS(100,0));
void drawLine(axis xy1, axis xy2, int *color){
   
	if(color!=NULL){
   
		setlinecolor(RGB(color[0],color[1],color[2]));
	}else{
   
		setlinecolor(RGB(255,255,255));
	}
	line(xy1.x, xy1.y, xy2.x, xy2.y);
}
//wchar s = L"他说";
//drawTip(s);
void drawTip(wchar_t *st, int *color){
   
	if(color!=NULL){
   
		settextcolor(RGB(color[0],color[1],color[2]));
		setfillcolor(RGB(color[0],color[1],color[2]));
	}else{
   
		settextcolor(RGB(255,255,255));
		setfillcolor(RGB(255,255,255));
	}
	TCHAR s[30];
	_stprintf(s, _T("·%s"),st);
	axis xy = AXIS(0.0, HEIGHT/2-22.0);

	settextstyle(22, 0, _T(FONTTYPE)); // 设置文字大小、字体
	outtextxy(xy.x, xy.y ,s);
	settextstyle(FONTSIZE, 0, _T(FONTTYPE)); // 设置文字大小、字体
}

主程序

注意引入mat.h

自建的矩阵库,适用于简单的矩阵运算
链接

#include<graphics.h>
#include<Windows.h>
#include<stdlib.h>
#include<math.h>
#include"mat.h"
#include "UI_set.h"
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值