C++: Simple Pendulum Animation on Ubuntu Machine

Here is the code to animate simple pendulum on Ubuntu Machine.

We have to add #include <graphics.h> header file.

If you want to install C/C++ graphic.h header file on Ubuntu you can visit this post. How to Install graphics.h C/C++ library on Ubuntu

We have to also write int gd = DETECT, gm; initgraph(&gd, &gm, NULL);

initgraph() initializes the graphics system by loading a graphics driver from disk (or validating a registered driver), and putting the system into graphics mode.

Equations used:-

Angular acceleration of the pendulum:

1
2
3
4
θ'' = − g⁄R sin <br/>
    θ = angle of pendulum ( 0= vertical) <br/>
    R = length of rod <br/>
    g = gravitational constant <br/>

Angular displacement :

1
2
θ(t) = θ0cos(2π/T * t + φ)<br/>
φ is a constant value

Period of oscillation:

1
T ≈ 2π√(R/g)

C++ Implementation

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
#include <iostream>
#include <cmath>
#include <graphics.h>

const float Pi = 3.1415926535897931f;

float angular_disp(float t, float angle, float T) //angular displacement
{
    //Simple Harmonic Motion equation
    float res = angle * cos((2*Pi / T) * t); //let additional const be 0
    return res;
}

void animate_pendulum(float x1, float y1,
                      float l, float g, float angle, float T)
{
   int gd = DETECT, gmAngular displacement :


    θ(t) = θ0cos(2π/T * t + φ)


    φ is a constant value


Period of oscillation:

    T  2π√(R/g);
   initgraph(&gd, &gm, NULL);

  float angular_acc;//angular acceleration

  for(float t = 1.0; t < 100.0;)
  {
     float new_angle = angular_disp(t, angle, T);
     angular_acc = -(g/l) * sin(new_angle);

     //To know other end coordinates
     float base = l * cos(new_angle);
     float height = l * sin(new_angle);

     //end coordinates
     float y2 = y1 + base;
     float x2 = x1 + height;

     setlinestyle(DASHED_LINE,0,NORM_WIDTH);
     setcolor(WHITE);
     line(x1, y1-20, x1, y1+250);

     setlinestyle(SOLID_LINE,0,THICK_WIDTH);
     setcolor(BLUE);
     line(x1, y1, x2, y2);

     setcolor(RED);
     circle(x2, y2, 10);
     floodfill(x2, y2, RED);

     delay(100);
     cleardevice();

     t = t + 1.0;
  }
  delay(100);
  closegraph();
}

int main()
{
   float x1 = 250, y1 = 100; //start coordinates of pendulum

   float l = 200; //length of pendulum
   float g = 10; //acceleration of gravity (assumed)

   float angle = Pi/2; // in radians

   float T = (2 * Pi) * sqrt(l/g); //period of swing

   animate_pendulum(x1, y1, l, g, angle, T);
}

Compile it with the command g++ simplependulum.cpp -o simplependulum -lgraph

Reference:

https://en.wikipedia.org/wiki/Pendulum
https://www.cs.colorado.edu/~main/bgi/doc/initgraph.html
https://www.myphysicslab.com/pendulum/pendulum-en.html