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

📄 transmatrix.cpp

📁 图形绘制算法实现、区域填充算法的实现过程、图形裁减算法的实现等
💻 CPP
字号:
// TransMatrix.cpp: implementation of the TransMatrix class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CreatePen.h"
#include "TransMatrix.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

TransMatrix::TransMatrix()
{
	Matrix3x3SetIdentity(theMatrix) ;
}

TransMatrix::~TransMatrix()
{

}

void TransMatrix::Matrix3x3SetIdentity(Matrix3x3 m)
{
	int i, j;
	for(i= 0; i< 3; i++)
	for(j= 0; j< 3; j++)
		m[i][j] = (i == j);
}

//矩阵相乘 a*b , putting result in b
void TransMatrix::Matrix3x3PreMultiply(Matrix3x3 a, Matrix3x3 b)
{
	int r, c;
	Matrix3x3 tmp;

	for(r= 0; r< 3; r++)
	for(c= 0; c< 3; c++)
		tmp[r][c] = a[r][0] * b[0][c] + a[r][1] * b[1][c] + a[r][2] * b[2][c];

	for(r= 0; r< 3; r++)
	for(c= 0; c< 3; c++)
		theMatrix[r][c] = tmp[r][c];
}

void TransMatrix::Translate2(int tx, int ty)
{
	Matrix3x3 m;
	Matrix3x3SetIdentity(m);
	m[0][2] = tx;
	m[1][2] = ty;

	Matrix3x3PreMultiply(m, theMatrix);
}

void TransMatrix::Scale2(float sx, float sy, POINT scalePt)
{
	Matrix3x3 m;
	Matrix3x3SetIdentity(m);
	m[0][0] = sx;
	m[0][2] = (1 - sx) * scalePt.x;
	m[1][1] = sy;
	m[1][2] = (1 - sy) * scalePt.y;
	Matrix3x3PreMultiply(m, theMatrix);
}

void TransMatrix::Rotate2(double angle, POINT RotatePt)
{
	double pi = 3.1415926535;
	Matrix3x3 m;
	Matrix3x3SetIdentity(m);
	angle = angle * pi / 180;      //角度到弧度的转换
	
	m[0][0] = 0.000000;//cos(1.570796);
	m[0][1] = - sin(angle);
	m[0][2] = RotatePt.x * (1 - cos(angle)) + RotatePt.y * sin(angle);
	m[1][0] = sin(angle);
	m[1][1] = 0.000000;//cos(angle);
	m[1][2] = RotatePt.y * (1 - cos(angle)) - RotatePt.x * sin(angle);
	Matrix3x3PreMultiply(m, theMatrix);
}

void TransMatrix::TransformPoints(int npts, POINT* pts)
{
	int k;
	for(k= 0; k< npts; k++){
		pts[k].x = theMatrix[0][0] * pts[k].x + theMatrix[0][1] * pts[k].y + theMatrix[0][2];
		pts[k].y = theMatrix[1][0] * pts[k].x + theMatrix[1][1] * pts[k].y + theMatrix[1][2];
	}
}

⌨️ 快捷键说明

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