~cypheon/elfelli

ref: d62f0945c78677b2fe75fe2a71484afb4875f9a2 elfelli/src/Simulation.h -rw-r--r-- 2.6 KiB
d62f0945 — Johann Rudloff Try enabling Travis macOS build env. 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// -*- C++ -*-
/*
 * Simulation.h
 * Copyright (C) 2006  Johann Rudloff
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef _SIMULATION_H_
#define _SIMULATION_H_

#include <vector>
#include <math.h>

const float PI = 3.14159265358979;

namespace Elfelli
{

class Vec2
{
public:
  Vec2();
  Vec2(float x, float y);

  Vec2 operator=(const Vec2& v);
  Vec2 operator+(const Vec2& v);
  Vec2 operator-(const Vec2& v);
  Vec2 operator*(float c);
  Vec2 operator/(float c);

  Vec2 operator-();

  void operator+=(const Vec2& v);
  void operator-=(const Vec2& v);
  void operator*=(float c);
  void operator/=(float c);

  float get_x() const {return x;};
  float get_y() const {return y;};

  void set_x(float x){this->x=x;};
  void set_y(float y){this->y=y;};

  float distance(const Vec2& v) const;
  float length() const {return sqrt(x*x+y*y);};
  Vec2 normalize() const;

private:
  float x, y;

};

struct Body
{
  Vec2 pos;
  float charge;
};

struct PlateBody
{
  Vec2 pos_a;
  Vec2 pos_b;
  float charge;
};

struct Particle
{
  Particle(){n=0;};
  void move(){pos+=vel;};

  Vec2 pos;
  Vec2 vel;

  int n;
  float charge;
};

struct FluxLine
{
  void add(const Vec2& p){points.push_back(p);};
  void clear(){points.clear();};

  std::vector<Vec2> points;
};

class Simulation
{
public:
  Vec2 force_at(const Vec2& pos, float charge);
  void reset(){bodies.clear();plates.clear();result.clear();};

  void add_body(const Vec2& v, float charge);
  void add_plate(const Vec2& a, const Vec2& b, float charge);

  const std::vector<Body>& get_bodies() const{return bodies;};
  const std::vector<PlateBody>& get_plates() const{return plates;};

  Body& operator[](int n){return bodies[n];};
  int n_bodies(){return bodies.size();};

private:
  bool step(Particle& p, float dtime);

protected:
  virtual void run();

  std::vector<Body> bodies;
  std::vector<PlateBody> plates;
  std::vector<FluxLine> result;

};

}

#endif // _SIMULATION_H_