0% found this document useful (0 votes)
59 views11 pages

CS602-Assignment 2 Solution Spring 2024

Uploaded by

michealcole2812
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)
59 views11 pages

CS602-Assignment 2 Solution Spring 2024

Uploaded by

michealcole2812
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/ 11

STU ID: BC210201541

Code:

#include <GL/glut.h>

#include <cmath>

#include <ctime>

#include <string>

// Window dimensions

const int WINDOW _WIDTH = 600;

CONTACT ON WHATSAPP
+923074960034
const int WINDOW _HEIGHT = 600;

// User ID

const char* userID = "YourID";

// Function to convert degrees to radians

float degToRad(float degrees) {

return degrees * M_ PI / 180.0f;


}

// Function to draw a string (used for displaying the ID)

void drawString(float x, float y, const char* str){

glRasterPos2f(x, y);

for (const char* c = str; *c != '\0'; ++c) {

glutBitmapCharacter(GLUT BITMAP HELVETICA 18, *c);

// Function to draw the clock face

void drawClockFace()

CONTACT ON WHATSAPP
+923074960034
// Draw the circle

glColor3f(1.0, 1.0, 1.0);

glBegin(GL_ LIN_LOOP);

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

float theta = degToRad(i);

glVertex2f(cos(theta), sin(theta));

}
glEnd();

// Draw the hour and minute ticks

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

float theta = degToRad(i * 6); // 360 / 60

float x1 = cos(theta);

float y1 = sin(theta);

float x2 = (i % 5 == 0) ? x1 * 0.9f: x1 * 0.95f; // Longer ticks for hours

float y2 = (i % 5 == 0) ? y1 * 0.9f: y1 * 0.95f;

glBegin(GL _LINES);

glVertex2f(x1, y1);

glVertex2f(x2, y2);

glEnd();
}

CONTACT ON WHATSAPP
+923074960034
}

// Function to draw the clock hands

void drawClockHands(int hour, int minute, int second) {

// Draw the second hand

glColor3f(1.0, 0.0, 0.0);

float secondAngle = degToRad(90 - second * 6);

glBegin(GL _LINES);

glVertex2f(0.0, 0.0);

glVertex2f(0.9f * cos(secondAngle), 0.9f* sin(secondAngle));

glEnd();

// Draw the minute hand

glColor3f(0.0, 1.0, 0.0);

float minuteAngle = degToRad(90 - (minute * 6 + second * 0.1f));

glBegin(GL_ LINES);

glVertex2f(0.0, 0.0);

glVertex2f(0.8f* cos(minuteAngle), 0.8f* sin(minuteAngle));

glEnd();

// Draw the hour hand

glColor3f(0.0, 0.0, 1.0);

CONTACT ON WHATSAPP
+923074960034
float hourAngle = degToRad(90 - (hour * 30 + minute * 0.5f));

glBegin(GL _LINES);

glVertex2f(0.0, 0.0);

glVertex2f(0.6f* cos(hourAngle), 0.6f* sin(hourAngle));

glEnd();

// Function to display the clock and the user ID

void display() {

glClear(GL _COLOR _ BUFFER _ BIT);

glLoadIdentity();

glScalef(0.8f, 0.8f, 1.0f); // Center the clock

drawClockFace();

// Get the current time

time _t now =time(0);

tm* utcTime = gmtime(&now);

utcTime->tm hour += 5; // Convert to Pakistan Standard Time (UTC+5)

mktime(utcTime); // Normalize the time structure

int hour = utcTime->tm hour % 12; // Convert to 12-hour format

CONTACT ON WHATSAPP
+923074960034
int minute = utcTime->tm _min;

int second = utcTime->tm _sec;

drawClockHands(hour, minute, second);

// Display the user ID

glLoadIdentity();

glColor3f(1.0, 1.0,

1.0);

drawString(-0.98f, 0.9f, userID);

glutSwapBuffers();

// Timer function to update the display every second

void timer(int value) {

glutPostRedisplay();

glutTimerFunc(1000, timer, 0);

// Function to initialize OpenGL settings

void initOpenGL() {

CONTACT ON WHATSAPP
+923074960034
glClearColor(0.0, 0.0, 0.0, 0.0);

glMatrixMode(GL _PROJECTION);

glLoadIdentity();

gluOrtho2D(-1.0, 1.0, -1.0, 1.0);

glMatrixMode(GL _ MODELVIEW);

// Main function

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT _ DOUBLE |GLUT _RGB);

glutInitWindowSize(WINDOW _WIDTH, WINDOW _HEIGHT);

glutCreateWindow("Analog Clock");

initOpenGL();

glutDisplayFunc(display);

glutTimerFunc(0, timer, 0);

glutMainLoop();

return 0;

CONTACT ON WHATSAPP
+923074960034
CONTACT ON WHATSAPP
+923074960034
CONTACT ON WHATSAPP
+923074960034

You might also like