Laboratory: Firework.

Zsolt Kohári · 2019.09.15.

Lists: an application example.

Preparation for lab:

  • Review lecture topics on linked lists.
  • Review laboratory topics on linked lists.

1. Firework

To administer a continously varying amount of data efficiently a linked list should be used. In social media systems the logged on users must be stored. Some people just log on to check messages and log off soon. Other users stay logged on for a long time. Using dynamic arrays would result in a huge amount of unnecessary data movement operation.

If you want to simulate a firework the situation is very similar: there are moments when there are very few light spots, and after an explosion there are plenty. The number of particles varies frequently and in a wide range. List may be the best choice to store them.

Physics: the movement of a particle in Δt time:

  • Position: r = r + vΔt
  • Speed: v = v + gΔt

When a rocket explodes into a lot of small parts, they must be inserted into the list and they obey the same rules. Particles should be deleted from the list when they fade away. The type determines what happens when the lifetime of a particle is over: explodes or gets deleted. This is the basic structure to represent a particle in a list:

enum Type { exploding, fading };
typedef struct Point {
    enum Type type;
    double x, y;        /* position */
    double vx, vy;      /* velocity */
    unsigned char r, g, b; /* colour */
    double lifetime;  /* time until explosion or deletion */

    struct Point *next;    /* in the list */
} Point;

(In the final program there are two more types in the enumeration to represent the two stages of a flower-like double explosion rocket.)

In the main simulation loop each element of the list gets processed.

  • When the lifetime of a particle is over, depending on its type, either it gets deleted, or it explodes and 30 new particles get inserted right after the current element.
  • Otherwise the lifetime gets decreased by Δt and the physics is applied to move the particle.

New elements get inserted after the actual element (this way the same loop iteration will process them). It is easier to insert after the actual than before it:

For a possible deletion the loop maintains a pointer to the previous element. Since the actual element (iter) will be deleted, the loop can not step like iter=iter->next – the next pointer must be saved before actually deleting the element:

The „previous” pointer must be handled carefully. When the actual element remains in the list the „previous” pointer should point to it in the next loop iteration. When the actual gets deleted, the „previous” pointer should not change its value. There is a sentinel at head position, thus the insertion does not require any further conditional statement, and the head pointer never changes.

Download the ZIP file and extract the firew folder it contains to your user folder. Open the project in Code::Blocks and examine the code. Understand the role of the functions and how they work together in the program. Build it and test it.