《计算机图形学基础》OpenGL实现旋转变换

《计算机图形学基础》OpenGL实现旋转变换

#include <stdio.h>
#include <glut.h>
#include <math.h>

float jiaodu;
float deltax,deltay;
struct Point{
int x;
int y;
};
void MultiplyMatrixAxB(int a[3][3],float b[3][3],float c[3][3],int npt){
	int i,j,k,sum;
	for(i=0;i<npt;i++){
		for(k=0;k<3;k++){
			sum=0;
			for(j=0;j<3;j++)
				sum+=a[i][j]*b[j][k];
			c[i][k]=sum;
		}
	}
}

void Rotating2D(Point shape[],int n,float jd,Point Rshape[]){
jd=jd*3.1415926/180;
float rotate[3][3]={0,0,0,0,0,0,0,0,1};
rotate[0][0]=rotate[1][1]=cos(jd);
rotate[0][1]=sin(jd);
rotate[1][0]=-rotate[0][1];
int pnt[1][3]={0,0,1};
int i;
float result[1][3]={0};

for(i=0;i<n;i++){
	pnt[0][0]=shape[i].x;
	pnt[0][1]=shape[i].y;
	MultiplyMatrixAxB(pnt,rotate,result,1);       
	Rshape[i].x=result[0][0];
	Rshape[i].y=result[0][1];

}
  	float rotate1[3][3]={1,0,0,0,1,0,0,0,1};
	rotate1[1][1]=2;
	for(i=0;i<n;i++){
	pnt[0][0]=Rshape[i].x;
	pnt[0][1]=Rshape[i].y;
	MultiplyMatrixAxB(pnt,rotate1,result,1);
	Rshape[i].x=result[0][0];
	Rshape[i].y=result[0][1];
	}

	float rotate2[3][3]={0,0,0,0,0,0,0,0,1};
	rotate2[0][0]=rotate2[1][1]=cos(-jd);
	rotate2[0][1]=sin(-jd);
	rotate2[1][0]=-rotate2[0][1];

	for(i=0;i<n;i++){
	pnt[0][0]=Rshape[i].x;
	pnt[0][1]=Rshape[i].y;
	MultiplyMatrixAxB(pnt,rotate2,result,1);       
	Rshape[i].x=result[0][0];
	Rshape[i].y=result[0][1];
}       
}

void LineDDA(int x0,int y0,int x1,int y1){
	float dy,dx,x,y,m;
	if(x0>x1){
		int temp;
		temp=x0;
		x0=x1;
		x1=temp;
		temp=y0;
		y0=y1;
		y1=temp;
	}

        dx=x1-x0;
	dy=y1-y0;
	if(dx!=0){
		m=dy/dx;
		if(m<=1&&m>=-1){
			y=y0;
			for(int i=0;i<=(int)fabs((float)(x1-x0));i++){
				glVertex2i(i+x0,int(y+0.5));
				y+=m;
			}
		}
		if(m>1||m<-1){
			m=1/m;
			x=x0;
			for(int j=0;j<=(int)fabs((float)(y1-y0));j++){
				glVertex2i(int(x+0.5),j+y0);
				x+=m;
			}
		}
		
	}
	
	else{
		int x=x0,y;
                y=(y0<=y1)?y0:y1;
		int d=fabs((double)(y0-y1));
		while(d>=0){
			glVertex2i(x,y);
			y++;
			d--;
		}
	}
	
}

void bianhuan(){
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.0,0.0,0.0);
	glBegin(GL_POINTS);
        LineDDA(10,200,400,200);
	LineDDA(395,195,400,200);
	LineDDA(400,200,395,205);
	LineDDA(200,400,200,10);
	LineDDA(200,10,205,15);
	LineDDA(200,10,195,15);

	Point shape[4]={{0,0},{0,100},{100,100},{100,0}};
	Point Rshape[4]={0};
	Rotating2D(shape,4,jiaodu,Rshape);
	int i;
	for(i=0;i<4;i++){
		shape[i].x=200+shape[i].x;
		Rshape[i].x=200+Rshape[i].x;
		shape[i].y=200+shape[i].y;
		Rshape[i].y=200+Rshape[i].y;
	}

	LineDDA(shape[0].x,shape[0].y,shape[1].x,shape[1].y);
	LineDDA(shape[1].x,shape[1].y,shape[2].x,shape[2].y);
	LineDDA(shape[2].x,shape[2].y,shape[3].x,shape[3].y);
	LineDDA(shape[0].x,shape[0].y,shape[3].x,shape[3].y);

	LineDDA(Rshape[0].x,Rshape[0].y,Rshape[1].x,Rshape[1].y);
	LineDDA(Rshape[1].x,Rshape[1].y,Rshape[2].x,Rshape[2].y);
	LineDDA(Rshape[2].x,Rshape[2].y,Rshape[3].x,Rshape[3].y);
	LineDDA(Rshape[0].x,Rshape[0].y,Rshape[3].x,Rshape[3].y);
	glEnd();
	glFlush();
}

void main(int argc,char *argv[]){
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	printf("输入的角度(-360-360)");
	scanf("%f",&jiaodu);
	glutInitWindowPosition(50,100);
	glutInitWindowSize(500,500);
	glutCreateWindow("图形变换");
	glClearColor(1.0,1.0,1.0,0.0);
	glMatrixMode(GL_PROJECTION);
	gluOrtho2D(0.0,500.0,0.0,500.0);
	glutDisplayFunc(bianhuan);
	glutMainLoop();
	
}

 

转载于:https://my.oschina.net/finchxu/blog/1573061

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值