// ================================================ // Combined Computer Graphics Programs // ================================================ /* This file combines multiple OpenGL-based C++ programs used for basic computer graphics algorithms and visualization, including: 1. Bezier Curve and Koch Curve 2. Boundary Fill Algorithm 3. Bresenham's Circle Drawing 4. Bresenham's Line Drawing 5. Cohen-Sutherland Line Clipping 6. DDA Line Drawing 7. Flood Fill Algorithm 8. 2D Transformations Each section is marked clearly for ease of navigation. */ // ---------------------------------------- // Headers Common to All Programs // ---------------------------------------- #include <GL/freeglut.h> #include <iostream> #include <cmath> #include <vector> using namespace std; // ---------------------------------------- // Start of Individual Program Sections // ---------------------------------------- // ================================================ // Benzieandkoch // ================================================ #include <GL/freeglut.h> #include <iostream> #include <cmath> #include <vector> using namespace std; struct Point { float x, y; }; vector<Point> controlPoints = {{100, 300}, {200, 500}, {400, 500}, {500, 300}}; int depth; void drawBezier() { glColor3f(1.0, 0.0, 0.0); glBegin(GL_LINE_STRIP); for (float t = 0; t <= 1; t += 0.01) { float x = pow(1 - t, 3) * controlPoints[0].x + 3 * t * pow(1 - t, 2) * controlPoints[1].x + 3 * pow(t, 2) * (1 - t) * controlPoints[2].x + pow(t, 3) * controlPoints[3].x; float y = pow(1 - t, 3) * controlPoints[0].y + 3 * t * pow(1 - t, 2) * controlPoints[1].y + 3 * pow(t, 2) * (1 - t) * controlPoints[2].y + pow(t, 3) * controlPoints[3].y; glVertex2f(x, y); } glEnd(); } void drawKoch(Point a, Point b, int depth) { if (depth == 0) { glBegin(GL_LINES); glVertex2f(a.x, a.y); glVertex2f(b.x, b.y); glEnd(); return; } Point p1 = {(2 * a.x + b.x) / 3, (2 * a.y + b.y) / 3}; Point p2 = {(a.x + 2 * b.x) / 3, (a.y + 2 * b.y) / 3}; float angle = M_PI / 3; Point p3 = {(p1.x + p2.x) / 2 + (p2.y - p1.y) * sqrt(3) / 2, (p1.y + p2.y) / 2 - (p2.x - p1.x) * sqrt(3) / 2}; drawKoch(a, p1, depth - 1); drawKoch(p1, p3, depth - 1); drawKoch(p3, p2, depth - 1); drawKoch(p2, b, depth - 1); } void display() { glClear(GL_COLOR_BUFFER_BIT); drawBezier(); drawKoch({100, 100}, {500, 100}, depth); glFlush(); } void keyboard(unsigned char key, int x, int y) { if (key == 'q') exit(0); } void init() { glClearColor(1.0, 1.0, 1.0, 1.0); glMatrixMode(GL_PROJECTION); gluOrtho2D(0, 600, 0, 600); depth = 3; } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(600, 600); glutInitWindowPosition(100, 100); glutCreateWindow("Bezier & Koch Curve Fractals"); init(); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; } // ================================================ // Boundryfill // ================================================ #include <GL/freeglut.h> #include <iostream> using namespace std; int fillColor[3] = {1, 0, 0}; // Red fill color int boundaryColor[3] = {0, 0, 0}; // Black boundary color void drawPixel(int x, int y, int color[3]) { glColor3f(color[0], color[1], color[2]); glBegin(GL_POINTS); glVertex2i(x, y); glEnd(); glFlush(); } void getPixelColor(int x, int y, int color[3]) { glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, color); } void boundaryFill(int x, int y, int fillColor[3], int boundaryColor[3]) { int color[3]; getPixelColor(x, y, color); if ((color[0] != boundaryColor[0] || color[1] != boundaryColor[1] || color[2] != boundaryColor[2]) && (color[0] != fillColor[0] || color[1] != fillColor[1] || color[2] != fillColor[2])) { drawPixel(x, y, fillColor); boundaryFill(x + 1, y, fillColor, boundaryColor); boundaryFill(x - 1, y, fillColor, boundaryColor); boundaryFill(x, y + 1, fillColor, boundaryColor); boundaryFill(x, y - 1, fillColor, boundaryColor); } } void display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.0, 0.0, 0.0); glBegin(GL_LINE_LOOP); glVertex2i(100, 100); glVertex2i(300, 100); glVertex2i(300, 300); glVertex2i(100, 300); glEnd(); glFlush(); } void mouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { y = 500 - y; boundaryFill(x, y, fillColor, boundaryColor); } } void init() { glClearColor(1.0, 1.0, 1.0, 1.0); glColor3f(0.0, 0.0, 0.0); glPointSize(1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, 500, 0, 500); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("Boundary Fill Algorithm"); init(); glutDisplayFunc(display); glutMouseFunc(mouse); glutMainLoop(); return 0; } // ================================================ // Breshnamscircle // ================================================ #include <GL/freeglut.h> #include <iostream> using namespace std; // Function to draw a pixel at (x, y) void drawPixel(int x, int y) { glBegin(GL_POINTS); glVertex2i(x, y); glEnd(); } // Function to draw a circle using Bresenham̢۪s Algorithm void drawCircle(int xc, int yc, int r) { int x = 0, y = r; int d = 3 - 2 * r; while (x <= y) { // Plot symmetric points in all eight octants drawPixel(xc + x, yc + y); drawPixel(xc - x, yc + y); drawPixel(xc + x, yc - y); drawPixel(xc - x, yc - y); drawPixel(xc + y, yc + x); drawPixel(xc - y, yc + x); drawPixel(xc + y, yc - x); drawPixel(xc - y, yc - x); x++; if (d > 0) { y--; d = d + 4 * (x - y) + 10; } else { d = d + 4 * x + 6; } } } // OpenGL display function void display() { glClear(GL_COLOR_BUFFER_BIT); // Clear the screen // Draw circles in all four quadrants drawCircle(0, 0, 100); drawCircle(150, 150, 100); drawCircle(-150, 150, 100); drawCircle(150, -150, 100); drawCircle(-150, -150, 100); glFlush(); // Render graphics } // OpenGL initialization void init() { glClearColor(0.0, 0.0, 0.0, 1.0); // Black background glColor3f(1.0, 1.0, 1.0); // White drawing color glPointSize(2.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-250, 250, -250, 250); // Set coordinate system } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("Bresenham's Circle Drawing"); init(); glutDisplayFunc(display); glutMainLoop(); return 0; } // ================================================ // Bresnhams // ================================================ #include <iostream> #include <GL/glu.h> #include <GL/freeglut.h> using namespace std; void myInit() { glClearColor(0, 0, 0, 1); glColor3f(1, 0, 0); gluOrtho2D(0, 700, 0, 480); glClear(GL_COLOR_BUFFER_BIT); } void bresnham() { int x1, y1, x2, y2; cout << "\n Enter Start Vertex: "; cin >> x1 >> y1; cout << "\n Enter End Vertex: "; cin >> x2 >> y2; int dx = x2 - x1, dy = y2 - y1, step; int p = 2 * dy - dx; int x, y; while (x <= x2) { glBegin(GL_POINTS); glVertex2f(x, y); glEnd(); if (p <= 0) { p = 2 * dy - 2 * dx; x = x + 1; y = y; } else { p = p + 2 * dy - 2 * dx; x += 1; y += 1; } } glFlush(); } int main(int n, char *v[]) { glutInit(&n, v); glutInitWindowSize(700, 480); glutCreateWindow("DDA Line Drawing"); myInit(); glutDisplayFunc(bresnham); glutMainLoop(); return 0; } // ================================================ // Cohensutherland // ================================================ #include <GL/freeglut.h> #include <iostream> using namespace std; const int INSIDE = 0; // 0000 const int LEFT = 1; // 0001 const int RIGHT = 2; // 0010 const int BOTTOM = 4; // 0100 const int TOP = 8; // 1000 int x_min = 100, y_min = 100, x_max = 400, y_max = 400; int computeCode(int x, int y) { int code = INSIDE; if (x < x_min) code |= LEFT; else if (x > x_max) code |= RIGHT; if (y < y_min) code |= BOTTOM; else if (y > y_max) code |= TOP; return code; } void cohenSutherlandClip(int x1, int y1, int x2, int y2) { int code1 = computeCode(x1, y1); int code2 = computeCode(x2, y2); bool accept = false; while (true) { if ((code1 == 0) && (code2 == 0)) { accept = true; break; } else if (code1 & code2) { break; } else { int x, y; int code_out = code1 ? code1 : code2; if (code_out & TOP) { x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1); y = y_max; } else if (code_out & BOTTOM) { x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1); y = y_min; } else if (code_out & RIGHT) { y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1); x = x_max; } else if (code_out & LEFT) { y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1); x = x_min; } if (code_out == code1) { x1 = x; y1 = y; code1 = computeCode(x1, y1); } else { x2 = x; y2 = y; code2 = computeCode(x2, y2); } } } if (accept) { glColor3f(0, 1, 0); glBegin(GL_LINES); glVertex2i(x1, y1); glVertex2i(x2, y2); glEnd(); glFlush(); } } void display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1, 0, 0); glBegin(GL_LINE_LOOP); glVertex2i(x_min, y_min); glVertex2i(x_max, y_min); glVertex2i(x_max, y_max); glVertex2i(x_min, y_max); glEnd(); glFlush(); } void mouse(int button, int state, int x, int y) { static int count = 0; static int x1, y1, x2, y2; if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { if (count == 0) { x1 = x; y1 = 500 - y; count++; } else { x2 = x; y2 = 500 - y; cohenSutherlandClip(x1, y1, x2, y2); count = 0; } } } void init() { glClearColor(1.0, 1.0, 1.0, 1.0); glColor3f(0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, 500, 0, 500); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("Cohen-Sutherland Clipping"); init(); glutDisplayFunc(display); glutMouseFunc(mouse); glutMainLoop(); return 0; } // ================================================ // Ddaline // ================================================ #include <iostream> #include <GL/glu.h> #include <GL/freeglut.h> using namespace std; void myInit() { glClearColor(0, 0, 0, 1); glColor3f(1, 0, 0); gluOrtho2D(0, 700, 0, 480); glClear(GL_COLOR_BUFFER_BIT); } void dda() { int x1, y1, x2, y2; cout << "\n Enter Start Vertex: "; cin >> x1 >> y1; cout << "\n Enter End Vertex: "; cin >> x2 >> y2; int dx = x2 - x1, dy = y2 - y1, step; if (abs(dx) >= abs(dy)) step = abs(dx); else step = abs(dy); float xin = dx / (float)step, yin = dy / (float)step; float x = x1, y = y1; int i = 0; while (i <= step) { glBegin(GL_POINTS); glVertex2f(x, y); glEnd(); x = x + xin; y = y + yin; i++; } glFlush(); } int main(int n, char *v[]) { glutInit(&n, v); glutInitWindowSize(700, 480); glutCreateWindow("DDA Line Drawing"); myInit(); glutDisplayFunc(dda); glutMainLoop(); return 0; } // ================================================ // Floodfill // ================================================ #include <GL/freeglut.h> #include <iostream> using namespace std; int fillColor[3] = {1, 0, 0}; // Red color int boundaryColor[3] = {0, 0, 0}; // Black boundary void drawPixel(int x, int y, int color[3]) { glColor3f(color[0], color[1], color[2]); glBegin(GL_POINTS); glVertex2i(x, y); glEnd(); glFlush(); } void getPixelColor(int x, int y, int color[3]) { glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, color); } void floodFill(int x, int y, int fillColor[3], int boundaryColor[3]) { int color[3]; getPixelColor(x, y, color); if ((color[0] != boundaryColor[0] || color[1] != boundaryColor[1] || color[2] != boundaryColor[2]) && (color[0] != fillColor[0] || color[1] != fillColor[1] || color[2] != fillColor[2])) { drawPixel(x, y, fillColor); floodFill(x + 1, y, fillColor, boundaryColor); floodFill(x - 1, y, fillColor, boundaryColor); floodFill(x, y + 1, fillColor, boundaryColor); floodFill(x, y - 1, fillColor, boundaryColor); } } void display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.0, 0.0, 0.0); glBegin(GL_LINE_LOOP); glVertex2i(100, 100); glVertex2i(300, 100); glVertex2i(300, 300); glVertex2i(100, 300); glEnd(); glFlush(); } void mouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { y = 500 - y; floodFill(x, y, fillColor, boundaryColor); } } void init() { glClearColor(1.0, 1.0, 1.0, 1.0); glColor3f(0.0, 0.0, 0.0); glPointSize(1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, 500, 0, 500); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("Flood Fill Algorithm"); init(); glutDisplayFunc(display); glutMouseFunc(mouse); glutMainLoop(); return 0; } // ================================================ // Transformation2D // ================================================ #include <GL/freeglut.h> #include <iostream> #include <cmath> using namespace std; float points[3][2] = {{100, 100}, {200, 100}, {150, 200}}; int choice; float sx, sy, angle, rx, ry; void drawTriangle() { glBegin(GL_TRIANGLES); glVertex2f(points[0][0], points[0][1]); glVertex2f(points[1][0], points[1][1]); glVertex2f(points[2][0], points[2][1]); glEnd(); } void scaling() { cout << "Enter scaling factors (sx, sy): "; cin >> sx >> sy; for (int i = 0; i < 3; i++) { points[i][0] *= sx; points[i][1] *= sy; } } void rotate() { cout << "Enter rotation angle and pivot point (rx, ry): "; cin >> angle >> rx >> ry; angle = angle * 3.14159 / 180.0; for (int i = 0; i < 3; i++) { float x = points[i][0] - rx; float y = points[i][1] - ry; points[i][0] = rx + (x * cos(angle) - y * sin(angle)); points[i][1] = ry + (x * sin(angle) + y * cos(angle)); } } void reflection() { cout << "Enter axis (1 for X, 2 for Y): "; int axis; cin >> axis; if (axis == 1) { for (int i = 0; i < 3; i++) points[i][1] = -points[i][1]; } else { for (int i = 0; i < 3; i++) points[i][0] = -points[i][0]; } } void display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 0.0, 0.0); drawTriangle(); glFlush(); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 's': scaling(); break; case 'r': rotate(); break; case 'f': reflection(); break; case 'q': exit(0); } glutPostRedisplay(); } void init() { glClearColor(1.0, 1.0, 1.0, 1.0); glMatrixMode(GL_PROJECTION); gluOrtho2D(-300, 300, -300, 300); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("2D Transformations"); init(); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; }