1. Trang chủ
  2. » Công Nghệ Thông Tin

Đồ họa máy tính Mô phỏng đồ họa máy tính

18 230 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 18
Dung lượng 172,31 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Code một số các phương pháp vẽ hình trên nền đồ họa C++ hay dev C .Bộ bài tập lập trình cơ bản về đồ họa trong C++ , C .Bài tập lớn về đồ họa máy tính cũng như chi tiết hướng dẫn làm bài tập đồ họa máy tính cho người mới học . Chi tiết liên hệ anh9306gmail.com.

Trang 1

BÀI TẬP THỰC HÀNH ĐỒ HỌA MÁY TÍNH_37DHTIN2

1 TRANSLATION – 2D

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

void main()

{

int ch;

int tx,ty;

int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\tc\\bgi");

setcolor(6);

printf("\n\n\tDoi tuong: ");

rectangle(100,150,150,100);

setcolor(2);

outtextxy(100,10,"TRANSLATION");

outtextxy(100,20," -");

printf("\n\ttx: ");

scanf("%d",&tx);

printf("\tty: ");

scanf("%d",&ty);

getch();

cleardevice();

outtextxy(100,10,"TRANSLATION");

outtextxy(100,20," -");

rectangle(100,150,150,100);

delay(1000);

rectangle(100+tx,150+ty,150+tx,100+ty);

Trang 2

getch();

closegraph();

}

2 Rotation, Scaling, Translation

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

void translate();

void scale();

void rotate();

void main()

{

int ch;

int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\tc\\bgi");

setcolor(6);

outtextxy (100,88,"Object.");

rectangle(100,150,150,100);

printf(" -MENU -");

printf("\n 1)Translate\n 2)Scale\n 3)Rotate"); printf("\nHay nhan chon: ");

scanf("%d",&ch);

cleardevice();

switch(ch)

{

case 1: translate();

break;

case 2: scale();

Trang 3

break;

case 3: rotate();

break;

default: printf("Nhap sai lua chon"); break;

}

getch();

closegraph();

}

void translate()

{

int tx,ty;

setcolor(2);

outtextxy(240,10,"TRANSLATION"); outtextxy(238,20," -");

printf("\nNhap tx: ");

scanf("%d",&tx);

printf("\nNhap ty: ");

scanf("%d",&ty);

cleardevice();

rectangle(100,150,150,100);

printf("\nAfter Translation");

rectangle(100+tx,150+ty,150+tx,100+ty); }

void scale()

{

int sx,sy;

setcolor(2);

outtextxy(240,10,"SCALING");

Trang 4

outtextxy(238,20," -");

printf("\nNhap sx: ");

scanf("%d",&sx);

printf("\nNhap sy: ");

scanf("%d",&sy);

cleardevice();

rectangle(100,150,150,100);

printf("\nAfter Scaling");

rectangle(100*sx,150*sy,150*sx,100*sy); }

void rotate()

{

float theta;

int x1,x2,x3,x4;

int y1,y2,y3,y4;

int ax1,ax2,ax3,ax4,ay1,ay2,ay3,ay4; int refx,refy;

printf("\nNhap he so theta: ");

scanf("%f",&theta);

theta=theta*(3.14/180);

cleardevice();

setcolor(2);

outtextxy(240,10,"ROTATE");

outtextxy(238,20," -");

refx=100;

refy=100;

x1=100;

y1=100;

x2=150;

Trang 5

y2=100;

x3=150;

y3=150;

x4=100;

y4=150;

ax1=refy+(x1-refx)*cos(theta)-(y1-refy)*sin(theta); ay1=refy+(x1-refx)*sin(theta)+(y1-refy)*cos(theta); ax2=refy+(x2-refx)*cos(theta)-(y2-refy)*sin(theta); ay2=refy+(x2-refx)*sin(theta)+(y2-refy)*cos(theta); ax3=refy+(x3-refx)*cos(theta)-(y3-refy)*sin(theta); ay3=refy+(x3-refx)*sin(theta)+(y3-refy)*cos(theta); ax4=refy+(x4-refx)*cos(theta)-(y4-refy)*sin(theta); ay4=refy+(x4-refx)*sin(theta)+(y4-refy)*cos(theta); rectangle(100,150,150,100);

line(ax1,ay1,ax2,ay2);

line(ax2,ay2,ax3,ay3);

line(ax3,ay3,ax4,ay4);

line(ax4,ay4,ax1,ay1);

}

3 CohenSutherlandClipping

#include <stdio.h>

#include <graphics.h>

#include <conio.h>

#include <math.h>

#include <process.h>

#define TRUE 1

#define FALSE 0

typedef unsigned int outcode;

outcode CompOutCode(float x,float y);

Trang 6

enum

{

TOP = 0x1,

BOTTOM = 0x2,

RIGHT = 0x4,

LEFT = 0x8

};

float xmin,xmax,ymin,ymax;

void clip(float x0,float y0,float x1,float y1)

{

outcode outcode0,outcode1,outcodeOut;

int accept = FALSE,done = FALSE;

outcode0 = CompOutCode(x0,y0);

outcode1 = CompOutCode(x1,y1);

do

{

if(!(outcode0|outcode1))

{

accept = TRUE;

done = TRUE;

}

else

if(outcode0 & outcode1)

done = TRUE;

else

{

float x,y;

outcodeOut = outcode0?outcode0:outcode1; if(outcodeOut & TOP)

Trang 7

{

x = x0+(x1-x0)*(ymax-y0)/(y1-y0);

y = ymax;

}

else if(outcodeOut & BOTTOM) {

x = x0+(x1-x0)*(ymin-y0)/(y1-y0);

y = ymin;

}

else if(outcodeOut & RIGHT)

{

y = y0+(y1-y0)*(xmax-x0)/(x1-x0);

x = xmax;

}

else

{

y = y0+(y1-y0)*(xmin-x0)/(x1-x0);

x = xmin;

}

if(outcodeOut==outcode0)

{

x0 = x;

y0 = y;

outcode0 = CompOutCode(x0,y0); }

else

{

x1 = x;

y1 = y;

Trang 8

outcode1 = CompOutCode(x1,y1);

}

}

}while(done==FALSE);

if(accept)

line(x0,y0,x1,y1);

outtextxy(150,20,"POLYGON AFTER CLIPPING"); rectangle(xmin,ymin,xmax,ymax);

}

outcode CompOutCode(float x,float y)

{

outcode code = 0;

if(y>ymax)

code|=TOP;

else if(y<ymin)

code|=BOTTOM;

if(x>xmax)

code|=RIGHT;

else if(x<xmin)

code|=LEFT;

return code;

}

void main( )

{

float x1,y1,x2,y2;

int gdriver = DETECT, gmode, n,poly[14],i;

clrscr( );

printf("Nhap so canh da giac: ");

scanf("%d",&n);

Trang 9

printf("\nNhap toa do da giac: \n");

for(i=0;i<2*n;i++)

{

scanf("%d",&poly[i]);

}

poly[2*n]=poly[0];

poly[2*n+1]=poly[1];

printf("Nhap toa do vung cua so: \n");

scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax); initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

outtextxy(150,20,"POLYGON BEFORE CLIPPING"); drawpoly(n+1,poly);

rectangle(xmin,ymin,xmax,ymax);

getch( );

cleardevice( );

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

clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]); getch( );

restorecrtmode( );

}

4 Bezier

#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <math.h>

void bezier(int x[4],int y[4])

{

int gd=DETECT,gm,i;

double u;

Trang 10

initgraph(&gd,&gm,"C:\\TC\\BGI");

setcolor(15);

outtextxy(400,400,"-> Diem dieu khien"); outtextxy(400,430,"-> Da dien");

outtextxy(400,460,"-> Bezier Curve");

setcolor(RED);

setfillstyle(1,RED);

circle(390,400,5);

floodfill(390,400,RED);

setcolor(YELLOW);

setfillstyle(1,YELLOW);

circle(390,430,5);

floodfill(390,430,YELLOW);

setcolor(WHITE);

setfillstyle(1,WHITE);

circle(390,460,5);

floodfill(390,460,WHITE);

delay(10);

for(u=0.0;u<1.0;u+=0.0002)

{

double xu= pow(1-u,3)*x[0]+3*u*pow(1-u,2)*x[1]+3*pow(u,2)*(1-u)*x[2]+pow(u,3)*x[3];

double yu= pow(1-u,3)*y[0]+3*u*pow(1-u,2)*y[1]+3*pow(u,2)*(1-u)*y[2]+pow(u,3)*y[3];

putpixel(xu,yu,WHITE);

delay(1);

}

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

{

Trang 11

setcolor(RED);

setfillstyle(1,RED);

circle(x[i],y[i],4);

floodfill(x[i],y[i],RED);

delay(1);

}

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

{

setcolor(YELLOW);

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

}

}

void main()

{

int x[4],y[4],i;

clrscr();

printf("\n\n====Bezier Curve====\n\n"); printf("Nhap toa do x,y 4 diem kiem soat :\n"); for(i=0;i<4;i++)

{

printf("x%d va y%d la:",i+1,i+1);

scanf("%d %d",&x[i],&y[i]);

}

bezier(x,y);

getch();

closegraph();

}

5 3D- Rotation, Scaling, Translation

Trang 12

#include<stdio.h>

#include<math.h>

#include<conio.h>

#include<graphics.h>

int gd=DETECT,gm,i,ch,sh,a1,b1,c1;

float temp,theta,temp1;

float a,b,c;

double x1,x2,y1,y2;

void draw_cube(double edge[20][3])

