báo cáo thực hành đồ họa máy tính - Pdf 13

TRƯỜNG ĐẠI HỌC BK ĐÀ NẴNG
KHOA CÔNG NGHỆ THÔNG TIN

BÁO CÁO THỰC HÀNH
ĐỒ HỌA MÁY TÍNH

Giáo viên : Nguyễn Văn Nguyên
Sinh viên : Trịnh Hoàng Long
Lớp : 11T2
Nhóm : 11B
MSSV : 102110212
Đà Nẵng, 10/04/2014
I. Buổi thực hành 1:
1. Cài đặt và sử dụng thư viện graphics.h:
1.1. Download 2 file sau đây về máy:
+ File graphics.h để ở thư mục C:\Dev-Cpp\include
+ File libbgi.a để ở thư mục C:\Dev-Cpp\lib
1.2. Khởi động Dev C++, vào File >New >Project >Empty Project (Nhớ chọn
C++ Project) >OK
Đặt 1 cái tên cho phù hợp và lưu vào đâu đó.
Nhấn chuột phải lên cái project vừa tạo. >New File hoặc chọn mục New File
trong menu Project
Một file mới được tạo ra trong Project. Trước khi viết code lưu lại bằng cách
Nhấn Ctrl + S hoặc File >Save.
Nhấn Alt + P hoặc mục Project Options trong menu Project >Chọn thẻ
Parameters
> Gõ chính xác những dòng sau vào khung Linker:
-lbgi
-lgdi32
-lcomdlg32
-luuid


line(400,280,450,280);
line(450,280,450,330);
line(450,330,400,330);
line(400,330,400,280);
}
int main(){
init();
ve();
system("PAUSE");
return EXIT_SUCCESS;
}
Demo:
II. Buổi thực hành 2:
1.Vẽ các đường tròn đồng tâm và tô màu chúng: Sử dụng thuật toán Michener.
Code:
#include <cstdlib>
#include <iostream>
#include <graphics.h>
#include <math.h>
using namespace std;
void Mcircle(int R,int x0,int y0){
int x,y,p,c;
p = 1 - R;
c=getcolor();
x= 0;y=R;
while(x<=y){
putpixel(x+x0,y+y0,c);
putpixel(-x+x0,y+y0,c);
putpixel(x+x0,-y+y0,c);

return EXIT_SUCCESS;
}
Demo:
III. Buổi thực hành 3:
1.Thuật toán xén hình:
Code:
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
//Kieu cua so va bien w toan cuc
struct wind
{
float l,t,r,b;
}w;
// Kieu diem
struct ptype
{
float x,y;
};
// Kieu ma
struct code
{
int l,t,r,b;
};
// Ham ma hoa diem p sang ma c
struct code Encode(struct ptype p)
{
struct code c;
c.l=p.x < w.l;
c.t=p.y < w.t;

tmpc=c1; c1=c2; c2=tmpc;
}
if(p1.x == p2.x)//doan thang dung
{
if(c1.t) p1.y = w.t;
else p1.y = w.b;
}
else
{
m=(p2.y - p1.y)/(p2.x - p1.x);
if(c1.l)
{
p1.y += m*(w.l - p1.x);
p1.x =w.l;
}
else if(c1.t)
{
p1.x +=(w.t - p1.y)/m;
p1.y = w.t;
}
else if(c1.r)
{
p1.y -=m*(w.r -p1.x);
p1.x = w.r;
}
else
{
p1.x -=(p1.y-w.b)/m;
p1.y = w.b;
}

a. Vẽ bằng lệnh putpixel(x,y,c):
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<math.h>
int main(){
float x,y;
initwindow( 800 , 600 , "09T4.no1" );
moveto(100,100);

for (x=-3.14;x<=3.14;x=x+0.001){
y=30*sin(x);
putpixel(int(100+30*x),int(100+y),6);
}
getch();
}
Demo:
b. Vẽ bằng lệnh lineto(x,y):
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main(){
float x,y,n=0.01;
initwindow( 800 , 600 , "09T4.no1" );
moveto(3,100);

for (x=-3.14;x<=3.14;x=x+n){
y=sin(x);

if(m==1)
{ outtextxy(0,0,"Truong hop m==1");
for(x=x1,y=y1;x<=x2;x++,y++)
{
putpixel(x,y,c);
}
}
else if(m==-1)
{ outtextxy(0,0,"Truong hop m==-1");
for(x=x1,y=y1;x<=x2;x++,y )
putpixel(x,y,c);
}
else if(m<-1)
{ outtextxy(0,0,"Truong hop m<-1");
ekt=dx+dx;
e=ekt+dy;
et=e+dy;
for (x=x2,y=y2;y<=y1;y++)
{
putpixel(x,y,c);
if (e<0) e+=ekt;
else
{
x ;
e+=et;
}
}
}
else if((m>-1)&&(m<0))
{ outtextxy(0,0,"Truong hop -1<m<0");

else {
outtextxy(0,0,"Truong hop 0<m<1");
ekt=dy+dy;
e=ekt-dx;
et=e-dx;
for(x=x1,y=y1;x<=x2;x++)
{
putpixel(x,y,c);
if(e<0) e+=ekt;
else
{
y++;
e+=et;
}
}
}
}
}
int main()
{
initwindow(800,600,"WinBGIm");
int x1,y1,x2,y2,mau;
printf("Nhap vao toa do x1:");
scanf("%d",&x1);
printf("Nhap vao toa do y1:");
scanf("%d",&y1);
printf("Nhap vao toa do x2:");
scanf("%d",&x2);
printf("Nhap vao toa so y2:");
scanf("%d",&y2);

setcolor(mau);
veduongtron(x,y,R,mau);
delay(3000);
mau=4;
setcolor(mau);
circle(x,y,R);
getch();
closegraph();
}
void veduongtron(int x,int y,int R,int mau)
{int a,b,d;
putpixel(x,y,15);
d=3-2*R;
for(a=0,b=R;a<=b;a++)
{putpixel(x+a,y+b,mau);
putpixel(x-a,y+b,mau);
putpixel(x+a,y-b,mau);
putpixel(x-a,y-b,mau);
putpixel(x+b,y+a,mau);
putpixel(x-b,y+a,mau);
putpixel(x-b,y-a,mau);
putpixel(x+b,y-a,mau);
if(d<0) d+=4*a+6;
else {
d+=4*(a-b)+10;
b ;
}
}
}
3. Hình học Fractal

d-=90*S;
Dragon(n-1,l*FACT,d,-1);
}
else {setcolor(rand()
%5+13);linerel(int(l*cos(d*RADS)),int(l*sin(d*RADS))); }
}
Demo:
4. Phép biến đổi 2 chiều
a. Thư viện Affine:
#include<math.h>
typedef float Point[2];
typedef float Affine[3][3];
void Change(Point A,Affine &B){
B[0][0]=A[0]; B[0][1]=A[1]; B[0][2]=1;
}
void Cover(Affine &A,Affine B){
A[0][0]=B[0][0]; A[0][1]=B[0][1];
}

void MatMul(Affine A,Affine B,Affine &C,int m,int n){
int i,j,k;
for(i=0;i<m;i++)
for(j=0;j<n;j++){
C[i][j]=0;
for(k=0;k<n;k++) C[i][j]+=A[i][k]*B[k][j];
}
}
void Quay(Affine &T,float fi){
T[0][0]=cos(fi); T[0][1]=sin(fi); T[0][2]=0.0;
T[1][0]=-sin(fi); T[1][1]=cos(fi); T[1][2]=0.0;

Tich(T1,Q,tam);
Tich(tam,T2,T);
}
b. Vẽ quạt bằng Affine:
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <dos.h>
#include "affine.h"
#define RAD 0.01745329
int x,y,goc=0;
int tm1,tm2,tm3,tm4,c[6];
Affine A1,B1,C1,D1;
void Vemayquat(int x,int y,int mau1,int mau2)
{
//Ve may quat
setcolor(mau1);
rectangle(x,y,x+140,y-20);
setfillstyle(9,1);
floodfill(x+1,y-1,mau1);
rectangle(x+50,y-20,x+90,y-80);
setfillstyle(1,3);
floodfill(x+52,y-21,mau1);
rectangle(x+65,y-80,x+75,y-180);
setfillstyle(4,7);
floodfill(x+66,y-81,mau1);
rectangle(x+68,y-26,x+72,y-32);
setfillstyle(1,15);
floodfill(x+69,y-27,mau1);
rectangle(x+68,y-39,x+72,y-44);

setfillstyle(6,mau2);
fillpoly(3,c);
//Canh quat thu hai
QuayTamO(A1,(goc+120)*RAD,c[0],c[1]);
MatMul(B1,A1,C1,1,3);
line(c[0],c[1],C1[0][0],C1[0][1]);

QuayTamO(A1,(goc+150)*RAD,c[0],c[1]);
MatMul(B1,A1,D1,1,3);
line(c[0],c[1],D1[0][0],D1[0][1]);
line(C1[0][0],C1[0][1],D1[0][0],D1[0][1]);
c[2]=C1[0][0]; c[3]=C1[0][1];
c[4]=D1[0][0]; c[5]=D1[0][1];
setfillstyle(6,mau2);
fillpoly(3,c);
//Canh quat thu ba
QuayTamO(A1,(goc+240)*RAD,c[0],c[1]);
MatMul(B1,A1,C1,1,3);
line(c[0],c[1],C1[0][0],C1[0][1]);

QuayTamO(A1,(goc+270)*RAD,c[0],c[1]);
MatMul(B1,A1,D1,1,3);
line(c[0],c[1],D1[0][0],D1[0][1]);
line(C1[0][0],C1[0][1],D1[0][0],D1[0][1]);
c[2]=C1[0][0]; c[3]=C1[0][1];
c[4]=D1[0][0]; c[5]=D1[0][1];
setfillstyle(6,mau2);
fillpoly(3,c);
}
int main()


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status