Advertisement
nanobruh

super

May 26th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. #include <SFML/Audio.hpp>
  2. #include <SFML/Graphics.hpp>
  3. #include <SFML/System.hpp>
  4. #include <SFML/Window.hpp>
  5. #include <iostream>
  6. #include <vector>
  7. #include <string>
  8. #include <cmath>
  9. #include <cstdlib>
  10. #include <ctime>
  11.  
  12. using namespace sf;
  13. using namespace std;
  14.  
  15. const double longueur = 1280;
  16. const double hauteur = 720;
  17. const double e = 1601;
  18. const double k = 1;
  19.  
  20.  
  21. RenderWindow window(VideoMode(longueur, hauteur, 32), "chiass");
  22.  
  23. void string_to_char(string s, char* b){
  24. int size = s.size();
  25. for (int x = 0; x < size; x++) {
  26. b[x] = s[x];
  27. }
  28. }
  29.  
  30. class Particule {
  31.  
  32. public:
  33. static int count;
  34. static vector<Particule*> parts;
  35.  
  36. Particule(double x, double y, double radius = 0, double charge = -e, double mass = 9109,double vx = 0, double vy = 0, double ax = 0, double ay = 0) :
  37. q(charge), m(mass), vx(vx), vy(vy), x(x), y(y), ax(ax), ay(ay), radius(radius)
  38. {
  39. count++;
  40. parts.push_back(this);
  41. circle.setRadius(radius);
  42. circle.setPointCount(500);
  43. circle.setFillColor(Color(255, 99, 71));
  44. circle.setOrigin(radius, radius);
  45. circle.setPosition(x, y);
  46. }
  47.  
  48. void SetRadius(double radius) {
  49. circle.setRadius(radius);
  50. }
  51.  
  52. void PrintInfo() {
  53. cout << "Mass = " << (*this).m << endl;
  54. cout << "Charge = " << (*this).q << endl;
  55. cout << "X position = " << (*this).x << endl;
  56. cout << "Y position = " << (*this).y << endl;
  57. cout << "Vx = " << (*this).vx << endl;
  58. cout << "Vy = " << (*this).vy << endl;
  59. cout << "Ax = " << (*this).ax << endl;
  60. cout << "Ay = " << (*this).ay << endl;
  61. }
  62.  
  63. int GetRadius() {
  64. return radius;
  65. }
  66.  
  67. void SetPosition(int x, int y) {
  68. this->x = x;
  69. this->y = y;
  70. }
  71.  
  72. void setcolor(int r, int g, int b) {
  73. circle.setFillColor(Color(r, g, b));
  74. }
  75.  
  76. vector<double> infos() {
  77. vector <double> infos = {x, y, q, m, vx, vy, ax, ay};
  78. return infos;
  79. };
  80.  
  81. template <class T>
  82. void SetAcceleration(T ax, T ay) {
  83. (*this).ax = ax;
  84. (*this).ay = ay;
  85. }
  86.  
  87. template <class T>
  88. void SetVitesse(T vx, T vy) {
  89. (*this).vx = vx;
  90. (*this).vy = vy;
  91. }
  92.  
  93. void changeV() {
  94. vx += ax;
  95. vy += ay;
  96. }
  97.  
  98. void changeXY() {
  99. x += vx;
  100. y += vy;
  101.  
  102. }
  103.  
  104. void update() {
  105.  
  106. if (radius == 0)
  107. return;
  108.  
  109. for (int i = 0; i < Particule::count; i++) {
  110. if (Particule::parts[i] != this) {
  111. Particule* other = parts[i];
  112. this->coulomb(other);
  113. }
  114. }
  115.  
  116. changeV();
  117.  
  118. if (vx > 5) {
  119. vx = 5;
  120. }
  121. if (vy > 5) {
  122. vy = 5;
  123. }
  124.  
  125. changeXY();
  126.  
  127. if(x - radius<0) {
  128. x = radius;
  129. ax *= -1;
  130. vx *= -1;
  131. }
  132. if (x +radius> longueur) {
  133. x = longueur- radius;
  134. ax *= -1;
  135. vx *= -1;
  136. }
  137. if (y - radius< 0) {
  138. y = radius;
  139. ay *= -1;
  140. vy *= -1;
  141. }
  142. if (y + radius > hauteur) {
  143. y = hauteur - radius;
  144. ay *= -1;
  145. vy *= -1;
  146. }
  147.  
  148. circle.setPosition(Vector2f(x, y));
  149. window.draw(circle);
  150. }
  151.  
  152. void text() {
  153. Font font;
  154. if (!font.loadFromFile("arial.ttf"))
  155. cout << "Merde";
  156. vector<double> faichier = (*this).infos();
  157.  
  158. int x = faichier[0];
  159. char xs[12];
  160. sprintf_s(xs, "%d", x);
  161. Text X(xs, font, 20);
  162.  
  163. int y = faichier[1];
  164. char ys[12];
  165. sprintf_s(ys, "%d", y);
  166. Text Y(ys, font, 20);
  167. Y.setPosition(50, 0);
  168.  
  169. window.draw(X);
  170. window.draw(Y);
  171.  
  172.  
  173.  
  174. }
  175.  
  176.  
  177. tuple<double, double> GetAcceleration() {
  178. return make_tuple(ax, ay);
  179. }
  180.  
  181. double distance(Particule *other) {
  182. return sqrt((other->x - (*this).x) * (other->x - (*this).x) + (other->y - (*this).y) * (other->y - (*this).y));
  183. }
  184.  
  185. void coulomb(Particule *other) {
  186.  
  187. double ax1;
  188. double ay1;
  189.  
  190. ax1 = -fabs(other->x - x)*k/ (this->distance(other) * this->distance(other));
  191. ay1 = -fabs(other->y - y)*k/ (this->distance(other) * this->distance(other));
  192.  
  193. this->SetAcceleration(ax1,ay1);
  194. other->SetAcceleration(-ax1,-ay1);
  195.  
  196. }
  197.  
  198. private:
  199. double q;
  200. double m;
  201. double vx;
  202. double vy;
  203. double x;
  204. double y;
  205. double ax;
  206. double ay;
  207. CircleShape circle;
  208. int radius;
  209. };
  210.  
  211.  
  212.  
  213. int Particule::count = 0;
  214. vector <Particule*> Particule::parts;
  215.  
  216. void updates(int nofparts = Particule::count) {
  217. for (int x = 0; x < nofparts; x++) {
  218. Particule::parts[x]->update();
  219. }
  220. }
  221.  
  222. int main()
  223. {
  224. Particule electron1(2*longueur/3, hauteur/2, 20);
  225. Particule electron2(longueur/3, hauteur/2, 20);
  226.  
  227. for (int i = 0; i < 5; i++) {
  228. srand(time(0));
  229. new Particule(rand() % 1280, rand() % 720);
  230. }
  231.  
  232. cout << Particule::count;
  233.  
  234. ContextSettings settings;
  235. settings.antialiasingLevel = 32;
  236.  
  237. /*
  238. electron1.SetVitesse(1, 1);
  239. electron2.SetVitesse(-1, -1);
  240. electron3.SetVitesse(1, 1);
  241.  
  242. */
  243.  
  244. electron1.setcolor(255, 255, 255);
  245. Font font;
  246. if (!font.loadFromFile("arial.ttf"))
  247. return EXIT_FAILURE;
  248.  
  249. while (window.isOpen())
  250. {
  251. Event event;
  252. Clock clock;
  253. Text infos;
  254. while (window.pollEvent(event))
  255. {
  256. if (event.type == Event::Closed)
  257. window.close();
  258. if (event.type == Event::MouseButtonPressed)
  259. {
  260. if (event.mouseButton.button == Mouse::Left)
  261. {
  262. cout << "Merde" << endl;
  263. for (int j = 0; j < Particule::count; j++) {
  264. if ((Particule::parts[j])->GetRadius() == 0) {
  265. Particule::parts[j]->SetRadius(20);
  266. Particule::parts[j]->SetPosition(event.mouseButton.x, event.mouseButton.y);
  267. break;
  268. }
  269. }
  270. }
  271. }
  272. }
  273. window.clear(Color(40, 40, 40));
  274. updates();
  275. electron1.text();
  276. window.display();
  277. }
  278. return EXIT_SUCCESS;
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement