Advertisement
High_Light

часики

Apr 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <GL/glut.h>
  7. #include <GL/gl.h>
  8. #include <GL/glu.h>
  9.  
  10. using namespace std;
  11.  
  12. double scale = 0.5;
  13. double pi = 3.1415926;
  14.  
  15. /*int avg(vector<float> a){
  16.     float res = 0;
  17.     for (int i = 0; i < a.size(); i++)
  18.         res += a[i];
  19.     return (res / a.size());
  20. }*/
  21.  
  22. float update_time = 20;
  23. float angle_step = M_PI / 30.0 / 1000.0 * update_time;
  24.  
  25. class vec2d {
  26. public:
  27.     float x;
  28.     float y;
  29.     vec2d() : x(0), y(0) {}
  30.     vec2d(float a,float b) : x(a), y(b){}
  31.     void print(){
  32.         cout << x << "\t" << y << endl;
  33.     }
  34.     float dot(vec2d other){
  35.         return (x * other.x + y * other.y);
  36.     }
  37.     vec2d operator + (vec2d other){
  38.         return vec2d(x + other.x, y + other.y);
  39.     }
  40.     vec2d operator - (vec2d other){
  41.         return vec2d(x - other.x, y - other.y);
  42.     }
  43.     vec2d operator * (float a){
  44.         return vec2d(x * a, y * a);
  45.     }
  46.     vec2d operator / (float a){
  47.         return vec2d(x / a, y / a);
  48.     }
  49. };
  50.  
  51. void render_square(double x, double y) {
  52.     glPushMatrix();
  53.  
  54.     double dx = 0.5;
  55.     double dy = 0.5;
  56.     glBegin(GL_POLYGON);
  57.     glVertex2f(x, y);
  58.     glVertex2f(x + dx, y);
  59.     glVertex2f(x + dx, y - dy);
  60.     glVertex2f(x, y - dy);
  61.     glEnd();
  62.     glPopMatrix();
  63. }
  64.  
  65. void draw_line(double x, double y, double x1, double y1) {
  66.     glBegin(GL_LINES);
  67.     glVertex2d(x, y);
  68.     glVertex2d(x1, y1);
  69.     glEnd();
  70. }
  71. void draw_arrow(float x, float y, vec2d a){
  72.     draw_line(x, y, a.x, a.y);
  73.     vec2d n1(a.y * 0.3, -a.x * 0.3);
  74.     vec2d n2(-a.y * 0.3, a.x * 0.3);
  75.     vec2d z(-a.x, -a.y);
  76.     vec2d zn1 = z + n1;
  77.     vec2d zn2 = z + n2;
  78.     draw_line(a.x, a.y, zn1.x / 6 + a.x, zn1.y / 6 + a.y);
  79.     draw_line(a.x, a.y, zn2.x / 6 + a.x, zn2.y / 6 + a.y);
  80. }
  81. class Circle{
  82. public:
  83.     float x, y, radius, vx, vy;
  84.     Circle(float cx, float cy, float vx, float vy, float r) : x(cx), y(cy), radius(r), vx(vx), vy(vy) {}
  85.     void draw(){
  86.         float step = 0.04;
  87.         for (float f = 0; f <= M_PI * 2; f += step){
  88.             draw_line(x + radius * cos(f), y + radius * sin(f), x + radius * cos(f+step), y + radius * sin(f+step));
  89.         }
  90.     }
  91.     void draw_sticks(){
  92.         static float cur_angle = -M_PI * 0.5;
  93.         for (float i = 0; i < 2 * M_PI; i += M_PI / 30){
  94.             if (i % 5 * (M_PI / 30) == 0)
  95.                 draw_line(cos(cur_angle), sin(cur_angle, cos(cur_angle) * 0.8, sin(cur_angle) * 0.8));
  96.             else
  97.         }
  98.        
  99.     }
  100. };
  101. Circle c(0, 0, 0, 0, 3);
  102.  
  103. void Render() {
  104.         static float cur_angle = -M_PI * 0.5;
  105.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  106.         glLoadIdentity();
  107.         glTranslatef(0, 0, -10);
  108.         glColor3d(1, 1, 1);
  109.         glScalef(scale, scale, scale);
  110.         vec2d arrow(3 * cos(-cur_angle),3 * sin(-cur_angle));
  111.         draw_arrow(0, 0, arrow);
  112.         cur_angle += angle_step;
  113.         if (cur_angle > 2 * M_PI) cur_angle -= 2 * M_PI;
  114.         c.draw();
  115.         glFlush();
  116. }
  117.  
  118. void update(int t) {
  119.     Render();
  120.     glutTimerFunc(update_time, update, 0);
  121. }
  122.  
  123. void keyb(unsigned char key, int x, int y) {
  124.     if(key == '+') {
  125.         scale *= 1.1;
  126.     }
  127.     if(key == '-') {
  128.         scale *= 0.9;
  129.     }
  130.  
  131.     Render();
  132. }
  133.  
  134. void reshape(int w, int h) {
  135.  
  136.     // предупредим деление на ноль
  137.     // если окно сильно перетянуто будет
  138.     if(h == 0)
  139.         h = 1;
  140.     float ratio = 1.0* w / h;
  141.  
  142.     // используем матрицу проекции
  143.     glMatrixMode(GL_PROJECTION);
  144.  
  145.         // Reset матрицы
  146.     glLoadIdentity();
  147.  
  148.     // определяем окно просмотра
  149.     glViewport(0, 0, w, h);
  150.  
  151.     // установить корректную перспективу.
  152.     gluPerspective(45,ratio,1,1000);
  153.  
  154.     // вернуться к модели
  155.     glMatrixMode(GL_MODELVIEW);
  156. }
  157.  
  158. int main(int argc, char **argv)
  159. {
  160.     glutInit(&argc, argv);
  161.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  162.     glutInitWindowSize(800, 600);
  163.     glutCreateWindow("Physics Engine");
  164.     glEnable(GL_DEPTH_TEST);
  165.     glMatrixMode(GL_PROJECTION);
  166.     glLoadIdentity();
  167.     glMatrixMode(GL_MODELVIEW);
  168.     glLoadIdentity();
  169.     glClearColor(0., 0.4, 0.0, 0.0);
  170.  
  171.     glutTimerFunc(50, update, 0);
  172.     glutReshapeFunc(reshape);
  173.     glutDisplayFunc(Render);
  174.     glutKeyboardFunc(keyb);
  175.     glutMainLoop();
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement