#include<windows.h> #define GLUT_DISABLE_ATEXIT_HACK #include <gl/glut.h> #include <math.h>//for PI value , sine and cosine function that we used to draw lines float PI= 3.141592654; float angleHour = 0, angleMin = 0, angleSec = 0, clockR = 80.0f, angle1min = PI / 30.0f, minStart = 0.9f, minEnd = 1.0f, stepStart = 0.8f, stepEnd = 1.0f; void init() { glClearColor(0.0f, 0.5f, 1.0f, 1.0f); glMatrixMode(GL_PROJECTION); glOrtho(-150, 150, -100, 100, -100, 100); } //number method is used to write the numbers found in the clock void numbers() { glRasterPos2i(58, 0); glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, '3'); glRasterPos2i(0, -60); glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, '6'); glRasterPos2i(-62, -2); glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, '9'); glRasterPos2i(-2, 57); glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, '1'); glRasterPos2i(1, 57); glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, '2'); } //display function responsible to design the classic clock interface void Display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f, 0.0f, 0.0f); glLineWidth(2); glEnable(GL_LINE_SMOOTH);//for drawing smooth lines glBegin(GL_LINES); for (int i = 0; i < 60; i++) { float c = cos(i * angle1min), s = sin(i * angle1min); if (i % 5) { // normal minute if (i % 5 == 1) glColor3f(1.0f, 1.0f, 1.0f); //The following is to draw white lines around the circle glVertex2f(clockR * minStart * c, clockR * minStart * s); glVertex2f(clockR * minEnd * c, clockR * minEnd * s); } else { glColor3f(0.0f, 1.0f, 0.0f); //The following is to draw red long lines around the circle glVertex2f(clockR * stepStart * c, clockR * stepStart * s); glVertex2f(clockR * stepEnd * c, clockR * stepEnd * s); } } glEnd(); numbers(); //the following is for second hand glColor3f(0.0f, 1.0f, 0.0f); glPushMatrix(); glTranslatef(-1, 3, 0); glPushMatrix(); glRotatef(-angleSec, 0, 0, 1); glPushMatrix(); glTranslatef(1, -3, 0); glBegin(GL_LINES); glVertex2i(-1, 3); glVertex2i(9, -45); glEnd(); glBegin(GL_TRIANGLES); glVertex2i(9, -48); glVertex2i(9, -42); glVertex2i(9, -40); glEnd(); glPopMatrix(); glPopMatrix(); glPopMatrix(); //for minute hand glLineWidth(8); glColor3f(0.0, 0.0, 1.0); glPushMatrix(); glTranslatef(-1, 3, 0); glPushMatrix(); glRotatef(-angleMin, 0, 0, 1); glPushMatrix(); glTranslatef(1, -3, 0); glBegin(GL_LINES); glVertex2i(-1, 3); glVertex2i(40, 2); glEnd(); glBegin(GL_TRIANGLES); glVertex2i(40, -3); glVertex2i(40, 7); glVertex2i(45, 2); glEnd(); glPopMatrix(); glPopMatrix(); glPopMatrix(); //for hour hand glLineWidth(10); glColor3f(1.0f, 0.0f, 0.0f); glPushMatrix(); glTranslatef(-1, 3, 0); glPushMatrix(); glRotatef(-angleHour, 0, 0, 1); glPushMatrix(); glTranslatef(1, -3, 0); glBegin(GL_LINES); glVertex2i(-1, 3); glVertex2i(0, 35); glEnd(); glBegin(GL_TRIANGLES); glVertex2i(-5, 35); glVertex2i(0, 40); glVertex2i(5, 35); glEnd(); glPopMatrix(); glPopMatrix(); glPopMatrix(); //circle design glPushMatrix(); glTranslatef(-140, -160, 0); float Xpos = 140, Ypos = 160, radius = 90; glColor3f(1.0, 0.0, 0.0); glBegin(GL_LINE_STRIP); for (float i = 0; i < 360; i += 0.01) glVertex2f(Xpos + sin(i) * radius, Ypos + cos(i) * radius); glEnd(); radius = 80; glColor3f(0.0, 1.0, 0.0); glBegin(GL_LINE_STRIP); for (float i = 0; i < 360; i += 0.01) glVertex2f(Xpos + sin(i) * radius, Ypos + cos(i) * radius); glEnd(); radius = 85; glColor3f(1.0, 1.0, 0.0); glBegin(GL_LINE_STRIP); for (float i = 0; i < 360; i += 0.01) glVertex2f(Xpos + sin(i) * radius, Ypos + cos(i) * radius); glEnd(); glLineWidth(15); glPopMatrix(); glColor3f(1.0, 0.5, 0.5); glPointSize(12); glBegin(GL_POINTS); glVertex2i(-1, 3); glEnd(); glFlush(); } //rotate function manages how the hour,minute and second //hand are simulated void rotate() { if (angleSec < 360) { angleSec += 0.03; } else { angleSec = 0; } if (angleMin < 360) { angleMin += 0.00085; } else { angleMin = 0; } if (angleHour < 360) { angleHour += 0.0000146; } else { angleHour = 0; } glutPostRedisplay(); } int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(100, 10); glutInitWindowSize(1080, 780); glutCreateWindow("Classic Clock Simulation"); init(); glutDisplayFunc(Display); glutIdleFunc(rotate); glutMainLoop(); return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}