Complex Numbers

Avi Gupta
hackerLog
Published in
3 min readMay 19, 2024
An image I made.

This is an image I made using code. Or, well, something like that.

I made it by using complex numbers in the go programming language. Here’s how it works.

Visualizing complex numbers

To visualize each complex number, we use a complex number’s absolute value and “angle”.

We can find both like using some go code:

abs, arg := cmplx.Polar(z)

This produces two values such that

z = abs * e^(arg * i) 

To visualize the values, we can use colors to represent a complex number.

  • The hue of the color represents arg , where -pi/2 radians is red, and
  • The brightness of the color represents abs , where abs = infinity is white.

Some function visualizations

All images have been rendered in 1920 by 1080 resolution.

For the image at the top:

func fractal(z complex128) complex128 { // this is the one used in the blog post.
return z + complex(imag(z), real(z)) * cmplx.Conj(cmplx.Sin(z * z) * cmplx.Cos(z * z)) / 1
}
func spiral(z complex128) complex128 {
abs := cmplx.Abs(z)
return cmplx.Pow(z, cmplx.Sinh(complex(abs / 10, abs / 10))) * complex(math.Sin(abs), 0) * complex(1, math.Cos(abs))
}

res := filter(z)
func filter(z complex128) complex128 {
return complex(imag(z) + imag(z), real(z) / math.Abs(real(z)))
}

func fn(z complex128) complex128 {
abs := cmplx.Abs(z)
return z * complex(math.Cos(abs / 2), 0) * complex(1, math.Sin(abs * 10)) / 2
}

res := filter(fn(z))
func fn(z complex128) complex128 {

abs := cmplx.Abs(z)
return z * complex(math.Cos(abs / 2), 0) * complex(1, math.Sin(abs * 10)) / 2
}

func fractal(z complex128) complex128 { // this is the one used at the top.
return z + complex(imag(z), real(z)) * cmplx.Conj(cmplx.Sin(z * z) * cmplx.Cos(z * z)) / 1
}

res = fn(fractal(z))

The code

The code is relatively straightforward:

  1. Create an image of dimentions WIDTH and HEIGHT.
  2. Convert the location of each pixel into a complex number.
  3. Put the complex number through a function.
  4. Convert the result of that function into a color.
  5. Render that color on that pixel.

You can find the code here: https://github.com/racecraftr/complex-art.

Thanks for reading :D

--

--

Avi Gupta
hackerLog

Named after the first 2 games I got on my xBox, Forza and Minecraft. Also, i have a blog. Real name is Avi.