0% found this document useful (0 votes)
47 views12 pages

CG Project

Uploaded by

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

CG Project

Uploaded by

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

Department of Computer Science & Engineering

National Institute of Technology Srinagar


Hazratbal, Srinagar, Jammu & Kashmir-190006, India

Computer Graphics Lab [CSL 358]

A Project submitted on

Implementation of a 3D Clock
by

Mohammad Mehran 2021BCSE086


Mirza Muneer 2021BCSE085
Mushkan Narayan 2021BCSE047

III Year B-Tech CSE (6th Semester)

Submitted to

Dr. Pramod Kumar Yadav

SPRING 2024
Serial number Heading Page no.
1. Abstract 3
2. Introduction 4
3. Implementation 5-7
4. Source code 8-11
5. Output 12
6. Conclusion 12
Abstract :

The provided code implements an analog clock using the Turbo C graphics library. It
calculates the positions of the clock numbers and the positions of the clock hands
(hour, minute, and second) based on trigonometric functions. The calcPoints
function determines the positions for the clock numbers and the hour hand, while the
minSecCalc() function calculates the positions for the minute and second hands.
The main function initializes the graphics mode, calculates the midpoint of the
screen, and sets the radius of the clock. In a continuous loop, the program updates the
clock hands to reflect the current time using the time() and localtime()
functions. It draws the clock hands for seconds, minutes, and hours, and then updates
the display every second. The loop terminates when a key is pressed, and the graphics
mode is closed. This code demonstrates the use of graphics programming in C to
create a functional analog clock.
Introduction:

The Real-time Clock Simulation project aims to create a digital clock using C programming
language and graphics libraries. This project leverages concepts such as trigonometry, time
management, and graphical user interface (GUI) programming to simulate the behaviour of a real-
world clock on a computer screen.
The key features of this clock simulation include:
1. Real-time Updates: The clock updates in real-time, displaying the current hour, minute, and
second accurately.
2. Graphical Representation: The clock is visually represented with hour, minute, and second
needles, along with numerals for each hour on the clock face.
3. User Interaction: Users can interact with the clock by observing the time change
dynamically or by pausing the simulation.
4. Educational Value: This project serves as an educational tool for understanding concepts
such as graphics programming, time management, and mathematical calculations involved
in clock simulations.
By implementing this project, developers can enhance their skills in C programming, graphics
handling, and real-time data management, making it a valuable learning experience and a visually
appealing application.
Implementation Details: Real-time Clock Simulation
1. Header Files:

• Include necessary header files such as stdio.h, conio.h, math.h, time.h,


and graphics.h for input/output operations, console input, mathematical
calculations, time management, and graphics handling, respectively.

2. Graphics Initialization:

• Initialize the graphics mode using initgraph function and handle any errors using
graphresult to ensure the graphics environment is set up correctly.
3. Clock Elements Calculation:

Implement functions to calculate the positions of clock elements:


• calcPoints: Calculate positions for hour numerals and major hour
markers on the clock face.
• minSecCalc: Calculate positions for minute and second markers on the
clock face.
4. Main Function:

• In the main function:


• Declare variables for graphics mode, error handling, clock dimensions
(radius, midx, midy), time components (hour, minute, second), and arrays for
storing needle positions.
• Initialize graphical mode, get screen dimensions (midx, midy), and set clock
radius.
• Call functions to calculate positions for clock elements (calcPoints,
minSecCalc for hour, minute, and second needles).
• Enter a loop to update and redraw the clock display in real-time:
• Get current time using time function and localtime struct.
• Update needle positions based on current time.
• Draw clock face, numerals, and needles using graphical functions
(circle, line, outtext, etc.).
• Delay for one second using delay function to synchronize with real-
time clock.
• Clear previous frame using cleardevice before redrawing.
5. User Interaction (Optional):
• Implement optional user interaction features such as pausing/resuming the clock
simulation or adjusting clock appearance.
• Use functions like kbhit to detect keyboard input for user commands.
6. Graphics Cleanup:
• After the main loop exits (e.g., user closes the program), close the graphics
environment using closegraph.
7. Compilation and Execution:
• Compile the program using a C compiler that supports graphics libraries (e.g., Turbo
C, Dev-C++, Code::Blocks with appropriate settings).
• Execute the compiled program to run the clock simulation.
This implementation provides a functional real-time clock simulation with graphical representation
and dynamic updates based on system time.
Source Code:

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <graphics.h>
#include <dos.h>

/*
* find the points at 0, 30, 60,.., 360 degrees
* on the given circle. x value correponds to
* radius * cos(angle) and y value corresponds
* to radius * sin(angle). Numbers in the clock
* are written using the above manipulated x and
* y values. And the hour needle movement
* is based on this
*/

