CS602 – Computer graphics Total Marks = 20
Assignment No.2 Solution Deadline
Semester: Spring 2025 th
28 of Jun 2025
Please carefully read the following instructions before attempting the assignment Solution.
NOTE
Don't copy-paste the same answer.
Make sure you can make some changes to your solution file before submitting copy
paste solution will be marked zero.
If you found any mistake then correct yourself and inform me.
Before submitting an Assignment GDB checks your assignment requirement file.
For any query, feel free to Contact at
WhatsApp: +923074960034
Provide by M.JUNAID QAZI
Code:
#include <GL/glut.h>
#include <cmath>
#include <vector>
#include <ctime>
struct Shape {
float x, y;
char type;
float angle;
float scale;
float r, g, b;
};
std::vector<Shape> shapes;
char currentShape = 'S';
float currentR = 1, currentG = 0, currentB = 0; // Default Red
void drawStar(float x, float y, float angle, float scale, float r, float g, float b) {
glPushMatrix();
CONTACT ON WHATSAPP
+923074960034
glTranslatef(x, y, 0);
glRotatef(angle, 0, 0, 1);
glScalef(scale, scale, 1);
glColor3f(r, g, b);
glBegin(GL_TRIANGLES);
for (int i = 0; i < 5; ++i) {
float theta = i * 2.0f * 3.1415926f / 5.0f;
float next = ((i + 2) % 5) * 2.0f * 3.1415926f / 5.0f;
glVertex2f(0.0f, 0.0f);
glVertex2f(cos(theta), sin(theta));
glVertex2f(cos(next), sin(next));
}
glEnd();
glPopMatrix();
}
void drawPolygon(float x, float y, float angle, float scale, float r, float g, float b, int sides = 6) {
glPushMatrix();
glTranslatef(x, y, 0);
glRotatef(angle, 0, 0, 1);
glScalef(scale, scale, 1);
glColor3f(r, g, b);
glBegin(GL_POLYGON);
for (int i = 0; i < sides; i++) {
float theta = 2.0f * 3.1415926f * i / sides;
glVertex2f(cos(theta), sin(theta));
}
glEnd();
glPopMatrix();
}
void drawSpiral(float x, float y, float angle, float scale, float r, float g, float b) {
glPushMatrix();
glTranslatef(x, y, 0);
glRotatef(angle, 0, 0, 1);
glScalef(scale, scale, 1);
glColor3f(r, g, b);
glBegin(GL_LINE_STRIP);
for (float theta = 0; theta < 6 * 3.1415926f; theta += 0.1f) {
float radius = theta * 0.05f;
glVertex2f(radius * cos(theta), radius * sin(theta));
}
glEnd();
glPopMatrix();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
for (int i = 0; i < shapes.size(); ++i) {
CONTACT ON WHATSAPP
+923074960034
Shape s = shapes[i];
if (s.type == 'S') drawStar(s.x, s.y, s.angle, s.scale, s.r, s.g, s.b);
else if (s.type == 'P') drawPolygon(s.x, s.y, s.angle, s.scale, s.r, s.g, s.b);
else if (s.type == 'L') drawSpiral(s.x, s.y, s.angle, s.scale, s.r, s.g, s.b);
}
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
Shape s;
s.x = (float)(x - 250) / 250; // Normalize
s.y = (float)(250 - y) / 250;
s.type = currentShape;
s.angle = 0;
s.scale = 0.2f;
s.r = currentR;
s.g = currentG;
s.b = currentB;
shapes.push_back(s);
glutPostRedisplay();
}
}
void keyboard(unsigned char key, int, int) {
if (key == 'S' || key == 's') currentShape = 'S';
else if (key == 'P' || key == 'p') currentShape = 'P';
else if (key == 'L' || key == 'l') currentShape = 'L';
else if (key == '+') shapes.back().scale += 0.05f;
else if (key == '-') shapes.back().scale -= 0.05f;
else if (key == '1') { currentR = 1; currentG = 0; currentB = 0; } // Red
else if (key == '2') { currentR = 0; currentG = 1; currentB = 0; } // Green
else if (key == '3') { currentR = 0; currentG = 0; currentB = 1; } // Blue
else if (key == '4') { currentR = 1; currentG = 1; currentB = 0; } // Yellow
else if (key == 'C' || key == 'c') shapes.clear();
glutPostRedisplay();
}
void specialKeys(int key, int, int) {
if (shapes.empty()) return;
if (key == GLUT_KEY_LEFT) shapes.back().angle += 5;
else if (key == GLUT_KEY_RIGHT) shapes.back().angle -= 5;
glutPostRedisplay();
}
void init() {
glClearColor(1, 1, 1, 1);
CONTACT ON WHATSAPP
+923074960034
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-1, 1, -1, 1);
srand(time(0));
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(500, 500);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("Geometric Pattern Designer");
init();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialKeys);
glutMainLoop();
return 0;
}
Every Assignment/GDB is change due to unique Student ID so don’t copy
That is truly perfect step by step idea solution get help easily.
Wish you the very best of Luck!
CONTACT ON WHATSAPP
+923074960034