{

initgraph(&gd,&gm,"c:\\TC\\bgi");

clearviewport();

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

{

x1=edge[i][0]+edge[i][2]*(cos(2.3562)); y1=edge[i][1]-edge[i][2]*(sin(2.3562));

x2=edge[i+1][0]+edge[i+1][2]*(cos(2.3562)); y2=edge[i+1][1]-edge[i+1][2]*(sin(2.3562)); line(x1+320,240-y1,x2+320,240-y2);

} line(320,240,320,25);

line(320,240,550,240);

line(320,240,150,410);

getch();

closegraph();

}

void draw_cube1(double edge1[20][3])

{

initgraph(&gd,&gm,"c:\\TC\\bgi");

Trang 13

clearviewport();

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

{

x1=edge1[i][0]+edge1[i][2]*(cos(2.3562)); y1=edge1[i][1]-edge1[i][2]*(sin(2.3562));

x2=edge1[i+1][0]+edge1[i+1][2]*(cos(2.3562)); y2=edge1[i+1][1]-edge1[i+1][2]*(sin(2.3562)); line(x1+320,240-y1,x2+320,240-y2);

}

line(320,240,320,25);

line(320,240,550,240);

line(320,240,150,410);

getch();

closegraph();

}

void scale(double edge1[20][3],double edge[20][3])

{

printf("Nhap he so Scale : ");

scanf("%f %f %f",&a,&b,&c);

initgraph(&gd,&gm,"c:\\TC\\bgi");

clearviewport();

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

{

edge1[i][0]=edge[i][0]*a;

edge1[i][1]=edge[i][1]*b;

edge1[i][2]=edge[i][2]*c;

}

draw_cube1(edge1);

closegraph();

Trang 14

}

void translate(double edge1[20][3],double edge[20][3]) {

printf("Nhap he so Translation: ");

scanf("%d %d %d",&a1,&b1,&c1);

initgraph(&gd,&gm,"c:\\TC\\bgi");

clearviewport();

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

{

edge1[i][0]=edge[i][0]+a1;

edge1[i][1]=edge[i][1]+b1;

edge1[i][2]=edge[i][2]+c1;

}

draw_cube1(edge1);

closegraph();

}

void rotate(double edge1[20][3],double edge[20][3]) {

clrscr();

printf("-=[ Thuc don Rotate]=-\n");

printf("1:==> X-Axis \n");

printf("2:==> Y-Axis \n");

printf("3:==> Z-Axis \n");

printf(" Hay chon so: ");

scanf("%d",&ch);

printf("Nhap goc: ");

scanf("%f",&theta);

switch(ch)

{

Trang 15

case 1:

theta=(theta*3.14)/180;

for(i=0;i<20;i++) {

edge1[i][0]=edge[i][0];

temp=edge[i][1];

temp1=edge[i][2];

edge1[i][1]=temp*cos(theta)-temp1*sin(theta); edge1[i][2]=temp*sin(theta)+temp1*cos(theta); }

draw_cube1(edge1);

break;

case 2:

theta=(theta*3.14)/180;

for(i=0;i<20;i++) {

edge1[i][1]=edge[i][1];

temp=edge[i][0];

temp1=edge[i][2];

edge1[i][0]=temp*cos(theta)+temp1*sin(theta); edge1[i][2]=-temp*sin(theta)+temp1*cos(theta); }

draw_cube1(edge1);

break;

case 3:

theta=(theta*3.14)/180;

for(i=0;i<20;i++) {

edge1[i][2]=edge[i][2];

Trang 16

temp=edge[i][0];

temp1=edge[i][1];

edge1[i][0]=temp*cos(theta)-temp1*sin(theta); edge1[i][1]=temp*sin(theta)+temp1*cos(theta); }

draw_cube1(edge1);

break;

} }

void main()

{

int choice;

double edge1[20][3],edge[20][3]={

100,0,0, 100,100,0, 0,100,0, 0,100,100, 0,0,100, 0,0,0, 100,0,0, 100,0,100, 100,75,100, 75,100,100, 100,100,75, 100,100,0, 100,100,75, 100,75,100, 75,100,100, 0,100,100,

Trang 17

0,100,0, 0,0,0, 0,0,100, 100,0,100 };

while(1)

{

clrscr();

printf("3D BASIC TRANSFORMATIONS\n"); printf("1:==> Draw Cube\n");

printf("2:==> Translate\n");

printf("3:==> Scaling \n");

printf("4:==> Rotation \n");

printf("5:==> Exit \n");

printf("Hay chon so: ");

scanf("%d",&choice);

switch(choice) {

case 1:

draw_cube(edge);

break;

case 2:

translate(edge1,edge);

break;

case 3:

scale(edge1,edge);

break;

Trang 18

case 4:

rotate(edge1,edge);

break;

case 5:

exit(0);

default:

printf(" Nhap sai Nhan phim bat ky!!! "); exit();

getch();

break;

}

getch();

closegraph();

}

}

Ngày đăng: 02/12/2015, 23:32

TỪ KHÓA LIÊN QUAN

w