Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <bevgrafmath2017.h>
- int mainCircleRad = 30, dotsRad = 5, dotsCount = 10;
- float circleSpeed = 0.5f;
- vec2 dotsPoses[10];
- float width = 800, height = 600;
- GLint novX = 1, novY=1;
- GLint halfSize = 50;
- vec2 center = { width/2, height/2 };
- void init()
- {
- glClearColor(1.0, 1.0, 1.0, 0.0);
- glMatrixMode(GL_PROJECTION);
- gluOrtho2D(0.0, width, 0.0, height);
- glShadeModel(GL_FLAT);
- }
- void Circle(vec2 center,float r, bool fill = false) {
- int segments = 200;
- glLineWidth(2);
- glColor3ub(0, 0, 0);
- if(fill)
- glBegin(GL_POLYGON);
- else
- glBegin(GL_LINE_LOOP);
- for (float t = 0; t <two_pi(); t += (pi() / 180))
- {
- float x = r * cosf(t) + center[0];
- float y = r * sinf(t) + center[1];
- glVertex2f(x, y);
- }
- glEnd();
- glFlush();
- }
- void GeneratePoses()
- {
- for (int i = 0; i < dotsCount; i++) {
- dotsPoses[i].x = rand();
- while (dotsPoses[i].x < dotsRad || dotsPoses[i].x > width- dotsRad)
- dotsPoses[i].x = rand();
- dotsPoses[i].y = rand();
- while (dotsPoses[i].y < dotsRad || dotsPoses[i].y > height- dotsRad)
- dotsPoses[i].y = rand();
- }
- }
- bool lock = false;
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.0, 0.0, 1.0);
- Circle(center, mainCircleRad);
- if (!lock)
- {
- GeneratePoses();
- lock = true;
- }
- for (int c = 0; c < dotsCount; c++)
- {
- if (dist(dotsPoses[c], center) < mainCircleRad)
- {
- dotsPoses[c] = vec2{ -100,-100 };
- mainCircleRad += 5;
- continue;
- }
- Circle(dotsPoses[c], dotsRad, true);
- }
- glutSwapBuffers();
- }
- void update(int n)
- {
- center[0] += novX * circleSpeed;
- center[1] += novY * circleSpeed;
- if (center[0] + mainCircleRad > width || center[0] - mainCircleRad < 0)
- {
- novX *= -1;
- center[0] += novX * 5;
- }
- if (center[1] + mainCircleRad > height || center[1] - mainCircleRad < 0)
- {
- novY *= -1;
- center[1] += novY * 5;
- }
- glutPostRedisplay();
- glutTimerFunc(5, update, 0);
- }
- void keyboard(unsigned char key, int x, int y)
- {
- switch (key) {
- case 27:
- exit(0);
- break;
- case 'w':
- novX *= -1;
- break;
- case 'd':
- novY *= -1;
- break;
- }
- }
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize(width, height);
- glutInitWindowPosition(100, 100);
- glutCreateWindow(argv[0]);
- init();
- glutDisplayFunc(display);
- glutKeyboardFunc(keyboard);
- glutTimerFunc(5, update, 0);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement