Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Audio.hpp>
- #include <SFML/Graphics.hpp>
- #include <SFML/System.hpp>
- #include <SFML/Window.hpp>
- #include <iostream>
- #include <vector>
- #include <string>
- #include <cmath>
- #include <cstdlib>
- #include <ctime>
- using namespace sf;
- using namespace std;
- const double longueur = 1280;
- const double hauteur = 720;
- const double e = 1601;
- const double k = 1;
- RenderWindow window(VideoMode(longueur, hauteur, 32), "chiass");
- void string_to_char(string s, char* b){
- int size = s.size();
- for (int x = 0; x < size; x++) {
- b[x] = s[x];
- }
- }
- class Particule {
- public:
- static int count;
- static vector<Particule*> parts;
- 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) :
- q(charge), m(mass), vx(vx), vy(vy), x(x), y(y), ax(ax), ay(ay), radius(radius)
- {
- count++;
- parts.push_back(this);
- circle.setRadius(radius);
- circle.setPointCount(500);
- circle.setFillColor(Color(255, 99, 71));
- circle.setOrigin(radius, radius);
- circle.setPosition(x, y);
- }
- void SetRadius(double radius) {
- circle.setRadius(radius);
- }
- void PrintInfo() {
- cout << "Mass = " << (*this).m << endl;
- cout << "Charge = " << (*this).q << endl;
- cout << "X position = " << (*this).x << endl;
- cout << "Y position = " << (*this).y << endl;
- cout << "Vx = " << (*this).vx << endl;
- cout << "Vy = " << (*this).vy << endl;
- cout << "Ax = " << (*this).ax << endl;
- cout << "Ay = " << (*this).ay << endl;
- }
- int GetRadius() {
- return radius;
- }
- void SetPosition(int x, int y) {
- this->x = x;
- this->y = y;
- }
- void setcolor(int r, int g, int b) {
- circle.setFillColor(Color(r, g, b));
- }
- vector<double> infos() {
- vector <double> infos = {x, y, q, m, vx, vy, ax, ay};
- return infos;
- };
- template <class T>
- void SetAcceleration(T ax, T ay) {
- (*this).ax = ax;
- (*this).ay = ay;
- }
- template <class T>
- void SetVitesse(T vx, T vy) {
- (*this).vx = vx;
- (*this).vy = vy;
- }
- void changeV() {
- vx += ax;
- vy += ay;
- }
- void changeXY() {
- x += vx;
- y += vy;
- }
- void update() {
- if (radius == 0)
- return;
- for (int i = 0; i < Particule::count; i++) {
- if (Particule::parts[i] != this) {
- Particule* other = parts[i];
- this->coulomb(other);
- }
- }
- changeV();
- if (vx > 5) {
- vx = 5;
- }
- if (vy > 5) {
- vy = 5;
- }
- changeXY();
- if(x - radius<0) {
- x = radius;
- ax *= -1;
- vx *= -1;
- }
- if (x +radius> longueur) {
- x = longueur- radius;
- ax *= -1;
- vx *= -1;
- }
- if (y - radius< 0) {
- y = radius;
- ay *= -1;
- vy *= -1;
- }
- if (y + radius > hauteur) {
- y = hauteur - radius;
- ay *= -1;
- vy *= -1;
- }
- circle.setPosition(Vector2f(x, y));
- window.draw(circle);
- }
- void text() {
- Font font;
- if (!font.loadFromFile("arial.ttf"))
- cout << "Merde";
- vector<double> faichier = (*this).infos();
- int x = faichier[0];
- char xs[12];
- sprintf_s(xs, "%d", x);
- Text X(xs, font, 20);
- int y = faichier[1];
- char ys[12];
- sprintf_s(ys, "%d", y);
- Text Y(ys, font, 20);
- Y.setPosition(50, 0);
- window.draw(X);
- window.draw(Y);
- }
- tuple<double, double> GetAcceleration() {
- return make_tuple(ax, ay);
- }
- double distance(Particule *other) {
- return sqrt((other->x - (*this).x) * (other->x - (*this).x) + (other->y - (*this).y) * (other->y - (*this).y));
- }
- void coulomb(Particule *other) {
- double ax1;
- double ay1;
- ax1 = -fabs(other->x - x)*k/ (this->distance(other) * this->distance(other));
- ay1 = -fabs(other->y - y)*k/ (this->distance(other) * this->distance(other));
- this->SetAcceleration(ax1,ay1);
- other->SetAcceleration(-ax1,-ay1);
- }
- private:
- double q;
- double m;
- double vx;
- double vy;
- double x;
- double y;
- double ax;
- double ay;
- CircleShape circle;
- int radius;
- };
- int Particule::count = 0;
- vector <Particule*> Particule::parts;
- void updates(int nofparts = Particule::count) {
- for (int x = 0; x < nofparts; x++) {
- Particule::parts[x]->update();
- }
- }
- int main()
- {
- Particule electron1(2*longueur/3, hauteur/2, 20);
- Particule electron2(longueur/3, hauteur/2, 20);
- for (int i = 0; i < 5; i++) {
- srand(time(0));
- new Particule(rand() % 1280, rand() % 720);
- }
- cout << Particule::count;
- ContextSettings settings;
- settings.antialiasingLevel = 32;
- /*
- electron1.SetVitesse(1, 1);
- electron2.SetVitesse(-1, -1);
- electron3.SetVitesse(1, 1);
- */
- electron1.setcolor(255, 255, 255);
- Font font;
- if (!font.loadFromFile("arial.ttf"))
- return EXIT_FAILURE;
- while (window.isOpen())
- {
- Event event;
- Clock clock;
- Text infos;
- while (window.pollEvent(event))
- {
- if (event.type == Event::Closed)
- window.close();
- if (event.type == Event::MouseButtonPressed)
- {
- if (event.mouseButton.button == Mouse::Left)
- {
- cout << "Merde" << endl;
- for (int j = 0; j < Particule::count; j++) {
- if ((Particule::parts[j])->GetRadius() == 0) {
- Particule::parts[j]->SetRadius(20);
- Particule::parts[j]->SetPosition(event.mouseButton.x, event.mouseButton.y);
- break;
- }
- }
- }
- }
- }
- window.clear(Color(40, 40, 40));
- updates();
- electron1.text();
- window.display();
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement