A real-time simulation of traffic flow through a randomly generated city grid, demonstrating basic Graph
Theory and congestion dynamics.
1. The Graph
The city is a Geometric Graph.
Nodes represent intersections. They are scattered randomly.
Edges represent roads. Two nodes are connected if they are close enough (within a radius).
To ensure you can drive anywhere, we run an algorithm to bridge any disconnected "islands" together,
guaranteeing a fully connected graph.
2. Pathfinding
When a car spawns, it picks a random start and end point.
It finds the shortest route using Dijkstra's Algorithm, optimizing for distance.
3. Traffic Logic
Cars aren't just dots; they react to density.
Each road has a calculated speed limit based on how many cars are on it:
Speed = Base / (1 + Density²)
As more cars enter a road, everyone slows down quadratically. This naturally creates traffic jams (red nodes)
that take time to clear even after the blockage is gone.
Controls
Regenerate City: Creates a new random layout.
Watch the Avg Speed stat to see how efficiently your city is running.