← back

Zac Garby's Website

Visualising Electric Fields

I've been learning about electric charge and the interesting-looking field diagrams they can make. But as cool as they look, there's always the problem that it's very very difficult to draw a field diagram for more than two or possibly three point charges. And, it's even more difficult if you wanted to draw them in 3 dimensions.

From Wikipedia

With a computer, it's not too difficult though. The rough plan is to place a number of charges randomly in 3D space and for each pixel (voxel?) calculate the field strength at each one. Sounds simple enough, but imagine having 512x512 voxels and that's already 262,144 calculations. Luckily, computers are pretty fast at number crunching and though it certainly won't be instant, it shouldn't take too long.

First, we'll need a way to calculate the field strength at a given point. This is done using the electrical field strength formula: $$ E = \frac{1}{4\pi\epsilon_0}\frac{Q}{r^2} $$ This looks quite confusing, but it's in fact very similar to the gravitational field strength formula, except the constant of proportionality is much more complicated. $E$ is the field strength, $\epsilon_0$ is the permittivity of free space (not relavent), $Q$ is the charge of the charged particle, and $r$ is the distance from the charged particle.

The first problem is that this formula only works for one point. We will have to sum together the field strengths of each charged particle in the space to include all of them in the final field strength value. This can't be simply done with $\sum{E}$ because they are scalars in their current form. We know that vectors are essentially scalars with magnitude, and as a result we can derive the formula: $$ E_{total}(\rho) = \sum{(|\xi - \rho| \frac{1}{4\pi\epsilon_0} \frac{Q}{r^2})} $$ Where $E_{total}(\rho)$ is the total vector charge at location vector $\rho$. The right hand side basically says "The vector sum of the norm of the vector differences from $\rho$ to the position, $\xi$, of each charge, multiplied each time by the corresponding field strength magnitude".

Now we have a function to get the field vector at each point, we just need a way to pick a colour for each point and then we'll be done. A simple way to do this is to take the magnitude of the field strength vector at each point and alternate red and white based on that value: $$ Colour(E) = \left\{ \begin{array}{ll} red & \lfloor E \rfloor = 0 \mod 2 \\ white & else \\ \end{array} \right.$$

Ok, so that wasn't quite the last thing. I mean, it would work at this point, but the constant of proportionality $\frac{1}{4\pi\epsilon_0} = 8987551788$ is so large that the colours (red and white) will alternate so quickly that it will be hard to differentiate them apart. To fix this, we can cheat be redefining $\frac{1}{4\pi\epsilon_0} := 10000$ (actually, $10000$ could be anything, but it seems to work nicely for this case).

To put all this together, I can create a nice big function to get the colour based on the vector location: $$ Colour(\rho) = \left\{ \begin{array}{ll} red & \lfloor \sum{|\xi - \rho| \frac{1}{4\pi\epsilon_0} \frac{Q}{r^2}} \rfloor = 0 \mod 2 \\ white & else \\ \end{array} \right. $$

And that's it, in terms of the maths! I've implemented this in Go here — have a go and see if you can make it too, and change it in some interesting way! Of course, I wouldn't write this entire article without posting some images of the final thing, so here you are.

A 2D visualisation of a 3D model of an electric charge field.

It's difficult to render an entire 3D image to a 2D monitor [citation needed] so I decided to render each vertical slice to a frame in a gif. It makes a nice "fluid" animation.

A static image, but with three colours instead of two.

This colouring scheme ($colour \propto magnitude \mod n$) is obviously different from drawing the field lines (as in the first image on this article) which shows the direction a positively charged particle would move. With the colouring I am using, each band of colour shows an equipotential line, or in other words a line across which you could move without putting any work into the system.

My wallpaper.

If you want, there are a few more of these images on my homepage.