summaryrefslogtreecommitdiff
path: root/animation.hpp
diff options
context:
space:
mode:
authorJaron Kent-Dobias <jaron@kent-dobias.com>2021-09-16 16:47:05 +0200
committerJaron Kent-Dobias <jaron@kent-dobias.com>2021-09-16 16:47:05 +0200
commit496dcbd9960677db246a84bcd3e4b4230ee28e0a (patch)
tree7477598b762a825e7aefae74a6748df7601fca33 /animation.hpp
downloadspheres-496dcbd9960677db246a84bcd3e4b4230ee28e0a.tar.gz
spheres-496dcbd9960677db246a84bcd3e4b4230ee28e0a.tar.bz2
spheres-496dcbd9960677db246a84bcd3e4b4230ee28e0a.zip
Initial commit.
Diffstat (limited to 'animation.hpp')
-rw-r--r--animation.hpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/animation.hpp b/animation.hpp
new file mode 100644
index 0000000..26b3d55
--- /dev/null
+++ b/animation.hpp
@@ -0,0 +1,39 @@
+#pragma once
+
+#include "spheres.hpp"
+#include <GL/glut.h>
+
+void draw(const Sphere<2>& s, std::array<double, 3> color = {0, 0, 0},
+ unsigned nPoints = 20) {
+ glColor3d(color[0], color[1], color[2]);
+ glBegin(GL_POLYGON);
+ for (unsigned i = 0; i < nPoints; i++) {
+ glVertex2d(s.x(0) + s.r * cos(2 * i * M_PI / nPoints), s.x(1) + s.r * sin(2 * i * M_PI / nPoints));
+ }
+ glEnd();
+}
+
+void initializeAnimation(int argc, char** argv, unsigned window_size = 1000) {
+ glutInit(&argc, argv);
+ glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
+ glutInitWindowSize(window_size, window_size);
+ glutCreateWindow("spheres");
+ glClearColor(0.0, 0.0, 0.0, 0.0);
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ gluOrtho2D(0, 1.0, 0, 1.0);
+ glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
+}
+
+template <Particle<2> T> void draw(const Model<2, T>& m) {
+ glClear(GL_COLOR_BUFFER_BIT);
+ for (const Sphere<2>& p : m.particles) {
+ draw(p);
+ if (p.intersectsBoundary()) {
+ for (Vector<2> Δy : {(Vector<2>){1,0}, {-1,0}, {0,1}, {0,-1}, {-1,1}, {1,1}, {1,-1},{-1,-1}}) {
+ draw({(Vector<2>)p.x + Δy, p.r});
+ }
+ }
+ }
+ glFlush();
+}