Upload
tran-van-teo
View
252
Download
0
Embed Size (px)
Citation preview
7/31/2019 Bai Tap Do Hoa
1/55
BI TP TUN 3
1. Ly li code BT9 tun 1 thm vo 1 s lnh ca php xoay 90o ngc chiu kim ng h parabol
c dng nh sau: trc khi xoay mu en, sau khi xoay mu
#include
#include
#include
typedef float MATRAN[3][3];
double PI=3.1415926535897932384626433832795;
float goc=PI/2;
const int width=640;
const int height=480;
int xtam=0,ytam=0;
void initQuay(MATRAN m)
{
m[0][0]=cos(goc);
m[0][1]=sin(goc);
7/31/2019 Bai Tap Do Hoa
2/55
m[0][2]=0;
m[1][0]=-sin(goc);
m[1][1]=cos(goc);
m[1][2]=0;
m[2][0]=0;
m[2][1]=0;
m[2][2]=1;
}
void biendiem(int &xtam,int &ytam, MATRAN m)
{
int x1,y1;
x1=xtam*m[0][0] + ytam* m[1][0] + m[2][0];
y1=xtam*m[0][1]+ ytam*m[1][1] +m[2][1];
xtam=x1;
ytam=y1;
}
void myInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,width,0,height);
}
int Round(double a)
7/31/2019 Bai Tap Do Hoa
3/55
{
return(int)(a+0.5);
}
void setPixel(int x,int y)
{
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
return;
}
void parabol(int xc, int yc,int a,MATRAN m)
{
int x, y;
int xa = Round(1.0*a/2);
int p = 2-a;
x=0; y=0;
setPixel(xc+x,yc+y);
setPixel(xc-x,yc+y);
while (x
7/31/2019 Bai Tap Do Hoa
4/55
}
x++;
glColor3f(0.0,0.0,0.0);
setPixel(xc+x,yc+y);
setPixel(xc-x,yc+y);
xtam=x; // t bin phu trnh iu kin x thay doi thi while n sai
ytam=y;
biendiem(xtam,ytam,m);
glColor3f(1.0,0.0,0.0);
setPixel(xc+xtam,yc+ytam);
xtam=-x;
ytam=y;
biendiem(xtam,ytam,m);
setPixel(xc+xtam,yc+ytam);
glColor3f(0.0,0.0,0.0);
}
p = 4*x*x + 4*x - 4*a*y - 4*a + 1 ;
while (y
7/31/2019 Bai Tap Do Hoa
5/55
7/31/2019 Bai Tap Do Hoa
6/55
MATRAN m;
initQuay(m);
parabol(300,200,100,m);
glFlush();
}
int main(int argc,char* argv[])
{
glutInit(&argc, argv);//initialize the tool kit
glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);//set the display mode
glutInitWindowSize(640,480);//set window size
glutInitWindowPosition(100,150);// set window position on screen
glutCreateWindow("Bai 1");// open the screen window
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}
7/31/2019 Bai Tap Do Hoa
7/55
2. Ly li code BT8 tun 1 thm vo 1 s lnh ca php bin dng ellipse thu nh 2 ln c dngnh sau:
Trc khi thu nh mu , sau khi thu nh mu xanh dng
#include
#include
#include
const GLint width =640;
const GLint height= 480;
typedef float MATRAN[3][3];
void initTiLe(float sx,float sy,MATRAN m)
{
m[0][0]=sx;
m[0][1]=0;
7/31/2019 Bai Tap Do Hoa
8/55
m[0][2]=0;
m[1][0]=0;
m[1][1]=sy;
m[1][2]=0;
m[2][0]=0;
m[2][1]=0;
m[2][2]=1;
}
void biendiem(int &x,int &y, MATRAN m)
{
int x1,y1;
x1=x*m[0][0] + y* m[1][0] + m[2][0];
y1=x*m[0][1]+y*m[1][1] +m[2][1];
x=x1;
y=y1;
}
void myInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f, 1.0f, 0.0f);
glPointSize(3.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, width, 0.0, height);
}
void putPixel(GLint x, GLint y)
7/31/2019 Bai Tap Do Hoa
9/55
{
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
return;
}
void put4pixel(int xc,int yc,int x,int y)
{
putPixel(xc+x,y+yc);
putPixel(xc-x,y+yc);
putPixel(xc-x,yc-y);
putPixel(xc+x,yc-y);
}
//Ve eclipse theo theo thuat toan Bresenham
void eclipseBresenham(int xc, int yc, int a, int b,MATRAN m) {
int xtam,ytam;
int x,y;
double z1,z2,p;
x=0;y=b;
z1=(double)(b*b)/(a*a);
z2=(double)1/z1;
p=2*z1-(2*b)+1;
while(z1*(double)x/y
7/31/2019 Bai Tap Do Hoa
10/55
xtam=x;
ytam=y;
biendiem(xtam,ytam,m);
glColor3f(0.0,0.0,1.0);
put4pixel(xc, yc, xtam, ytam);
glColor3f(1.0,0.0,0.0);
if (p
7/31/2019 Bai Tap Do Hoa
11/55
else
{
p+=2*z2*(2*y+3)+4*(1-x);
x--;
}
y++;
}
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,1.0);
glBegin(GL_LINES);
glVertex2i(50,height/2);
glVertex2i(600,height/2);
glVertex2i(width/2,50);
glVertex2i(width/2,400);
glEnd();
MATRAN m;
initTiLe(0.5f,0.5f,m);
eclipseBresenham(width/2,height/2,100,70,m);
glFlush();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv); //initialize the tool kit
7/31/2019 Bai Tap Do Hoa
12/55
glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);//set the display mode
glutInitWindowSize(width, height); //set window size
glutInitWindowPosition(100, 150); // set window position on screen
glutCreateWindow("Bai 2"); // open the screen window
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}
3. Ly li code BT9 tun 1 thm vo 1 s lnh ca php xoay 90o cng chiu kim ng h, sau thunh parabol 2 ln parabol c dng nh sau: mu en trc khi xoay, mu sau khi xoay 90 o
cng chiu kim ng h, mu xanh thu nh 2 ln
#include
#include
#include
typedef float MATRAN[3][3];
typedef float MATRAN1[3][3];
7/31/2019 Bai Tap Do Hoa
13/55
double PI=3.1415926535897932384626433832795;
float goc=-PI/2;
const int width=640;
const int height=480;
float sx=0.5f,sy=0.5f;
void initQuay(MATRAN m)
{
m[0][0]=cos(goc);
m[0][1]=sin(goc);
m[0][2]=0;
m[1][0]=-sin(goc);
m[1][1]=cos(goc);
m[1][2]=0;
m[2][0]=0;
m[2][1]=0;
m[2][2]=1;
}
void initQuay_Tile(MATRAN1 m)
{
m[0][0]=sx*cos(goc);
m[0][1]=sy*sin(goc);
m[0][2]=0;
7/31/2019 Bai Tap Do Hoa
14/55
m[1][0]=-sx*sin(goc);
m[1][1]=sy*cos(goc);
m[1][2]=0;
m[2][0]=0;
m[2][1]=0;
m[2][2]=1;
}
void biendiem(int &xtam,int &ytam, MATRAN m)
{
int x1,y1;
x1=xtam*m[0][0] + ytam* m[1][0] + m[2][0];
y1=xtam*m[0][1]+ ytam*m[1][1] +m[2][1];
xtam=x1;
ytam=y1;
}
void myInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(3.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
7/31/2019 Bai Tap Do Hoa
15/55
gluOrtho2D(0.0,width,0,height);
}
//ham lam tron
int Round(double a)
{
return(int)(a+0.5);
}
// ham set pixel
void setPixel(int x,int y)
{
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
return;
}
// v parabol
void parabol(int xc, int yc,int a,MATRAN m,MATRAN1 m1)
{
int xtam=0,ytam=0;
int x, y;
int xa = Round(1.0*a/2);
int p = 2-a;
7/31/2019 Bai Tap Do Hoa
16/55
x=0; y=0;
glColor3f(0.0,0.0,0.0);
setPixel(xc+x,yc+y);
setPixel(xc-x,yc+y);
while (x
7/31/2019 Bai Tap Do Hoa
17/55
biendiem(xtam,ytam,m); // bien cung thu 2
setPixel(xc+xtam,yc+ytam);
glColor3f(0.0,0.0,1.0); // mau xanh ve hinh 3
xtam=x;
ytam=y;
biendiem(xtam,ytam,m1); // ve cung 1
setPixel(xc+xtam,yc+ytam);
xtam=-x;
ytam=y;
biendiem(xtam,ytam,m1); // ve cung 2
setPixel(xc+xtam,yc+ytam);
glColor3f(0.0,0.0,0.0); // quay ve mau den ban dau
}
p = 4*x*x + 4*x - 4*a*y - 4*a + 1 ;
while (y
7/31/2019 Bai Tap Do Hoa
18/55
else
p = p - 4*a;
y++;
setPixel(xc+x,yc+y);
setPixel(xc-x,yc+y);
xtam=x;
ytam=y;
glColor3f(1.0,0.0,0.0); // mau do ve hinh 2
biendiem(xtam,ytam,m); // bien cung thu 1
setPixel(xc+xtam,yc+ytam);
xtam=-x;
ytam=y;
biendiem(xtam,ytam,m); // bien cung thu 2
setPixel(xc+xtam,yc+ytam);
glColor3f(0.0,0.0,1.0); // mau xanh ve hinh 3
xtam=x;
ytam=y;
biendiem(xtam,ytam,m1); // ve cung 1
setPixel(xc+xtam,yc+ytam);
xtam=-x;
7/31/2019 Bai Tap Do Hoa
19/55
ytam=y;
biendiem(xtam,ytam,m1); // ve cung 2
setPixel(xc+xtam,yc+ytam);
glColor3f(0.0,0.0,0.0); // quay ve mau den ban dau
}
return;
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,0,1.0);
glBegin(GL_LINES);
glVertex2i(150,200);
glVertex2i(450,200);
glVertex2i(300,100);
glVertex2i(300,430);
glEnd();
glColor3f(1.0,0.0,0.0);
HWND hWnd;
MATRAN m;
MATRAN1 m1;
initQuay_Tile(m1);
7/31/2019 Bai Tap Do Hoa
20/55
initQuay(m);
parabol(300,200,100,m,m1);
glFlush();
}
//hm main
int main(int argc,char* argv[])
{
glutInit(&argc, argv);//initialize the tool kit
glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);//set the display mode
glutInitWindowSize(640,480);//set window size
glutInitWindowPosition(100,150);// set window position on screen
glutCreateWindow("Bai 3");// open the screen window
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}
4. Ci t thut ton t mu theo ng bin dng quy. Kt qu nh hnh: mu bin mu , mut mu xanh
7/31/2019 Bai Tap Do Hoa
21/55
#include
#include
void setPixel(int x, int y, GLfloat color[3]);
void getPixel(int x, int y, GLfloat color[3]);
bool ktTrungMau(GLfloat color1[3], GLfloat color2[3]);
void FillLeft(int x, int y, GLfloat F_Color[3], GLfloat B_Color[3]);
void FillTop(int x, int y, GLfloat F_Color[3], GLfloat B_Color[3]);
void FillRight(int x, int y, GLfloat F_Color[3], GLfloat B_Color[3]);
void FillBottom(int x, int y, GLfloat F_Color[3], GLfloat B_Color[3]);
void BoundaryFillEnhanced(int x, int y, GLfloat F_Color[3], GLfloat B_Color[3]);
void myInit();
void myDisplay();
const int width=240;
7/31/2019 Bai Tap Do Hoa
22/55
const int height=240;
int main(int argc,char* argv[])
{
glutInit(&argc, argv);//initialize the tool kit
glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);//set the display mode
glutInitWindowSize(width,height);//set window size
glutInitWindowPosition(100,150);// set window position on screen
glutCreateWindow("Bai 4");// open the screen window
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}
void setPixel(int x, int y, GLfloat color[3])
{
glRasterPos2i(x,y);
glDrawPixels(1,1,GL_RGB,GL_FLOAT,color);
return;
}
// tr v mu sc ti im x,y. mu thng c to thnh t luc lam
void getPixel(int x, int y, GLfloat color[3])
{
7/31/2019 Bai Tap Do Hoa
23/55
glReadPixels(x,y,1,1,GL_RGB,GL_FLOAT,color);
return;
}
// khi kim tra mu, ta phi kim tra c 3 mu ca im ny c trng vi im kia hay khng
bool ktTrungMau(GLfloat color1[3], GLfloat color2[3])
{
if (color1[0]==color2[0] && color1[1]==color2[1] && color1[2]==color2[2])
return true; // mu bi trng ri, khng t c
else
return false; // mu khng bi trng, t vy l t c
}
void FillLeft(int x, int y, GLfloat F_Color[3], GLfloat B_Color[3])
{
GLfloat color[3]; // mu hin ti
getPixel(x,y,color); // lu mu vo mng color
// nu mu t F_color v mu ng bin B_color khc vi mu ca mt im trong hnhth tin hnh t
if(!ktTrungMau(F_Color,color)&& !ktTrungMau(B_Color,color))
{
setPixel(x,y,F_Color);
FillLeft(x-1, y, F_Color, B_Color);
FillTop(x, y+1, F_Color, B_Color);
FillBottom(x, y-1, F_Color, B_Color);
7/31/2019 Bai Tap Do Hoa
24/55
}
} // FillLeft
void FillTop(int x, int y, GLfloat F_Color[3], GLfloat B_Color[3])
{
GLfloat color[3]; // mu hin ti
getPixel(x,y,color);
if(!ktTrungMau(F_Color,color)&& !ktTrungMau(B_Color,color))
{
setPixel(x,y,F_Color);
FillLeft(x-1, y, F_Color, B_Color);
FillTop(x, y+1, F_Color, B_Color);
FillRight(x+1, y, F_Color, B_Color);
}
} // FillTop
void FillRight(int x, int y, GLfloat F_Color[3], GLfloat B_Color[3])
{
GLfloat color[3]; // mu hin ti
getPixel(x,y,color);
if(!ktTrungMau(F_Color,color)&& !ktTrungMau(B_Color,color))
{
setPixel(x,y,F_Color);
FillTop(x, y+1, F_Color, B_Color);
7/31/2019 Bai Tap Do Hoa
25/55
FillRight(x+1, y, F_Color, B_Color);
FillBottom(x, y-1, F_Color, B_Color);
}
} // FillRight
void FillBottom(int x, int y, GLfloat F_Color[3], GLfloat B_Color[3])
{
GLfloat color[3]; // mu hin ti
getPixel(x,y,color);
if(!ktTrungMau(F_Color,color)&& !ktTrungMau(B_Color,color))
{
setPixel(x,y,F_Color);
FillLeft(x-1, y, F_Color, B_Color);
FillRight(x+1, y, F_Color, B_Color);
FillBottom(x, y-1, F_Color, B_Color);
}
} // FillBottom
void BoundaryFillEnhanced(int x, int y, GLfloat F_Color[3], GLfloat B_Color[3])
{
GLfloat color[3]; // mu hin ti
getPixel(x,y,color);
7/31/2019 Bai Tap Do Hoa
26/55
if(!ktTrungMau(F_Color,color)&& !ktTrungMau(B_Color,color))
{
setPixel(x,y,F_Color);
FillLeft(x-1, y, F_Color, B_Color);
FillTop(x, y+1, F_Color, B_Color);
FillRight(x+1, y, F_Color, B_Color);
FillBottom(x, y-1, F_Color, B_Color);
}
} // BoundaryFillEnhanced
void myInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,width,0,height);
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
7/31/2019 Bai Tap Do Hoa
27/55
glVertex2i(100,100);
glVertex2i(100,130);
glVertex2i(130,130);
glVertex2i(130,100);
glEnd();
GLfloat F_Color[3]={0.0f,0.0f,1.0f};
GLfloat B_Color[3]={1.0f,0.0f,0.0f};
BoundaryFillEnhanced(120,107,F_Color,B_Color);
glFlush();
}
5. Ly li code BT8 tun 1 thm vo 1 s lnh ca php quay quay ellipse theo chiu kim ng h1 gc 45o c dng nh sau: mu trc khi xoay, mu xanh sau khi xoay
7/31/2019 Bai Tap Do Hoa
28/55
#include
#include
#include
#include
const GLint width =640;
const GLint height= 480;
typedef float MATRAN[3][3];
float PI=3.1415926535897932384626433832795;
float goc=-PI/4;
void initQuay(MATRAN m)
{
m[0][0]=cos(goc);
m[0][1]=sin(goc);
m[0][2]=0;
m[1][0]=-sin(goc);
m[1][1]=cos(goc);
m[1][2]=0;
m[2][0]=0;
m[2][1]=0;
m[2][2]=1;
7/31/2019 Bai Tap Do Hoa
29/55
}
void biendiem(int &x,int &y, MATRAN m)
{
int x1,y1;
x1=x*m[0][0] + y* m[1][0] + m[2][0];
y1=x*m[0][1]+y*m[1][1] +m[2][1];
x=x1;
y=y1;
}
void myInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f, 1.0f, 0.0f);
glPointSize(3.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, width, 0.0, height);
}
void putPixel(GLint x, GLint y)
{
glBegin(GL_POINTS);
glVertex2i(x,y);
7/31/2019 Bai Tap Do Hoa
30/55
glEnd();
return;
}
//Ve eclipse theo theo thuat toan Bresenham
void eclipseBresenham(int xc, int yc, int a, int b,MATRAN m) {
int xtam,ytam;
int x,y;
double z1,z2,p;
x=0;y=b;
z1=(double)(b*b)/(a*a);
z2=(double)1/z1;
p=2*z1-(2*b)+1;
while(z1*(double)x/y
7/31/2019 Bai Tap Do Hoa
31/55
glColor3f(0.0,0.0,1.0);
putPixel(xc+xtam,ytam+yc);
xtam=-x;
ytam=y;
biendiem(xtam,ytam,m);
putPixel(xc+xtam,ytam+yc);
xtam=-x;
ytam=-y;
biendiem(xtam,ytam,m);
putPixel(xc+xtam,ytam+yc);
xtam=x;
ytam=-y;
biendiem(xtam,ytam,m);
putPixel(xc+xtam,ytam+yc);
glColor3f(1.0,0.0,0.0);
if (p
7/31/2019 Bai Tap Do Hoa
32/55
}
x++;
}
x=a;y=0;
p=2*z2-2*a+1;
while(z2*(double)y/x
7/31/2019 Bai Tap Do Hoa
33/55
biendiem(xtam,ytam,m);
putPixel(xc+xtam,ytam+yc);
xtam=x;
ytam=-y;
biendiem(xtam,ytam,m);
putPixel(xc+xtam,ytam+yc);
if (p
7/31/2019 Bai Tap Do Hoa
34/55
glVertex2i(width/2,50);
glVertex2i(width/2,400);
glEnd();
MATRAN m;
initQuay(m);
eclipseBresenham(width/2,height/2,100,70,m);
glFlush();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv); //initialize the tool kit
glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);//set the display mode
glutInitWindowSize(width, height); //set window size
glutInitWindowPosition(100, 150); // set window position on screen
glutCreateWindow("Bai 5"); // open the screen window
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}
6. Ly li code BT9 tun 1 thm vo 1 s lnh ca php xoay 45o ngc chiu kim ng h parabolc dng nh sau: mu en trc khi xoay, mu sau khi xoay 45o ngc chiu kim ng h
7/31/2019 Bai Tap Do Hoa
35/55
#include
#include #include
typedef float MATRAN[3][3];
double PI=3.1415926535897932384626433832795;
float goc=PI/4;
const int width=640;
const int height=480;
int xtam=0,ytam=0;
void initQuay(MATRAN m)
{
m[0][0]=cos(goc);
m[0][1]=sin(goc);
m[0][2]=0;
m[1][0]=-sin(goc);
7/31/2019 Bai Tap Do Hoa
36/55
m[1][1]=cos(goc);
m[1][2]=0;
m[2][0]=0;
m[2][1]=0;
m[2][2]=1;
}
void biendiem(int &xtam,int &ytam, MATRAN m)
{
int x1,y1;
x1=xtam*m[0][0] + ytam* m[1][0] + m[2][0];
y1=xtam*m[0][1]+ ytam*m[1][1] +m[2][1];
xtam=x1;
ytam=y1;
}
void myInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
7/31/2019 Bai Tap Do Hoa
37/55
glLoadIdentity();
gluOrtho2D(0,width,0,height);
}
//ham lam tron
int Round(double a)
{
return(int)(a+0.5);
}
// ham set pixel
void setPixel(int x,int y)
{
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
return;
}
// v parabol
void parabol(int xc, int yc,int a,MATRAN m)
{
int x, y;
int xa = Round(1.0*a/2);
int p = 2-a;
7/31/2019 Bai Tap Do Hoa
38/55
x=0; y=0;
setPixel(xc+x,yc+y);
setPixel(xc-x,yc+y);
while (x
7/31/2019 Bai Tap Do Hoa
39/55
xtam=-x; // t bin phu trnh iu kin x thay doi thi while n sai
ytam=y;
biendiem(xtam,ytam,m);
setPixel(xc+xtam,yc+ytam);
glColor3f(0.0,0.0,0.0);
}
p = 4*x*x + 4*x - 4*a*y - 4*a + 1 ;
while (y
7/31/2019 Bai Tap Do Hoa
40/55
glColor3f(1.0,0.0,0.0);
setPixel(xc+xtam,yc+ytam);
xtam=-x; // t bin phu trnh iu kin x thay doi thi while n sai
ytam=y;
biendiem(xtam,ytam,m);
setPixel(xc+xtam,yc+ytam);
glColor3f(0.0,0.0,0.0);
}
return;
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,0,1.0);
glBegin(GL_LINES);
glVertex2i(150,200);
glVertex2i(450,200);
glVertex2i(300,100);
glVertex2i(300,430);
glEnd();
glColor3f(1.0,0.0,0.0);
7/31/2019 Bai Tap Do Hoa
41/55
MATRAN m;
initQuay(m);
parabol(300,200,100,m);
glFlush();
}
//hm main
int main(int argc,char* argv[])
{
glutInit(&argc, argv);//initialize the tool kit
glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);//set the display mode
glutInitWindowSize(640,480);//set window size
glutInitWindowPosition(100,150);// set window position on screen
glutCreateWindow("Bai 6");// open the screen window
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}
7. Dng thut ton Bresenham v Hyperbol 12
2
2
2
=
b
y
a
xra gia mn hnh . Ca s c kch thc
640x480
7/31/2019 Bai Tap Do Hoa
42/55
#include
#include
#include
const int width=640;
const int height=480;
void myInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,width,0.0,height);
}
7/31/2019 Bai Tap Do Hoa
43/55
//ham lam tron
int Round(double a)
{
return(int)(a+0.5);
}
// ham set pixel
void setPixel(int x,int y)
{
glBegin(GL_POINTS);
glVertex2i((x+300),(200-y));
glVertex2i((-x+300),(200-y));
glVertex2i((x+300),(200+y));
glVertex2i((-x+300),(200+y));
glEnd();
return;
}
void hyperbolBress(double a,double b)
{
//double x=a,y=0;
//double d1 = (2*a*a) - (b*b) - (2*a*b*b);
7/31/2019 Bai Tap Do Hoa
44/55
//setPixel(x,y);
//while((a*a*y)
7/31/2019 Bai Tap Do Hoa
45/55
// y++;
// x++;
// }
// else
// {
// d1+= -2.0f*b*b*a*a*(2*x+3);
// x++;
// }
// setPixel(x,y);
//}
//---------------------------------------------------------------- Thu cach nay
float x=a,y=0,d=b*b*4*a-4*a*a+b*b;
setPixel(x,y);
while (a*a*a*a>=x*x*(a*a-b*b))
{
if (d100) break;
7/31/2019 Bai Tap Do Hoa
46/55
}
while (x
7/31/2019 Bai Tap Do Hoa
47/55
glEnd();
glColor3f(1.0,0.0,0.0);
hyperbolBress(100,100);
glFlush();
}
//hm main
int main(int argc,char* argv[])
{
glutInit(&argc, argv);//initialize the tool kit
glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);//set the display mode
glutInitWindowSize(640,480);//set window size
glutInitWindowPosition(100,150);// set window position on screen
glutCreateWindow("Bai 7");// open the screen window
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}
7/31/2019 Bai Tap Do Hoa
48/55
8. Ly li code BT7T3 thm vo 1 s lnh ca php xoay 90o ngc chiu kim ng h hyperbolc dng nh sau: mu l mu trc khi xoay, mu en l mu sau khi xoay. Ca s c kchthc 640x640.
#include
#include
#include
#include
typedef float MATRAN[3][3];
double PI=3.1415926535897932384626433832795;
float goc=PI/2;
const int width=640;
const int height=480;
int xc=300;
int yc=200;
void initQuay(MATRAN m)
{
7/31/2019 Bai Tap Do Hoa
49/55
m[0][0]=cos(goc);
m[0][1]=sin(goc);
m[0][2]=0;
m[1][0]=-sin(goc);
m[1][1]=cos(goc);
m[1][2]=0;
m[2][0]=0;
m[2][1]=0;
m[2][2]=1;
}
void myInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,width,0,height);
}
//ham lam tron
7/31/2019 Bai Tap Do Hoa
50/55
int Round(double a)
{
return(int)(a+0.5);
}
// ham set pixel
void setPixel(int x,int y)
{
glBegin(GL_POINTS);
glVertex2i(x+xc,y+yc);
glEnd();
return;
}
void biendiem(int &xtam,int &ytam, MATRAN m)
{
int x1,y1;
x1=xtam*m[0][0] + ytam* m[1][0] + m[2][0];
y1=xtam*m[0][1]+ ytam*m[1][1] +m[2][1];
xtam=x1;
ytam=y1;
}
void hyperbolBress(double a,double b,MATRAN m)
{
int xtam=0,ytam=0;
7/31/2019 Bai Tap Do Hoa
51/55
float x=a,y=0,d=b*b*4*a-4*a*a+b*b;
glColor3f(1,0,0);
setPixel(x,y);
setPixel(-x,y);
setPixel(-x,-y);
setPixel(x,-y);
while (a*a*a*a>=x*x*(a*a-b*b))
{
if (d
7/31/2019 Bai Tap Do Hoa
52/55
biendiem(xtam,ytam,m);
setPixel(xtam,ytam);
xtam=x;
ytam=-y;
biendiem(xtam,ytam,m);
setPixel(xtam,ytam);
xtam=-x;
ytam=y;
biendiem(xtam,ytam,m);
setPixel(xtam,ytam);
xtam=-x;
ytam=-y;
biendiem(xtam,ytam,m);
setPixel(xtam,ytam);
glColor3f(1,0,0);
if (y>100) break;
}
while (x
7/31/2019 Bai Tap Do Hoa
53/55
else {
d=d+b*b*8*x+12*b*b-a*a*8*y-8*a*a;
y++;
}
x++;
//setPixel(x,y);
glColor3f(0,0,0);
xtam=x;
ytam=y;
biendiem(xtam,ytam,m);
setPixel(xtam,ytam);
xtam=x;
ytam=-y;
biendiem(xtam,ytam,m);
setPixel(xtam,ytam);
xtam=-x;
ytam=y;
biendiem(xtam,ytam,m);
setPixel(xtam,ytam);
xtam=-x;
ytam=-y;
7/31/2019 Bai Tap Do Hoa
54/55
biendiem(xtam,ytam,m);
setPixel(xtam,ytam);
glColor3f(1,0,0);
}
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,0,1.0);
glBegin(GL_LINES);
glVertex2i(150,200);
glVertex2i(450,200);
glVertex2i(300,50);
glVertex2i(300,430);
glEnd();
glColor3f(1.0,0.0,0.0);
MATRAN m;
initQuay(m);
hyperbolBress(100,100,m);
glFlush();
}
7/31/2019 Bai Tap Do Hoa
55/55
//hm main
int main(int argc,char* argv[])
{
glutInit(&argc, argv);//initialize the tool kit
glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);//set the display mode
glutInitWindowSize(640,480);//set window size
glutInitWindowPosition(100,150);// set window position on screen
glutCreateWindow("Bai 8");// open the screen window
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}