Posted by Jason Polak on 01. April 2015 · Write a comment · Categories: math

The other day I learned a little about the pygame python module that provides access to sdl functions on Linux along with some higher level functions. Basically, this means that you can use pygame to access some pretty low level graphics to draw things. I thought it might be fun to use it to make pretty pictures. Here’s an example where points are plotted, with the colour of each point a function of the coordinates. These simple examples should indicate how this type of low-level drawing can be used, perhaps to visualise complicated data or make interesting animations.

In sdl as with many computer graphics systems, coordinates start from the top left with $(0,0)$, and proceed positively right and down. The equations presented below give the red-green-blue (RGB) values of the color of the point at $(x,y)$. Each value is taken modulo $255$ (actually, I should have taken the value modulo $256$, since each color goes from $0$ to $255$, but I didn’t think carefully about this when I wrote the program!).

mod1 $\begin{align*}
r &= x\\
g &= x-y\\
b &= x + y
\end{align*}
$
mod2 $\begin{align*}
r &= x^2\\
g &= x\\
b &= x+y
\end{align*}
$
mod3 $\begin{align*}
r &= 255*\cos(x)\\
g &= 255*\sin(x)\\
b &= xy
\end{align*}
$
mod4 $\begin{align*}
r &= 100 + y\\
g &= x/(10 + y)\\
b &= x – y^2
\end{align*}
$
mod5 $\begin{align*}
r &= x + y + xy\\
g &= x – y + 240\\
b &= x + y^4
\end{align*}
$
mod6 $\begin{align*}
r &= x^2\\
g &= 4y – x^4\\
b &= y^3 – y^2 + y
\end{align*}
$
mod7 $\begin{align*}
r &= xy\\
g &= x^5-y^5\\
b &= x^{13}
\end{align*}
$
mod8 $\begin{align*}
r &= x^2y^3\\
g &= y + x^2 + 7xy\\
b &= x + y^2 + x^3 + x^4 + y^6
\end{align*}
$
mod9 $\begin{align*}
r &= 50*\sin(x^2 + y^2)\\
g &= 40 + 10\sin(x) + 20\cos(y)\\
b &= 200
\end{align*}
$
mod10 $\begin{align*}
r &= (1 + x + 100x^3)/(1 + 25y^2)\\
g &= y/4\\
b &= (1 + 200y^3)/(1 + x^4)
\end{align*}
$

Here is the code (since it’s so short, consider it public domain):

Leave a Reply

Your email address will not be published. Required fields are marked *