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
Trang 1TRƯỜ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
Trang 2I 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
-loleaut32
-lole32
2 Dùng hàm line() để vẽ 1 ngôi nhà đơn giản:
Code:
#include <cstdlib>
#include <iostream>
#include <graphics.h>
using namespace std;
void init(){
int gd = DETECT,gm;
initgraph(&gd,&gm,"");
}
void ve(){
line(200,100,400,100);
line(100,200,500,200);
line(200,100,100,200);
line(400,100,500,200);
line(100,200,100,400);
line(100,400,500,400);
line(500,400,500,200);
line(300,400,300,280);
line(250,280,350,280);
line(250,280,250,400);
Trang 3line(350,280,350,400);
line(150,280,200,280); line(200,280,200,330); line(200,330,150,330); line(150,330,150,280);
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:
Trang 4II 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);
putpixel(-x+x0,-y+y0,c);
putpixel(y+x0,x+y0,c);
putpixel(y+x0,-x+y0,c);
putpixel(-y+x0,x+y0,c);
putpixel(-y+x0,-x+y0,c);
if(p<0) p+=2*x + 3;
else { p+=2*(x-y)+5;y ;}
x++;
}
}
void totron(int xc, int yc, int R, int c)
{
for (int i=xc-R; i<xc+R; i++)
{ for (int j=yc-R; j<yc+R; j++)
{ float d= sqrt((float)(xc-i)*(xc-i)+ (float)(yc-j)*(yc-j));
if (d<50) putpixel(i,j,11);
else if (d<R && d>R-15) putpixel(i,j,c);
} }
}
int main(int argc, char *argv[])
{
int c=1;
initwindow(500,400);
for (int R=50;R<200;R=R+15){
Trang 5
Mcircle(R,250,200);
totron(250,200,R,c);
c++;}
system("PAUSE");
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
{
Trang 6float 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;
c.r=p.x > w.r;
c.b=p.y > w.b;
return c;
};
//Ham kiem tra p co thuoc w hay ko
int InW(struct ptype p)
{
struct code c;
c= Encode(p);
return !(c.l||c.t||c.r||c.b);
}
// Thuat toan
int clip(ptype &p1, ptype &p2)
{
ptype tmpp;
code c1,c2,tmpc;
int in1,in2;
float m;
while(1)
{
c1= Encode(p1);
c2= Encode(p2);
in1=InW(p1);
in2=InW(p2);
if(in1 && in2){return 1;}
if((c1.l && c2.l)||(c1.t && c2.t)||(c1.r && c2.r)||(c1.b && c2.b)) return 0; if(in1)
{
tmpp =p1; p1=p2; p2=tmpp;
tmpc=c1; c1=c2; c2=tmpc;
}
if(p1.x == p2.x)//doan thang dung
Trang 7{
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;
}
}
}
}
main()
{
int gd=0,gm;
struct ptype p1,p2;
initgraph(&gd,&gm,"");
w.l=100;
w.t=150;
w.r=300;
w.b=350;
rectangle(int (w.l), int (w.t), int (w.r), int (w.b)); p1.x=90;
p1.y=100;
p2.x=300;
p2.y=400;
line(int (p1.x), int (p1.y), int (p2.x), int (p2.y)); clip(p1,p2);
Trang 8setcolor(RED);
line(int (p1.x), int (p1.y), int (p2.x), int (p2.y));
getch();
closegraph();
}
Demo:
2.Thư viện Affine.h:
Thầy cho về tự tìm hiểu ở phái dưới-Phần “Bài tập trên trang chủ”
Bài tập trên trang chủ:
I Khái quát hệ thống đồ họa:
1 Viết chương trình vẽ đồ thị hàm số y=sin(x) với -x
a Vẽ bằng lệnh putpixel(x,y,c):
Trang 9#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:
Trang 10b 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);
delay(5);
lineto(100+30*x,100+30*y);
} getch();
}
Demo:
Trang 112 Các thuật toán cơ bản:
a Thuật toán Bresenham:
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void veduongthang(int x1,int y1,int x2,int y2,int mau) {
int x,y,dx,dy,e,et,ekt,c=mau;float m;
dx=x2-x1;
dy=y2-y1;
if (dx==0)
{ outtextxy(0,0,"Truong hop dx==0");
for(x=x1,y=(y1<y2)?y1:y2;(y<=y2||y<=y1);y++) putpixel(x,y,c);
} else if (dy==0)
{ outtextxy(0,0,"Truong hop dy==0");
for(x=(x1<x2)?x1:x2,y=y1;(x<=x2)||(x<=x1);x++)
putpixel(x,y,c);
} else if (dx<0) veduongthang(x2,y2,x1,y1,mau);
else { m=(float)dy/dx;
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
{
Trang 12x ;
e+=et;
} }
}
else if((m>-1)&&(m<0))
{ outtextxy(0,0,"Truong hop -1<m<0");
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;
} }
}
else if (m>1)
{
outtextxy(0,0,"Truong hop m>1");
e=2*dx-dy;
et=2*dx-2*dy;
ekt=2*dx;
for(x=x1,y=y1;y<=y2;y++)
{
putpixel(x,y,c);
if(e<0) e+=ekt;
else {
x++;
e+=et;
}
}
}
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++)
{
Trang 13if(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);
mau=5;
setcolor(mau);
veduongthang(x1,y1,x2,y2,mau);
mau=3;
setcolor(mau);
delay(3000);
line(x1,y1,x2,y2);
getch();
closegraph();
}
b Thuật toán Michener:
#include<stdlib.h>
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
void veduongtron(int x,int y,int R,int mau); int main()
{
initwindow(800,600,"WinBGIm");
Trang 14int x,y,R,mau;
printf("Nhap vao toa do cua x ");
scanf("%d",&x);
printf("Nhap vao toa do cua y ");
scanf("%d",&y);
printf("Nhap vao ban kinh ");
scanf("%d",&R);
int gd=DETECT,gm=0;
initgraph(&gd,&gm,"C:\\TC\\BGI"); mau=6;
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
a Đường cong Knock:
void K(int n,float l, float d){
if(n>0) {
Trang 15K(n-1,l/3,d);
d+=60;
K(n-1,l/3,d);
d-=120;
d+=60;
K(n-1,l/3,d);
}
else linerel(int(l*cos(d*RADS)),int(l*sin(d*RADS))); }
b Đường cong C:
#define FACT 0.7071
void C(int n,float l,float d){
if (n>0){
d+=45;
C(n-1,l*FACT,d);
d-=90;
C(n-1,l*FACT,d);
d+=45;
}
else linerel(int(l*cos(d*RADS)),int(l*sin(d*RADS))); }
c Đường cong Rồng:
#define FACT 0.7071
void Dragon(int n,float l,int d,int S){
if(n>0){
d+=45*S;
Dragon(n-1,l*FACT,d,1);
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:
Trang 164 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; T[2][0]=0.0; T[2][1]=0.0; T[2][2]=1.0; }
void Tinhtien(Affine &T,float x,float y){
T[0][0]=1; T[0][1]=0; T[0][2]=0;
T[1][0]=0; T[1][1]=1; T[1][2]=0;
T[2][0]=x; T[2][1]=y; T[2][2]=1;
}
void Nghieng(Affine &T,float h,float g){
T[0][0]=1; T[0][1]=g; T[0][2]=0;
T[1][0]=h; T[1][1]=1; T[1][2]=0;
T[2][0]=0; T[2][1]=0; T[2][2]=1;
}
void Codan(Affine &T,float Sx,float Sy){
T[0][0]=Sx; T[0][1]=0; T[0][2]=0;
T[1][0]=0; T[1][1]=Sy; T[1][2]=0;
T[2][0]=0; T[2][1]=0; T[2][2]=1;
Trang 17void Tich(Affine A,Affine B,Affine &C){
Affine Tr1,Tr;
Tr1[0][0]=A[2][0]; Tr1[0][1]=A[2][1]; Tr1[0][2]=1; MatMul(A,B,C,2,2);
MatMul(Tr1,B,Tr,1,2);
C[2][0]=Tr[0][0]+B[2][0];C[2][1]=Tr[0][1]+B[2][1]; }
void QuayTamO(Affine &T,float fi,float x,float y){ Affine T1,T2,Q,tam;
tam[0][2]=0;
Tinhtien(T1,-x,-y);
Tinhtien(T2,x,y);
Quay(Q,fi);
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);
Trang 18rectangle(x+68,y-39,x+72,y-44);
setfillstyle(1,1);
floodfill(x+69,y-40,mau1);
rectangle(x+68,y-51,x+72,y-57);
setfillstyle(1,14);
floodfill(x+69,y-52,mau1);
rectangle(x+68,y-64,x+72,y-70);
setfillstyle(1,4);
floodfill(x+69,y-65,mau1);
//Khung tron quat
circle(x+70,y-260,80);
circle(x+70,y-260,79);
}
void Vecanhquat(int x,int y,int mau1,int mau2) {
//Code ve canh quat dang test
setcolor(mau1);
c[0]=x+70; c[1]=y-260;
B1[0][0]=x+70; B1[0][1]=y-190; B1[0][2]=1;
//Canh quat thu nhat
QuayTamO(A1,goc*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+30)*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 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);
Trang 19//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()
{
initwindow(980,480,"OTO");
outtextxy(10,360,"THUC HANH DO HOA VE MAY QUAT"); line(0,350,getmaxx(),350);
x=getmaxx()/2-50;
y=getmaxy()/2+110;
Vemayquat(x,y,GREEN,14);
while(!kbhit()){
Vecanhquat(x,y,GREEN,14);
delay(50);
Vecanhquat(x,y,BLACK,BLACK);
goc+=20;
}
getch();
closegraph();
return 0;
}
Demo: