计算机图形学二维图形基本变换实验原理,【实验课件】二维及三维图形基本变换的实现...

实验二

二维及三维图形基本变换的实现

一、实验学时 4学时

二、实验类型 设计型实验

三、实验目的和要求

1、

掌握二维图形变换的原理,对一条直线实现二维基本变换(平移、错切、比例、旋转)。

2、

掌握三维图形变换的原理,画一个立方体,实现对该图形的三维基本变换(平移、错切、比例、旋转)。

四、实验内容

1、画一条直线,实现基本二维图形变换

2、画一个立方体,实现基本的二维图形变换。

五、源代码如下:

二维图形变换:

#include

#include

#include

int initjuzhen(m)

int m[3][3];

{ int i,j;

for(i=0;i<3;i++)

for(j=0;j<3;j++)

m[i][j]=0;

for(i=0;i<3;i++)

m[i][i]=1;

}

main()

{ int

x0,y0,x1,y1,i,j;

int a[3][3];

char key;

int graphdriver=DETECT;

int graphmode=0;

initgraph(&graphdriver,&graphmode,"

");

cleardevice();

x0=250;y0=120;x1=350;y1=220;

line(x0,y0,x1,y1);

for(;;)

{

outtextxy(10,10,"

->:right

^:up

v:down

Esc->exit");

key=getch();

initjuzhen(a);

switch(key)

{

case 75:

a[2][0]=-10;break;

case 77:

a[2][0]=10;break;

case 72:

a[2][1]=-10;break;

case 80:

a[2][1]=10;break;

case 27:

exit();break;

}

x0=x0*a[0][0]+y0*a[1][0]+a[2][0];

y0=x0*a[0][1]+y0*a[1][1]+a[2][1];

x1=x1*a[0][0]+y1*a[1][0]+a[2][0];

y1=x1*a[0][1]+y1*a[1][1]+a[2][1];

clearviewport();

line(x0,y0,x1,y1);

}

closegraph();

}

三维图形变换:

#include

#include

#include

#include

#include

#include

#define ZOOM_IN

0.9

#define ZOOM_OUT

1.1

int turn1[3];

typedef struct

{ float x;

float y;

float z;

}

point;

typedef struct

{ float x;

float y;

} point2d;

typedef struct

{

float x;

float y;

float h;

point biao[8];

} fanti;

void make_box(float x,float y,float h,fanti

*p)

{

p->x=x;p->y=y;p->h=h;

p->biao[0].x=x/2;

p->biao[0].y=y/2;

p->biao[0].z=h/2;

p->biao[1].x=-x/2;

p->biao[1].y=y/2;

p->biao[1].z=h/2;

p->biao[2].x=-x/2;

p->biao[2].y=-y/2;

p->biao[2].z=h/2;

p->biao[3].x=x/2;

p->biao[3].y=-y/2;

p->biao[3].z=h/2;

p->biao[4].x=x/2;

p->biao[4].y=y/2;

p->biao[4].z=-h/2;

p->biao[5].x=-x/2;

p->biao[5].y=y/2;

p->biao[5].z=-h/2;

p->biao[6].x=-x/2;

p->biao[6].y=-y/2;

p->biao[6].z=-h/2;

p->biao[7].x=x/2;

p->biao[7].y=-y/2;

p->biao[7].z=-h/2;

}

void trun2d(point *p,point2d *q)

{

q->x=p->x+p->z*cos(0.25);

q->y=p->y+p->z*sin(0.25);

}

void initm(float mat[][4])

{

int count;

for(count=0;count<4;count++)

{

mat[count][0]=0.;

mat[count][1]=0.;

mat[count][2]=0.;

mat[count][3]=0.;

mat[count][count]=1.;

}

return;

}

void transfrom(point *p,point *q,float

tm[][4])

{

float xu,yv,zw,h;

xu=tm[0][0]*p->x+tm[1][0]*p->y+tm[2][0]*p->z+tm[3][0];

yv=tm[0][1]*p->x+tm[1][1]*p->y+tm[2][1]*p->z+tm[3][1];

zw=tm[0][2]*p->x+tm[1][2]*p->y+tm[2][2]*p->z+tm[3][2];

p->x=xu;

p->y=yv;

p->z=zw;

return;

}

void rotationx(point *p,float alfa,float

tm[][4])

{

float

rad=0.0174532925;

initm(tm);

tm[1][1]=cos(rad*alfa);

tm[1][2]=sin(rad*alfa);

tm[2][1]=-tm[1][2];

tm[2][2]=tm[1][1];

return;

}

void rotationz(point *p,float alfa,float

tm[][4])

{

float

rad=0.0174532925;

initm(tm);

tm[0][0]=cos(rad*alfa);

tm[0][1]=sin(rad*alfa);

tm[1][0]=-tm[0][1];

tm[1][1]=tm[0][0];

return;

}

void rotationy(point *p,float alfa,float

tm[][4])

{

float

rad=0.0174532925;

initm(tm);

tm[0][0]=cos(rad*alfa);

tm[2][0]=sin(rad*alfa);

tm[0][2]=-tm[2][0];

tm[2][2]=tm[0][0];

return;

}

void adjust(point *p,point *q)

{ float

t[4][4];

switch(turn1[0])

{

case 1:

rotationy(p,2,t);

transfrom(p,q,t);

break;

case -1:

rotationy(p,-2,t);

transfrom(p,q,t);

break;

default: break;

}

switch(turn1[1])

{

case 1:

rotationz(p,2,t);

transfrom(p,q,t);

break;

case -1:

rotationz(p,-2,t);

transfrom(p,q,t);

break;

default: break;

}

switch(turn1[2])

{

case 1:

q->x=ZOOM_IN*p->x;

q->y=ZOOM_IN*p->y;

q->z=ZOOM_IN*p->z;

break;

case -1:

q->x=ZOOM_OUT*p->x;

q->y=ZOOM_OUT*p->y;

q->z=ZOOM_OUT*p->z;

break;

default: break;

}

}

void drawbox(fanti *p)

{ point2d

fan2d[8];

int

i;

for(i=0;i<=7;i++)

{

adjust(&p->biao[i],&p->biao[i]);

trun2d(&p->biao[i],&fan2d[i]);

fan2d[i].x+=300;

fan2d[i].y+=200;

}

clearviewport();

outtext("\n

-> :right\n

:up\n

v

:down");

moveto(0,10);

outtext("\n page up :zoom

in\n page

down :zoom out\n space

:Redraw\n

Esc :exit");

for(i=0;i<=3;i++)

{

if(i==3)

{

line(fan2d[i].x,fan2d[i].y,fan2d[0].x,fan2d[0].y);

line(fan2d[i+4].x,fan2d[i+4].y,fan2d[4].x,fan2d[4].y);

}

else

{

line(fan2d[i].x,fan2d[i].y,fan2d[i+1].x,fan2d[i+1].y);

line(fan2d[i+4].x,fan2d[i+4].y,fan2d[i+5].x,fan2d[i+5].y);

}

line(fan2d[i].x,fan2d[i].y,fan2d[i+4].x,fan2d[i+4].y);

}

}

void main()

{

int gd=DETECT,gm,i,j;

char key;

float x,y,h;

fanti a1;

x=100;

y=100;

h=100;

initgraph(&gd,&gm," ");

make_box(x,y,h,&a1);

drawbox(&a1);

for(;;)

{

turn1[0]=0;

turn1[1]=0;

turn1[2]=0;

key=getch();

switch(key)

{ case 77:turn1[0]=1;

break;

case 75:turn1[0]=-1;

break;

case 72:turn1[1]=1;

break;

case 80:turn1[1]=-1;

break;

case 73:turn1[2]=1;

break;

case

81:turn1[2]=-1;

break;

case

32:make_box(x,y,h,&a1); break;

case 27:

exit();break;

default

:key=0;break;

}

if(key!=0) drawbox(&a1);

}

closegraph();

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值