Advertisement
Jokker115

Planets

Mar 29th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.87 KB | None | 0 0
  1. //planet.h
  2.  
  3. #include <cmath>
  4. #include <GL/gl.h>
  5. #include "vec2d.h"
  6.  
  7. using namespace std;
  8.  
  9. struct planet {
  10.     double r, m, speed, red, green, blue;
  11.     Vec2D pos;
  12.  
  13.     planet () {
  14.         m = 1;
  15.         r = 1;
  16.         speed = 0;
  17.         red = 1;
  18.         green = 1;
  19.         blue = 1;
  20.     }
  21.     planet (Vec2D pos_, Vec2D shift_, double r_, double m_, double speed_) {
  22.         m = m_;
  23.         r = r_;
  24.         pos = pos_ + shift_;
  25.         speed = speed_;
  26.         red = 1;
  27.         green = 1;
  28.         blue = 1;
  29.     }
  30.     void set_color (double r, double g, double b) {
  31.         red = r;
  32.         green = g;
  33.         blue = b;
  34.     }
  35.     void show () {
  36.         double Phi, x, y;
  37.         glColor3d (red, green, blue);
  38.         glBegin(GL_POLYGON);
  39.         for (int i = 0; i < 360; i++) {
  40.             Phi = i * M_PI / 180;
  41.             x = pos.x + r * cos(Phi);
  42.             y = pos.y + r * sin(Phi);
  43.             glVertex2d(x, y);
  44.         }
  45.         glEnd();
  46.     }
  47. };
  48.  
  49.  
  50.  
  51. //Vec2D.h
  52.  
  53. #include <cmath>
  54. #include <GL/gl.h>
  55.  
  56. using namespace std;
  57.  
  58. struct Vec2D {
  59.     double x, y, red, green, blue;
  60.     Vec2D () {
  61.         x = 0;
  62.         y = 0;
  63.         red = 1;
  64.         green = 1;
  65.         blue = 1;
  66.     }
  67.     Vec2D (double x_, double y_) {
  68.         x = x_;
  69.         y = y_;
  70.         red = 1;
  71.         green = 1;
  72.         blue = 1;
  73.     }
  74.     Vec2D (double r) {
  75.         x = r;
  76.         y = r;
  77.         red = 1;
  78.         green = 1;
  79.         blue = 1;
  80.     }
  81.     double len () {
  82.         return sqrt(x*x + y*y);
  83.     }
  84.     void rotate (double alpha) {
  85.         double Phi = atan2(y, x);
  86.         double r = len();
  87.         Phi += alpha;
  88.         x = r * cos(Phi);
  89.         y = r * sin(Phi);
  90.     }
  91.     void rotate_g (double alpha) {
  92.         double Alpha = alpha / 180.0 * M_PI;
  93.         rotate (Alpha);
  94.     }
  95.     Vec2D operator + (Vec2D other) {
  96.         Vec2D ret = Vec2D(x + other.x, y + other.y);
  97.         ret.set_color(0.5 * (red + other.red), 0.5 * (green + other.green), 0.5 * (blue + other.blue));
  98.         return ret;
  99.  
  100.     }
  101.     Vec2D operator - (Vec2D other) {
  102.         Vec2D ret = Vec2D(x -  other.x, y - other.y);
  103.         ret.set_color(0.5 * (red - other.red), 0.5 * (green - other.green), 0.5 * (blue - other.blue));
  104.         return ret;
  105.     }
  106.     double operator * (Vec2D other) {
  107.         return double (x * other.x + y * other.y);
  108.     }
  109.  
  110.     Vec2D operator * (double alpha) {
  111.         return Vec2D(x * alpha, y * alpha);
  112.     }
  113.  
  114.     Vec2D& operator += (Vec2D shift) {
  115.         x += shift.x;
  116.         y += shift.y;
  117.         return *this;
  118.     }
  119.     Vec2D& operator -= (Vec2D shift) {
  120.         x -= shift.x;
  121.         y -= shift.y;
  122.         return *this;
  123.     }
  124.  
  125.     void set_color (double r, double g, double b) {
  126.         red = r;
  127.         green = g;
  128.         blue = b;
  129.     }
  130.     void arrow (Vec2D beta) {
  131.         Vec2D A, B(x,y), C, D, c, d, alpha;
  132.         double num = -0.1;
  133.         alpha = B * num;
  134.         c = alpha;
  135.         c.rotate_g(30);
  136.         d = alpha;
  137.         d.rotate_g(-30);
  138.         C = B + c;
  139.         D = B + d;
  140.         shift_arrow(beta, A, B, C, D);
  141.         show_arrow(A, B, C, D);
  142.     }
  143.     void shift_arrow (Vec2D beta, Vec2D &A, Vec2D &B, Vec2D &C, Vec2D &D) {
  144.         A = A + beta;
  145.         B = B + beta;
  146.         C = C + beta;
  147.         D = D + beta;
  148.     }
  149.     void show_arrow (Vec2D A, Vec2D B, Vec2D C, Vec2D D) {
  150.         glColor3d (red, green, blue);
  151.         glBegin(GL_LINES);
  152.         glVertex2d(A.x, A.y);
  153.         glVertex2d(B.x, B.y);
  154.         glEnd();
  155.         glBegin(GL_POLYGON);
  156.         glVertex2d(B.x, B.y);
  157.         glVertex2d(C.x, C.y);
  158.         glVertex2d(D.x, D.y);
  159.         glEnd();
  160.     }
  161.     void show () {
  162.         glColor3d (red, green, blue);
  163.         glBegin(GL_LINES);
  164.         glVertex2d(0, 0);
  165.         glVertex2d(x, y);
  166.         glEnd();
  167.     }
  168. };
  169.  
  170.  
  171.  
  172. //main.cpp
  173.  
  174. #include <iostream>
  175. #include <GL/glut.h>
  176. #include <GL/gl.h>
  177. #include "vec2d.h"
  178. #include "planet.h"
  179.  
  180. using namespace std;
  181.  
  182. planet sun(Vec2D(0,0), Vec2D(), 3, 10, 0);
  183. planet earth(Vec2D(6,5), Vec2D(), 0.5, 2, 0.5);
  184. planet moon(Vec2D(6,5), Vec2D(0.75,-0.75), 0.125, 0.5, 1);
  185.  
  186. void draw() {
  187.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  188.     glLoadIdentity();
  189.     sun.set_color(0.9, 0.9, 0.0);
  190.     earth.set_color(0.0, 0.3, 0.8);
  191.     moon.set_color(0.9, 0.9, 0.9);
  192.     sun.show();
  193.     earth.show();
  194.     moon.show();
  195.     glutSwapBuffers();
  196. }
  197.  
  198. void update(int t) {
  199.    
  200.     Vec2D es1, es2, es;
  201.     es1 = earth.pos;
  202.     es1.rotate_g(earth.speed);
  203.     es2 = es1;
  204.     es1 = earth.pos;
  205.     es = es2 - es1;
  206.     earth.pos = earth.pos + es;
  207.     moon.pos = moon.pos + es;
  208.  
  209.     Vec2D me;
  210.     me =  moon.pos - earth.pos;
  211.     me.rotate_g(moon.speed);
  212.     moon.pos = earth.pos + me;
  213.  
  214.     glutPostRedisplay();
  215.     glutTimerFunc(50, update, 0);
  216. }
  217.  
  218. void keyb(unsigned char key, int x, int y) {
  219.  
  220.     glutPostRedisplay();
  221. }
  222.  
  223. void reshape(int w, int h) {
  224.  
  225.     // предупредим деление на ноль
  226.     // если окно сильно перетянуто будет
  227.     if(h == 0)
  228.         h = 1;
  229.  
  230.     // используем матрицу проекции
  231.     glMatrixMode(GL_PROJECTION);
  232.  
  233.     // Reset матрицы
  234.     glLoadIdentity();
  235.  
  236.     // определяем окно просмотра
  237.     glViewport(0, 0, w, h);
  238.  
  239.     // установить корректную перспективу.
  240.     glOrtho(-10, 10, -10, 10, -10, 10);
  241.     // вернуться к модели
  242.     glMatrixMode(GL_MODELVIEW);
  243. }
  244.  
  245. int main(int argc, char **argv)
  246. {
  247.     glutInit(&argc, argv);
  248.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  249.     glutInitWindowSize(800, 800);
  250.     glutCreateWindow("GL2D");
  251.     glEnable(GL_DEPTH_TEST);
  252.     glClearColor(0.0, 0.0, 0.0, 0.0);
  253.     glutTimerFunc(50, update, 0);
  254.     glutReshapeFunc(reshape);
  255.     glutDisplayFunc(draw);
  256.     glutKeyboardFunc(keyb);
  257.     glutMainLoop();
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement