0% found this document useful (0 votes)
20 views13 pages

21N14 1021020 NguyenMinhThong LabĐHMT01

mmt

Uploaded by

Thong nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views13 pages

21N14 1021020 NguyenMinhThong LabĐHMT01

mmt

Uploaded by

Thong nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

bai1 hinh hop chu nhat:GLuint MakeBox(const float length, const float width, const float height)

GLuint dp_list;

dp_list = glGenLists(1);

glNewList(dp_list, GL_COMPILE);

float x = length;

float y = height;

float z = width ;

//Back

glBegin(GL_QUADS);

glNormal3f(0.0f, 0.0f, -1.0f);

glVertex3f(0, 0, 0);

glVertex3f(x, 0, 0);

glVertex3f(x, y, 0);

glVertex3f(0, y, 0);

glEnd();

// left

glBegin(GL_QUADS);

glNormal3f(-1.0f, 0.0f, 0.0f);

glVertex3f(0, 0, 0);

glVertex3f(0, 0, z);

glVertex3f(0, y, z);

glVertex3f(0, y, 0);

glEnd();

//front

glBegin(GL_QUADS);

glNormal3f(0.0f, 0.0f, 1.0f);

glVertex3f(0, 0, z);
glVertex3f(0, y, z);

glVertex3f(x, y, z);

glVertex3f(x, 0, z);

glEnd();

//// right

glBegin(GL_QUADS);

glNormal3f(1.0f, 0.0f, 0.0f);

glVertex3f(x, 0, z);

glVertex3f(x, 0, 0);

glVertex3f(x, y, 0);

glVertex3f(x, y, z);

glEnd();

//Top

glBegin(GL_QUADS);

glNormal3f(0.0f, 1.0f, 0.0f);

glVertex3f(0, y, 0);

glVertex3f(x, y, 0);

glVertex3f(x, y, z);

glVertex3f(0, y, z);

//Bottom

glBegin(GL_QUADS);

glNormal3f(0.0f, -1.0f, 0.0f);

glVertex3f(0, 0, 0);

glVertex3f(x, 0, 0);

glVertex3f(x, 0, z);

glVertex3f(0, 0, z);

glEnd();
glEndList();

return dp_list;

bai 2

#include <GL/glut.h>

#include <math.h>

// Tính khoảng cách giữa hai điểm (x1,y1) và (x2,y2)

float distance(float x1, float y1, float x2, float y2) {

return sqrt(pow(x2-x1,2) + pow(y2-y1,2));

void drawCircle(int xa, int ya, int xb, int yb, int xc, int yc) {

// Tính toán bán kính của đường tròn

float a = distance(xb,yb,xc,yc);

float b = distance(xa,ya,xc,yc);

float c = distance(xa,ya,xb,yb);

float p = (a+b+c)/2;

float radius = (2*sqrt(p*(p-a)*(p-b)*(p-c)))/(a+b+c);

// Tính toán tọa độ tâm của đường tròn

float cx = (a*xa + b*xb + c*xc) / (a+b+c);

float cy = (a*ya + b*yb + c*yc) / (a+b+c);

// Vẽ đường tròn sử dụng GL_LINE_LOOP

glBegin(GL_LINE_LOOP);

for(int i = 0; i < 360; i++) {


float angle = i * 3.14159 / 180;

float x = cx + radius * cos(angle);

float y = cy + radius * sin(angle);

glVertex2f(x,y);

glEnd();

// Hàm vẽ scene

void display() {

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0f, 1.0f, 1.0f);

drawCircle(100, 100, 50, 200, 200, 250);

glFlush();

int main(int argc, char** argv) {

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE);

glutInitWindowSize(500, 500);

glutInitWindowPosition(100, 100);

glutCreateWindow("OpenGL 2D Circle Example");

glutDisplayFunc(display);

glutMainLoop();

return 0;

bài 3
#include <GL/glut.h>

#include <math.h>

void drawCircle(float x0, float y0, float R)

float x = 0, y = R;

float d = 5.0 / 4.0 - R;

glBegin(GL_POINTS);

glVertex2f(x + x0, y + y0);

while (y > x) {

if (d < 0) {

d += 2 * x + 3;

} else {

d += 2 * (x - y) + 5;

y--;

x++;

glVertex2f(x + x0, y + y0);

glVertex2f(y + x0, x + y0);

glVertex2f(-x + x0, y + y0);

glVertex2f(-y + x0, x + y0);

glVertex2f(-x + x0, -y + y0);

glVertex2f(-y + x0, -x + y0);

glVertex2f(x + x0, -y + y0);

glVertex2f(y + x0, -x + y0);

}
glEnd();

void display()

// Các điểm của tam giác ABC

float xa = 100, ya = 100;

float xb = 200, yb = 200;

float xc = 300, yc = 100;

// Tính tọa độ tâm và bán kính của đường tròn ngoại tiếp

float xm = (xa + xb) / 2, ym = (ya + yb) / 2;

float dx = xb - xa, dy = yb - ya;

float dn = sqrt(dx*dx + dy*dy);

float cosA = dx / dn, sinA = dy / dn;

float xt = xm + sinA*(yc - ym) - cosA*(xc - xm);

float yt = ym + cosA*(yc - ym) + sinA*(xc - xm);

float R = sqrt((xt - xa)*(xt - xa) + (yt - ya)*(yt - ya));

// Vẽ đường tròn ngoại tiếp

glColor3f(1.0, 0.0, 0.0); // Màu đỏ

drawCircle(xt, yt, R);

// Vẽ tam giác

glColor3f(0.0, 0.0, 1.0); // Màu xanh dương

glBegin(GL_LINE_LOOP);

glVertex2f(xa, ya);

glVertex2f(xb, yb);

glVertex2f(xc, yc);

glEnd();
glFlush();

int main(int argc, char** argv)

glutInit(&argc, argv);

glutInitWindowSize(400, 400);

glutCreateWindow("Circle Circumscribed");

glutDisplayFunc(display);

glutMainLoop();

return 0;

bài 5:

Đỉnh 1: (1, 0)

- Đỉnh 2: (0.309, 0.951)

- Đỉnh 3: (-0.809, 0.588)

- Đỉnh 4: (-0.809, -0.588)

- Đỉnh 5: (0.309, -0.951)

GLfloat radius = 1.0; // bán kính của ngôi sao

GLfloat x_center = 0.0; // tọa độ x của tâm ngôi sao

GLfloat y_center = 0.0; // tọa độ y của tâm ngôi sao

GLfloat x, y;

GLfloat angle = 72.0 * (M_PI / 180.0); // góc quay 72 độ (đổi sang radian)

GLfloat start_angle = -90.0 * (M_PI / 180.0); // góc ban đầu ở trục x

GLfloat x1 = x_center + radius * cos(start_angle);

GLfloat y1 = y_center + radius * sin(start_angle);

glBegin(GL_POLYGON);
glVertex2f(x1, y1);

for(int i = 1; i <= 5; ++i)

x = x_center + radius * cos(start_angle + i * angle);

y = y_center + radius * sin(start_angle + i * angle);

glVertex2f(x, y);

glEnd();

// Nối các đỉnh thành đa giác

glBegin(GL_LINE_LOOP);

glVertex2f(x1, y1);

glVertex2f(x, y);

glVertex2f(x, y);

x = x_center + radius * cos(start_angle + 2 * angle);

y = y_center + radius * sin(start_angle + 2 * angle);

glVertex2f(x, y);

x1 = x_center + radius * cos(start_angle + 4 * angle);

y1 = y_center + radius * sin(start_angle + 4 * angle);

glVertex2f(x1, y1);

glEnd();

bài 6:

#include <GL/glut.h>

#include <math.h>

void drawFlower() {

int i;

float angle;
// Tâm của bông hoa

float centerX = 0.5;

float centerY = 0.5;

// Bán kính của bông hoa

float radius = 0.25;

// Số cánh của bông hoa

int petals = 5;

// Góc giữa hai cánh

float petalAngle = 360.0 / petals;

// Vẽ cánh bông hoa

glColor3f(1.0, 0.0, 0.0);

glBegin(GLTRIANGLEFAN);

glVertex2f(centerX, centerY);

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

angle = i petalAngle;

glVertex2f(centerX + radius cos(angle M_PI / 180.0),

centerY + radius sin(angle M_PI / 180.0));

glEnd();

// Vẽ phiến hoa

radius = 0.1;

glColor3f(0.0, 1.0, 0.0);

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

angle = i petalAngle;

glBegin(GLTRIANGLEFAN);

glVertex2f(centerX + (radius cos((angle + (petalAngle / 2)) MPI / 180.0)),


centerY + (radius * sin((angle + (petalAngle / 2)) * MPI / 180.0)));

glVertex2f(centerX + (radius cos((angle + (petalAngle / 2)) MPI / 180.0)) + 0.05 * cos(angle *


MPI / 180.0),

centerY + (radius sin((angle + (petalAngle / 2)) MPI / 180.0)) + 0.05 * sin(angle * MPI /
180.0));

glVertex2f(centerX + (radius cos((angle + (petalAngle / 2)) MPI / 180.0)) + 0.1 * cos((angle -


(petalAngle / 2)) * MPI / 180.0),

centerY + (radius sin((angle + (petalAngle / 2)) MPI / 180.0)) + 0.1 * sin((angle -


(petalAngle / 2)) * MPI / 180.0));

glVertex2f(centerX + (radius cos((angle + (petalAngle / 2)) MPI / 180.0)),

centerY + (radius * sin((angle + (petalAngle / 2)) * MPI / 180.0)) + 0.075 sin((angle +


(petalAngle / 2)) MPI / 180.0));

glVertex2f(centerX + (radius * cos((angle + (petalAngle / 2)) * MPI / 180.0)) - 0.1 cos((angle +


(petalAngle / 2)) MPI / 180.0),

centerY + (radius * sin((angle + (petalAngle / 2)) * MPI / 180.0)) + 0.1 sin((angle +


(petalAngle / 2)) MPI / 180.0));

glVertex2f(centerX + (radius * cos((angle + (petalAngle / 2)) * MPI / 180.0)) - 0.05 cos((angle -


(petalAngle / 2)) MPI / 180.0),

centerY + (radius * sin((angle + (petalAngle / 2)) * MPI / 180.0)) - 0.05 sin((angle -


(petalAngle / 2)) MPI / 180.0));

glEnd();

void display() {

glClear(GLCOLORBUFFERBIT);

drawFlower();

glFlush();

void reshape(int w, int h) {

glViewport(0, 0, (GLsizei) w, (GLsizei) h);

glMatrixMode(GLPROJECTION);

glLoadIdentity();
gluOrtho2D(0, 1, 0, 1);

glMatrixMode(GLMODELVIEW);

int main(int argc, char argv) {

glutInit(&argc, argv);

glutInitDisplayMode(GLUTSINGLE | GLUTRGB);

glutInitWindowSize(500, 500);

glutCreateWindow("Flower");

glClearColor(0.5, 0.5, 0.5, 1.0);

glutDisplayFunc(display);

glutReshapeFunc(reshape);

glutMainLoop();

return 0;

Bài 7:

#include <GL/glut.h>

void drawRect(float width, float height, float r, float g, float b) {

glBegin(GL_QUADS);

glColor3f(r, g, b);

glVertex2f(-width / 2, height / 2);

glVertex2f(width / 2, height / 2);

glVertex2f(width / 2, -height / 2);

glVertex2f(-width / 2, -height / 2);

glEnd();

void drawStar(float r, float g, float b) {

glBegin(GL_TRIANGLES);

glColor3f(r, g, b);
glVertex2f(-0.1, 0.2);

glVertex2f(0, -0.15);

glVertex2f(0.1, 0.2);

glVertex2f(-0.3, -0.2);

glVertex2f(0, 0.15);

glVertex2f(0.3, -0.2);

glEnd();

void drawFlag(bool withStar) {

// Draw the red background

drawRect(1, 2.0 / 3.0, 1, 0, 0);

// Draw the yellow star

if (withStar) {

drawStar(1, 1, 0);

// Draw the central red stripe

drawRect(1, 2.0 / 15.0, 1, 0, 0);

// Draw the remaining stripes

glPushMatrix();

glTranslatef(0, -4.0 / 15.0, 0); // Move to the bottom of the red stripe

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

drawRect(1, 2.0 / 15.0, 1, 1, 1);

glTranslatef(0, -2.0 / 15.0, 0); // Move down to the next stripe

glPopMatrix();

}
void display() {

glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

drawFlag(true); // Draw the flag with a star

glFlush();

void init() {

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(-1, 1, -1, 1);

int main(int argc, char** argv) {

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(400, 600);

glutCreateWindow("Vietnamese Flag");

glutDisplayFunc(display);

init();

glutMainLoop();

return 0;

You might also like