Advertisement
High_Light

csx

May 8th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <cmath>
  4. #include <algorithm>
  5.  
  6. #include <GL/glut.h>
  7. #include <GL/gl.h>
  8. #include <GL/glu.h>
  9.  
  10. using namespace std;
  11. int msec = 10;
  12. double scale = 1.0;
  13. double vx=0,vy=0;
  14. double p=3.14,r_1=0;
  15. double kx=5,ky=5;
  16. double angle_step = p/30/100.0 * msec;
  17.  
  18. void render_square(double x, double y) {
  19.     glPushMatrix();
  20.  
  21.     double dx = 0.5;
  22.     double dy = 0.5;
  23.     glBegin(GL_POLYGON);
  24.     glVertex2f(x, y);
  25.     glVertex2f(x + dx, y);
  26.     glVertex2f(x + dx, y - dy);
  27.     glVertex2f(x, y - dy);
  28.     glEnd();
  29.     glPopMatrix();
  30. }
  31.  
  32. void draw_line(double x, double y, double x1, double y1) {
  33.     glBegin(GL_LINES);
  34.     glVertex2d(x, y);
  35.     glVertex2d(x1, y1);
  36.     glEnd();
  37. }
  38. class circle{
  39. public:
  40.     double x,y,r,vx,vy;
  41.     circle(double x_, double y_, double r_, double vx_, double vy_):r(r_), x(x_), y(y_){
  42.         vx = vx_ / 1000.0 * msec;
  43.         vy = vy_ / 1000.0 * msec;
  44.     }
  45.     void print_circle(){
  46.         int n=40;
  47.         double cx=x + r;
  48.         double cy=y;
  49.         double step = p/n;
  50.         for (float f=step; f<=p*2;f+=step){
  51.             draw_line(cx, cy ,cos(f)*r+x ,sin(f)*r+y);
  52.              cx=x + cos(f)*r;
  53.              cy=y + sin(f)*r;
  54.         }
  55.     }
  56.  
  57.     void update(){
  58.         if(x+r>kx || x-r<-kx){
  59.             vx*=-1;
  60.         }
  61.         if(y+r>ky || y-r<-ky){
  62.             vy*=-1;
  63.         }
  64.         x+=vx;
  65.         y+=vy;
  66.     }
  67. };
  68. class Vector{
  69. public:
  70.     double x,y;
  71.     Vector(float a,float b):x(a),y(b){
  72.     }
  73.     Vector operator +(Vector other){
  74.         return Vector(x+other.x, y+other.y);
  75.     }
  76.     Vector operator -(Vector other){
  77.         return Vector(x-other.x, y-other.y);
  78.     }
  79.     Vector operator *(float a){
  80.         return Vector(x*a,y*a);
  81.     }
  82.     Vector operator /(float a){
  83.         return Vector(x/a,y/a);
  84.     }
  85.     float dot (Vector other){
  86.         return x*other.x+y*other.y;
  87.     }
  88.     void print(){
  89.         cout<<'('<<x<<','<<y<<')'<<endl;
  90.     }
  91.     void draw(){
  92.         static double d = 6.0;
  93.         Vector n1(-y, x);
  94.         Vector n2(y, -x);
  95.         Vector a(x,y);
  96.         Vector ar_1 = (n1 - a*2)/d;
  97.         Vector ar_2 = (n2 - a*2)/d;
  98.         draw_line(0,0,x,y);
  99.         draw_line(x, y, ar_1.x + x, ar_1.y + y);
  100.         draw_line(x, y, ar_2.x + x, ar_2.y + y);
  101.     }
  102. };
  103. class Liniya{
  104. public:
  105.     double x,y;
  106.     Liniya(float a,float b):x(a),y(b){
  107.     }
  108.     Liniya operator +(Liniya other){
  109.         return Liniya(x+other.x, y+other.y);
  110.     }
  111.     Liniya operator -(Liniya other){
  112.         return Liniya(x-other.x, y-other.y);
  113.     }
  114.     Liniya operator *(float a){
  115.         return Liniya (x*a,y*a);
  116.     }
  117.     Liniya operator /(float a){
  118.         return Liniya (x/a,y/a);
  119.     }
  120.     float dot (Liniya  other){
  121.         return x*other.x+y*other.y;
  122.     }
  123.     void print(){
  124.         cout<<'('<<x<<','<<y<<')'<<endl;
  125.     }
  126.     void draw(){
  127.         static double d = 6.0;
  128.         Liniya  n1(-y, x);
  129.         Liniya  n2(y, -x);
  130.         Liniya  a(x,y);
  131.         //Liniya  ar_1 = (n1 - a*2)/d;
  132.         //Liniya  ar_2 = (n2 - a*2)/d;
  133.         draw_line(0,0,x,y);
  134.         //draw_line(x, y, ar_1.x + x, ar_1.y + y);
  135.         //draw_line(x, y, ar_2.x + x, ar_2.y + y);
  136.     }
  137. };
  138. Liniya c(2,2);
  139. Vector b(2,2);
  140. circle a(0, 0, 0.4, 20, 40);
  141. circle n(0, 0, 1, 0, 0);
  142. void draw_clocks(float stop_angle){
  143.     int j = 0;
  144.     float coef = 0.8;
  145.     for(float  angle = 0; angle <  2 * p; angle += p/30.0, j++){
  146.         float x = cos(angle);
  147.         float y = sin(angle);
  148.         if(j % 5 == 0) coef = 0.6;
  149.         else coef = 0.8;
  150.         draw_line(x, y, x * coef, y * coef);
  151.     }
  152. }
  153.  
  154. void Render() {
  155.         static double cur_angle = 1.5*p;
  156.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  157.         glLoadIdentity();
  158.         glTranslatef(0, 0, -10);
  159.         glColor3d(1, 1, 1);
  160.         glScalef(scale, scale, scale);
  161.         a.print_circle();
  162.         draw_clocks(cur_angle);
  163.         Vector b(cos(-cur_angle),sin(-cur_angle));
  164.        // Liniya c()
  165.         cur_angle += angle_step;
  166.         if (cur_angle - 1.5 * p > 2 * p) cur_angle -= 2*p;
  167.         draw_line(-kx,-ky,kx,-ky);
  168.         draw_line(kx,-ky,kx,ky);
  169.         draw_line(kx,ky,-kx,ky);
  170.         draw_line(-kx,ky,-kx,-ky);
  171.         b.draw();
  172.         n.print_circle();
  173.         a.update();
  174.         glFlush();
  175. }
  176.  
  177. void update(int t) {
  178.     Render();
  179.     glutTimerFunc(msec, update, 0);
  180. }
  181.  
  182. void keyb(unsigned char key, int x, int y) {
  183.     if(key == '+') {
  184.         scale *= 1.1;
  185.     }
  186.     if(key == '-') {
  187.         scale *= 0.9;
  188.     }
  189.     if(key == 'w') {
  190.         kx *= 1.1;
  191.     }
  192.     Render();
  193. }
  194.  
  195. void reshape(int w, int h) {
  196.  
  197.     // предупредим деление на ноль
  198.     // если окно сильно перетянуто будет
  199.     if(h == 0)
  200.         h = 1;
  201.     float ratio = 1.0* w / h;
  202.  
  203.     // используем матрицу проекции
  204.     glMatrixMode(GL_PROJECTION);
  205.  
  206.         // Reset матрицы
  207.     glLoadIdentity();
  208.  
  209.     // определяем окно просмотра
  210.     glViewport(0, 0, w, h);
  211.  
  212.     // установить корректную перспективу.
  213.     gluPerspective(45,ratio,1,1000);
  214.  
  215.     // вернуться к модели
  216.     glMatrixMode(GL_MODELVIEW);
  217. }
  218.  
  219. int main(int argc, char **argv)
  220. {
  221.     glutInit(&argc, argv);
  222.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  223.     glutInitWindowSize(800, 600);
  224.     glutCreateWindow("Physics Engine");
  225.     glEnable(GL_DEPTH_TEST);
  226.     glMatrixMode(GL_PROJECTION);
  227.     glLoadIdentity();
  228.     glMatrixMode(GL_MODELVIEW);
  229.     glLoadIdentity();
  230.     glClearColor(0, 0, 0.0, 0.0);
  231.  
  232.     glutTimerFunc(50, update, 0);
  233.     glutReshapeFunc(reshape);
  234.     glutDisplayFunc(Render);
  235.     glutKeyboardFunc(keyb);
  236.     glutMainLoop();
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement