Modular Multiplication Circle

A visualization of modular arithmetic patterns using lines drawn over a circle.

The complete code can be found here

Overview

This visualization was implemented in C++ using SFML as a rendering library with ImGui for UI controls. It draws N points evenly spaced around a circle and connects each point i to (i × k) mod N. The result produces intricate and often beautiful patterns governed by modular multiplication.

The UI allows you to control:

Live Demo

This demo shows the modular multiplication with N = 250 and k in the range [240, 246].

Some Explanation

A total of N points are placed evenly around a circle using:


for( i = 0 to N - 1 )
	angle = 2 * PI * i / N
	a = ( center.x + radius * cos( angle ), center.y + radius * sin( angle ) )
				

Next, for every point i, it is connected to the point ( i * k ) mod N.


for( i = 0 to N - 1 )
	a = points[i]
	b = points[( i * k ) % N]
				

This works for integers but to animate this we can use k as a floating point number and calculate the position of the point which i is connected to using:


for( i = 0 to N - 1 )
	angle = 2 * PI * i * k / N;
	b = ( center.x + radius * cos( angle ), center.y + radius * sin( angle ) )
				

Finally, we draw a line from a to b.