void calcPoints(int radius, int midx, int midy, int x[12], int y[12]) {
int x1, y1;

/* 90, 270, 0, 180 degrees */


x[0] = midx, y[0] = midy - radius;
x[6] = midx, y[6] = midy + radius;
x[3] = midx + radius, y[3] = midy;
x[9] = midx - radius, y[9] = midy;

/* 30, 150, 210, 330 degrees */


x1 = (int) ((radius / 2) * sqrt(3));
y1 = (radius / 2);
x[2] = midx + x1, y[2] = midy - y1;
x[4] = midx + x1, y[4] = midy + y1;
x[8] = midx - x1, y[8] = midy + y1;
x[10] = midx - x1, y[10] = midy - y1;

/* 60, 120, 210, 300 degrees */


x1 = radius / 2;
y1 = (int) ((radius / 2) * sqrt(3));
x[1] = midx + x1, y[1] = midy - y1;
x[5] = midx + x1, y[5] = midy + y1;
x[7] = midx - x1, y[7] = midy + y1;
x[11] = midx - x1, y[11] = midy - y1;

return;
}

/*
* Calculates position for minute and second needle movement
* Each quadrant has 90 degrees. So, we need to split each
* quadrant into 15 parts(6 degree each) to get the minute
* and second needle movement
*/
void minSecCalc(int radius, int midx, int midy, int secx[60], int secy[60]) {
int i, j = 0, x, y;
char str[32];

/* 15 position(min/sec - 12 to 3) in first quadrant of clock */


secx[j] = midx, secy[j++] = midy - radius;

for (i = 96; i < 180; i = i + 6) {


secx[j] = midx - (radius * cos((i * 3.14) / 180));
secy[j++] = midy - (radius * sin((i * 3.14) / 180));
}

/* 15 positions(min or sec - 3 to 6) in second quadrant of clock */


secx[j] = midx + radius, secy[j++] = midy;
for (i = 186; i < 270; i = i + 6) {
secx[j] = midx - (radius * cos((i * 3.14) / 180));
secy[j++] = midy - (radius * sin((i * 3.14) / 180));
}

/* 15 positions(min or sec - 6 to 9) in third quadrant of clock */


secx[j] = midx, secy[j++] = midy + radius;
for (i = 276; i < 360; i = i + 6) {
secx[j] = midx - (radius * cos((i * 3.14) / 180));
secy[j++] = midy - (radius * sin((i * 3.14) / 180));
}

/* 15 positions(min or sec - 9 to 12) in fourth quadrant of clock */


secx[j] = midx - radius, secy[j++] = midy;
for (i = 6; i < 90; i = i + 6) {
secx[j] = midx - (radius * cos((i * 3.14) / 180));
secy[j++] = midy - (radius * sin((i * 3.14) / 180));
}

return;
}

int main() {
/* request auto detection */
int gdriver = DETECT, gmode, err, tmp;
int i, j, midx, midy, radius, hr, min, sec;
int x[12], y[12], minx[60], miny[60];
int hrx[12], hry[12], secx[60], secy[60];
int secx1, secy1;
char str[256];
time_t t1;
struct tm *data;

/* initialize graphic mode */


initgraph(&gdriver, &gmode, "C:/TURBOC3/BGI");
err = graphresult();

if (err != grOk) {
/* error occurred */
printf("Graphics Error: %s",
grapherrormsg(err));
return 0;
}

/* mid position in x and y -axis */


midx = getmaxx() / 2;
midy = getmaxy() / 2;

radius = 200;

/* position to locate numbers in clock */


calcPoints(radius - 30, midx, midy, x, y);

/* gets position for hour needle */


calcPoints(radius - 90, midx, midy, hrx, hry);

/* gets position for minute needle */


minSecCalc(radius - 50, midx, midy, minx, miny);

/* gets position for second needle */


minSecCalc(radius - 70, midx, midy, secx, secy);

while (!kbhit()) {
setlinestyle(SOLID_LINE, 1, 3);
settextstyle(TRIPLEX_FONT, 0, 3);

/* draws frame of the clock */


circle(midx, midy, radius);

/* place the numbers in clock */


for (j = 0; j < 12; j++) {
if (j == 0) {
sprintf(str, "%d", 12);
} else {
sprintf(str, "%d", j);
}
settextjustify(CENTER_TEXT, CENTER_TEXT);
moveto(x[j], y[j]);
outtext(str);
}

/* get the current time using time() API */


t1 = time(NULL);
data = localtime(&t1);

/* draw the second needle in clock */


sec = data->tm_sec % 60;
line(midx, midy, secx[sec], secy[sec]);

/* draw the minute needle in clock */


min = data->tm_min % 60;
line(midx, midy, minx[min], miny[min]);

/* draw the hour needle in clock */


hr = data->tm_hour % 12;
line(midx, midy, hrx[hr], hry[hr]);

/* sleep for a second */


delay(1000);
cleardevice();
}

getch();

/* deallocate memory allocated for graphic screen */


closegraph();
return 0;
}
OUTPUT:

Conclusion:

The Real-time Clock Simulation project in C provides a practical


implementation of a digital clock with real-time updates and graphical
representation. By leveraging graphics libraries and time management
functions, the project achieves dynamic clock display with hour, minute,
and second needles on a graphical interface. This project enhances
programming skills in C, graphics handling, and real-time data
management, making it a valuable learning experience for developers
interested in graphical simulations and interactive applications.

You might also like