Professional Documents
Culture Documents
Đà 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
-loleaut32
-lole32
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);
line(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:
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);
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);
}
}
}
Demo:
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);
delay(5);
lineto(100+30*x,100+30*y);
}
getch();
}
Demo:
2. 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
{
x--;
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++)
{
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);
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");
int 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) {
K(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:
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];
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);
//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: