0% found this document useful (0 votes)
3 views2 pages

Comp Graph-4

The document contains C++ code that utilizes OpenGL to create a graphical application displaying a grid of hexagonal spirals. It defines functions to draw hexagons, create a pattern of spirals, and set up a grid layout with specific viewport configurations. The main function initializes the GLUT window and starts the rendering loop.

Uploaded by

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

Comp Graph-4

The document contains C++ code that utilizes OpenGL to create a graphical application displaying a grid of hexagonal spirals. It defines functions to draw hexagons, create a pattern of spirals, and set up a grid layout with specific viewport configurations. The main function initializes the GLUT window and starts the rendering loop.

Uploaded by

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

#include <GL/glut.

h>
#include <math.h>

#define PI 3.14159265358979323846
#define ROWS 4
#define COLS 5
#define VIEWPORT_SIZE 150

void drawHexagon(float size) {


glBegin(GL_LINE_LOOP);
for (int i = 0; i < 6; i++) {
float angle = 2.0 * PI * i / 6;
glVertex2f(size * cos(angle), size * sin(angle));
}
glEnd();
}

void drawPattern() {
float size = 0.03;
float step = 0.02;
int numHexagons = 20;

glPushMatrix();
for (int i = 0; i < numHexagons; i++) {
drawHexagon(size);
size += step;
glRotatef(10, 0, 0, 1);
}
glPopMatrix();
}

void drawGrid() {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
// Viewport-ийг тохируулах
glViewport(col * VIEWPORT_SIZE, row * VIEWPORT_SIZE, VIEWPORT_SIZE,
VIEWPORT_SIZE);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-0.25, 0.25, -0.25, 0.25);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glPushMatrix();

// Тэгш хэмийг мөр болон баганаар нь тохируулах


if (col % 2 == 1 && row % 2 == 1) {
glScalef(-1, -1, 1);
}
else if (col % 2 == 1) {
glScalef(-1, 1, 1);
}
else if (row % 2 == 1) {
glScalef(1, -1, 1);
}
drawPattern();
glPopMatrix();
}
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
drawGrid(); // Only draw the grid
glutSwapBuffers();
}

void init() {
glClearColor(0.2, 0.5, 0.8, 1.0);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(COLS * VIEWPORT_SIZE, ROWS * VIEWPORT_SIZE);
glutCreateWindow("Hexagonal Spiral Grid");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

You might also like