Advertisement
marwanpro

Particles Revision

Jan 11th, 2017
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <Grapic.h>
  6. #define CHMAX 255
  7. #define MAXPART 255
  8. #define MAXSPR 255
  9.  
  10. using namespace std;
  11. using namespace grapic;
  12.  
  13. const char title[CHMAX] = "Particule";
  14. const int WINX = 250;
  15. const int WINY = 250;
  16. const int DIMW = 500;
  17. const int DT = 0.001;
  18. const float G = 9.81;
  19.  
  20. struct Vec2
  21. #pragma region Vector
  22. {
  23.     float x, y;
  24. };
  25.  
  26. Vec2 make_complex(float x, float y)
  27. {
  28.     Vec2 c;
  29.     c.x = x;
  30.     c.y = y;
  31.     return c;
  32. }
  33.  
  34. Vec2 make_complex_exp(float r, float theta)
  35. {
  36.     Vec2 c;
  37.     c.x = r * cos(theta);
  38.     c.y = r * sin(theta);
  39.     return c;
  40. }
  41.  
  42. Vec2 operator+(Vec2 a, Vec2 b)
  43. {
  44.     Vec2 r;
  45.     r.x = a.x + b.x;
  46.     r.y = a.y + b.y;
  47.     return r;
  48. }
  49.  
  50. Vec2 operator+(float x, Vec2 a)
  51. {
  52.     Vec2 r;
  53.     r.x = a.x + x;
  54.     r.y = a.y + x;
  55.     return r;
  56. }
  57.  
  58. Vec2 operator-(Vec2 a, Vec2 b)
  59. {
  60.     Vec2 r;
  61.     r.x = a.x - b.x;
  62.     r.y = a.y - b.y;
  63.     return r;
  64. }
  65.  
  66. Vec2 operator*(float landa, Vec2 b)
  67. {
  68.     Vec2 r;
  69.     r.x = landa * b.x;
  70.     r.y = landa * b.y;
  71.     return r;
  72. }
  73.  
  74. Vec2 operator*(Vec2 a, Vec2 b)
  75. {
  76.     Vec2 r;
  77.     r.x = a.x * b.x - a.y * b.y;
  78.     r.y = a.x * b.y + b.x * a.y;
  79.     return r;
  80. }
  81.  
  82. Vec2 operator/(Vec2 a, float landa)
  83. {
  84.     Vec2 r;
  85.     r.x = a.x / landa;
  86.     r.y = a.y / landa;
  87.     return r;
  88. }
  89. #pragma endregion
  90.  
  91.  
  92. struct Particle
  93. {
  94.     Vec2 d, v, F;
  95.     float m;
  96. };
  97.  
  98. struct World
  99. {
  100.     int nbPart;
  101.     Particle tab[MAXPART];
  102. };
  103.  
  104. void init_part(Particle& p)
  105. {
  106.     p.m = 1;
  107.     p.d = make_complex(DIMW / 2, DIMW / 2);
  108.     p.v = make_complex(0, 0);
  109.     p.F = make_complex(0, G * p.m);
  110. }
  111.  
  112.  
  113. void update_part(Particle& p)
  114. {
  115.     p.v = p.v + (1 / p.m) * DT * p.F;
  116.     p.d = p.d + DT * p.v;
  117.     if (p.d.y < 0)
  118.     {
  119.         p.d.y = -p.d.y;
  120.         p.v.y = -p.v.y;
  121.         p.v = 0.8 * p.v;
  122.         p.F.y = -p.F.y;
  123.     }
  124.     if (p.d.y > DIMW)
  125.     {
  126.         p.d.y = DIMW - (p.d.y - DIMW);
  127.         p.v.y = -p.v.y;
  128.         p.v = 0.8 * p.v;
  129.         p.F.y = -p.F.y;
  130.     }
  131.     if (p.d.x < 0)
  132.     {
  133.         p.d.x = -p.d.x;
  134.         p.v.x = -p.v.x;
  135.         p.v = 0.8 * p.v;
  136.         p.F.x = -p.F.x;
  137.     }
  138.     if (p.d.x > DIMW)
  139.     {
  140.         p.d.x = DIMW - (p.d.x - DIMW);
  141.         p.v.x = -p.v.x;
  142.         p.v = 0.8 * p.v;
  143.         p.F.x = -p.F.x;
  144.     }
  145.     if (p.F.y < 0)
  146.         p.d.y -= p.F.y / (p.F.y * 10);
  147.     else if (p.F.y > 0)
  148.         p.d.y += p.F.y / (p.F.y * 10);
  149.     else if (p.F.x < 0)
  150.         p.d.x -= p.F.x / (p.F.x * 10);
  151.     else if (p.F.x > 0)
  152.         p.d.x += p.F.x / (p.F.x * 10);
  153. }
  154.  
  155.  
  156. void init(World& w)
  157. {
  158.     backgroundColor(0);
  159.     w.nbPart = 0;
  160. }
  161.  
  162.  
  163. void update(World& w)
  164. {
  165. }
  166.  
  167. void draw(World& w)
  168. {
  169. }
  170.  
  171. int main(void)
  172. {
  173.     bool stop = false;
  174.     winInit(title, WINX, WINY);
  175.     srand(time(NULL));
  176.     World w;
  177.     init(w);
  178.     while (!stop)
  179.     {
  180.         winClear();
  181.         draw(w);
  182.         update(w);
  183.         stop = winDisplay();
  184.     }
  185.     winQuit();
  186.     return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement