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

📄 90.c

📁 推荐刚刚开始学C语言的同志们看下
💻 C
字号:
#include <math.h>
#include <malloc.h>
#include <graphics.h>

float pToRadians( float a ) /*将角度转换成弧度*/
{
	a = a * M_PI / 180.0;
	return a;
}

void pFillArea( int num, wcPt2* pts, int color ) 
/*填充多边形, num为多边形的点数,pts为顶点集, color是指定的填充颜色*/
{
	int i;
	int* poly = (int*)malloc( num * 2 * sizeof( int ) );
	for( i = 0; i < num; i++ ) /*将顶点结构转换成整数数组*/
	{
		poly[ 2 * i ] = (int)pts[i].x;
		poly[ 2 * i + 1 ] = (int)pts[i].y;
	}
	setfillstyle( SOLID_FILL, color ); /*设置填充的格式*/
	fillpoly( num, poly ); /*填充*/
	free( poly );
}

void matrix3x3SetIdentity( Matrix3x3 m ) /*将矩阵m设置成单位矩阵*/
{
	int i, j;
	for( i = 0; i < 3; i++ )
		for( j = 0; j < 3; j++ )
			m[i][j] = ( i == j );
}

/*将矩阵A,B相乘,结果存放在B中*/
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++ )
			b[r][c] = tmp[r][c];
}

void translate2( int tx, int ty ) 
/*平移,tx是x坐标的移动距离,ty是y坐标的移动距离*/
{
	Matrix3x3 m;
	
	matrix3x3SetIdentity( m );
	m[0][2] = tx;
	m[1][2] = ty;
	matrix3x3PreMultiply( m, theMatrix );
}

void scale2( float sx, float sy, wcPt2 refpt )
/*寻边,按照给定的系数sx, sy放大,找出边*/
{
	Matrix3x3 m;
	
	matrix3x3SetIdentity( m );
	m[0][0] = sx;
	m[0][2] = ( 1 - sx ) * refpt.x;
	m[1][1] = sy;
	m[1][2] = ( 1 - sy ) * refpt.y;
	matrix3x3PreMultiply( m, theMatrix );
}

void rotate2( float a, wcPt2 refpt )
/*按照给定的参考点旋转*/
{
	Matrix3x3 m;
	
	matrix3x3SetIdentity( m );
	a = pToRadians( a );
	m[0][0] = cos( a );
	m[0][1] = -sin( a );
	m[0][2] = refpt.x * ( 1 - cos( a ) ) + refpt.y * sin( a );
	m[1][0] = sin( a );
	m[1][1] = cos( a );
	m[1][2] = refpt.y * ( 1 - cos( a ) ) + refpt.x * sin( a );
	matrix3x3PreMultiply( m, theMatrix );
}

void transformPoints2( int npts, wcPt2* pts )
/*进行最后的计算*/
{
	int k;
	float tmp;
	
	for( k = 0; k < npts; k++ )
	{
		tmp = 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];
		pts[k].x = tmp;
	}
}

void main( int argc, char** argv )
{
	int gdriver = DETECT, gmode;
	
	wcPt2 pts[3] = { 50.0, 50.0, 150.0, 50.0, 100.0, 150.0 };
	wcPt2 refPt = { 100.0, 100.0 };
	
	registerbgidriver( EGAVGA_driver );
	initgraph( &gdriver, &gmode, "e:\\tc\\bgi" );
	
	setbkcolor( WHITE );
	pFillArea( 3, pts, BLUE );
	matrix3x3SetIdentity( theMatrix );
	scale2( 0.5, 0.5, refPt );
	rotate2( 90.0, refPt );
	translate2( 0, 150 );
	transformPoints2( 3, pts ); 
	pFillArea( 3, pts, BLUE );
	getch();
	closegraph();
}

⌨️ 快捷键说明

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