parent
1aa7bf1713
commit
5c45de0785
10 changed files with 178 additions and 11 deletions
@ -1,7 +1,22 @@ |
|||||||
#include <QOpenGLWidget> |
#include <QOpenGLWidget> |
||||||
#include <QOpenGLFunctions> |
#include <QOpenGLFunctions> |
||||||
|
#include <chrono> |
||||||
|
|
||||||
|
using namespace std::chrono; |
||||||
|
|
||||||
|
class Simulation; |
||||||
|
|
||||||
class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions { |
class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions { |
||||||
|
public: |
||||||
|
explicit GLWidget(Simulation *); |
||||||
protected: |
protected: |
||||||
void paintEvent(QPaintEvent* e) override; |
void paintEvent(QPaintEvent* e) override; |
||||||
|
void timerEvent(QTimerEvent* e) override; |
||||||
|
private: |
||||||
|
Simulation * simulation; |
||||||
|
time_point<system_clock> previousFrame; |
||||||
|
int currentFPS; |
||||||
|
void updateFPS(); |
||||||
|
|
||||||
|
static bool AnyDialogOpen(); |
||||||
}; |
}; |
@ -1 +1,34 @@ |
|||||||
#include "Pendulum.h" |
#include "Pendulum.h" |
||||||
|
#include <iostream> |
||||||
|
#include <QPainter> |
||||||
|
|
||||||
|
Pendulum::Pendulum(const std::vector<double> &M, |
||||||
|
const std::vector<double> &L, |
||||||
|
QColor color, double startAngle) : M(M), L(L), color(color){ |
||||||
|
|
||||||
|
startAngle *= 3.141 / 180; |
||||||
|
|
||||||
|
Vector direction(-sin(startAngle), cos(startAngle)); |
||||||
|
Vector currentPosition(0, 0); |
||||||
|
|
||||||
|
for (int i = 0; i < M.size(); i++){ |
||||||
|
currentPosition = currentPosition + direction * L[i]; |
||||||
|
X.push_back(currentPosition); |
||||||
|
V.emplace_back(0, 0); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void Pendulum::draw(QPainter *p, double scale) const { |
||||||
|
p->setPen(color); |
||||||
|
p->setBrush(Qt::white); |
||||||
|
Vector previousX(0, 0); |
||||||
|
|
||||||
|
for (int i = 0; i < X.size(); i++){ |
||||||
|
Vector x = X[i] * scale; |
||||||
|
p->drawLine(previousX.x, previousX.y, x.x, x.y); |
||||||
|
previousX = x; |
||||||
|
|
||||||
|
double r = sqrt(M[i] * scale / 5); |
||||||
|
p->drawEllipse(QPointF(x.x, x.y), r * 2, r * 2); |
||||||
|
} |
||||||
|
} |
||||||
|
@ -1,3 +1,20 @@ |
|||||||
|
#include <vector> |
||||||
|
#include <QVector2D> |
||||||
|
#include <QColor> |
||||||
|
#include "Vector.h" |
||||||
|
|
||||||
|
class QPainter; |
||||||
|
|
||||||
class Pendulum { |
class Pendulum { |
||||||
|
public: |
||||||
|
explicit Pendulum(const std::vector<double> &M, |
||||||
|
const std::vector<double> &L, |
||||||
|
QColor color, double startAngle); |
||||||
|
|
||||||
|
void draw(QPainter*, double) const; |
||||||
|
|
||||||
|
private: |
||||||
|
std::vector<Vector> X, V; |
||||||
|
std::vector<double> M, L; |
||||||
|
QColor color; |
||||||
}; |
}; |
@ -1,3 +1,12 @@ |
|||||||
#include "Simulation.h" |
#include "Simulation.h" |
||||||
|
#include "Pendulum.h" |
||||||
|
#include <QPainter> |
||||||
|
|
||||||
Simulation::Simulation() = default; |
Simulation::Simulation() = default; |
||||||
|
|
||||||
|
void Simulation::draw(QPainter *p, int screenSize) const { |
||||||
|
double scale = screenSize / size; |
||||||
|
|
||||||
|
for (const auto pendulum : pendula) |
||||||
|
pendulum->draw(p, scale); |
||||||
|
} |
||||||
|
@ -0,0 +1,31 @@ |
|||||||
|
#include "Vector.h" |
||||||
|
#include <cmath> |
||||||
|
|
||||||
|
Vector::Vector(double x, double y) : x(x), y(y) {} |
||||||
|
|
||||||
|
double Vector::length() const { |
||||||
|
return sqrt(x * x + y * y); |
||||||
|
} |
||||||
|
|
||||||
|
void Vector::normalize() { |
||||||
|
double l = length(); |
||||||
|
x /= l; |
||||||
|
y /= l; |
||||||
|
} |
||||||
|
|
||||||
|
Vector operator+(Vector lhs, Vector rhs) { |
||||||
|
return {lhs.x + rhs.x, lhs.y + rhs.y}; |
||||||
|
} |
||||||
|
|
||||||
|
Vector operator-(Vector lhs, Vector rhs) { |
||||||
|
return {lhs.x - rhs.x, lhs.y - rhs.y}; |
||||||
|
} |
||||||
|
|
||||||
|
Vector operator*(Vector v, double factor){ |
||||||
|
return {v.x * factor, v.y * factor}; |
||||||
|
} |
||||||
|
|
||||||
|
Vector operator/(Vector v, double divisor){ |
||||||
|
return {v.x / divisor, v.y / divisor}; |
||||||
|
} |
||||||
|
|
@ -0,0 +1,12 @@ |
|||||||
|
struct Vector { |
||||||
|
double x, y; |
||||||
|
Vector(double x, double y); |
||||||
|
|
||||||
|
double length() const; |
||||||
|
void normalize(); |
||||||
|
|
||||||
|
friend Vector operator +(Vector lhs, Vector rhs); |
||||||
|
friend Vector operator -(Vector lhs, Vector rhs); |
||||||
|
friend Vector operator *(Vector v, double factor); |
||||||
|
friend Vector operator /(Vector v, double divisor); |
||||||
|
}; |
Loading…
Reference in new issue