X11 (also called Xlib or X Windows), is the 2D graphics and event library available on most Unix systems, and which has been used to develop Full Metal Jacket's development environment. Here, we use it to develop two simple programs, a damped harmonic oscillator and a sketch pad. Damped Harmonic OscillatorIn general, a damped harmonic oscillator has the equations y' = y + v * dt v' = v + a * dt where m * a = spring force + damping force = - k * (y - y0) - b * v The spring force is proportional to the distance from the equilibrium value, and the damping force is proportional to the velocity, opposite in direction to them. The mass oscillates back and forth through the equilibrium point, each oscillation smaller than the last. In the system modelled below, dt = 0.01 y0 = 300 m = 1 k = b = 0.1 a = - k * (y - y0) - b * v = - 0.1 * (y - 300) - 0.1 * v = 0.1 * (300 - y) - 0.1 * vso we have y' = y + v * 0.01 v' = v + a * dt = v + (0.1 * (300 - y) - 0.1 * v) * 0.01 It is necessary to have separate loops for y and v.
Values of v are updated from their initial value of 0.0 in the master
feedback loop (left), and values of y are updated from their initial value
of 100.0 in the slave feedback loop (right). To model the system
graphically, we have a circle move up and down on a canvas. The canvas,
created by ![]() To run, the inputs to the slave loop, the master loop and
any input of It is possible to change values of the constants while the system is running, with immediate effect. At present, input is by typing in the new value, but in future, input of "constants" using sliders and other GUI components will be added. The output is: ![]() Sketch PadKeyboard and mouse events can be captured by
Here, we implement a simple sketch pad, which allows us
to draw on a canvas. If the event is ![]() We can then draw on the canvas by pressing the left mouse button and dragging the mouse: ![]() © Copyright Donald Fisk 2015, 2016 |