Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <cmath>
- #include <cstring>
- #include <iostream>
- #include <Grapic.h>
- #define CHMAX 255
- #define MAXPART 255
- #define MAXSPR 255
- using namespace std;
- using namespace grapic;
- const char title[CHMAX] = "Particule";
- const int WINX = 250;
- const int WINY = 250;
- const int DIMW = 500;
- const int DT = 0.001;
- const float G = 9.81;
- struct Vec2
- #pragma region Vector
- {
- float x, y;
- };
- Vec2 make_complex(float x, float y)
- {
- Vec2 c;
- c.x = x;
- c.y = y;
- return c;
- }
- Vec2 make_complex_exp(float r, float theta)
- {
- Vec2 c;
- c.x = r * cos(theta);
- c.y = r * sin(theta);
- return c;
- }
- Vec2 operator+(Vec2 a, Vec2 b)
- {
- Vec2 r;
- r.x = a.x + b.x;
- r.y = a.y + b.y;
- return r;
- }
- Vec2 operator+(float x, Vec2 a)
- {
- Vec2 r;
- r.x = a.x + x;
- r.y = a.y + x;
- return r;
- }
- Vec2 operator-(Vec2 a, Vec2 b)
- {
- Vec2 r;
- r.x = a.x - b.x;
- r.y = a.y - b.y;
- return r;
- }
- Vec2 operator*(float landa, Vec2 b)
- {
- Vec2 r;
- r.x = landa * b.x;
- r.y = landa * b.y;
- return r;
- }
- Vec2 operator*(Vec2 a, Vec2 b)
- {
- Vec2 r;
- r.x = a.x * b.x - a.y * b.y;
- r.y = a.x * b.y + b.x * a.y;
- return r;
- }
- Vec2 operator/(Vec2 a, float landa)
- {
- Vec2 r;
- r.x = a.x / landa;
- r.y = a.y / landa;
- return r;
- }
- #pragma endregion
- struct Particle
- {
- Vec2 d, v, F;
- float m;
- };
- struct World
- {
- int nbPart;
- Particle tab[MAXPART];
- };
- void init_part(Particle& p)
- {
- p.m = 1;
- p.d = make_complex(DIMW / 2, DIMW / 2);
- p.v = make_complex(0, 0);
- p.F = make_complex(0, G * p.m);
- }
- void update_part(Particle& p)
- {
- p.v = p.v + (1 / p.m) * DT * p.F;
- p.d = p.d + DT * p.v;
- if (p.d.y < 0)
- {
- p.d.y = -p.d.y;
- p.v.y = -p.v.y;
- p.v = 0.8 * p.v;
- p.F.y = -p.F.y;
- }
- if (p.d.y > DIMW)
- {
- p.d.y = DIMW - (p.d.y - DIMW);
- p.v.y = -p.v.y;
- p.v = 0.8 * p.v;
- p.F.y = -p.F.y;
- }
- if (p.d.x < 0)
- {
- p.d.x = -p.d.x;
- p.v.x = -p.v.x;
- p.v = 0.8 * p.v;
- p.F.x = -p.F.x;
- }
- if (p.d.x > DIMW)
- {
- p.d.x = DIMW - (p.d.x - DIMW);
- p.v.x = -p.v.x;
- p.v = 0.8 * p.v;
- p.F.x = -p.F.x;
- }
- if (p.F.y < 0)
- p.d.y -= p.F.y / (p.F.y * 10);
- else if (p.F.y > 0)
- p.d.y += p.F.y / (p.F.y * 10);
- else if (p.F.x < 0)
- p.d.x -= p.F.x / (p.F.x * 10);
- else if (p.F.x > 0)
- p.d.x += p.F.x / (p.F.x * 10);
- }
- void init(World& w)
- {
- backgroundColor(0);
- w.nbPart = 0;
- }
- void update(World& w)
- {
- }
- void draw(World& w)
- {
- }
- int main(void)
- {
- bool stop = false;
- winInit(title, WINX, WINY);
- srand(time(NULL));
- World w;
- init(w);
- while (!stop)
- {
- winClear();
- draw(w);
- update(w);
- stop = winDisplay();
- }
- winQuit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement