Tag Archives: Math

Random point in circle

The best algorithm to generate a point in a circle is to generate a point in a square with the same width as the circle diameter and then rerun the algorithm until this random point is inside the circle. This algorithm might need to run a couple of times before finding a suitable point but will be faster than a more algorithmic solutions.

Pseudo code:

  1. Randomize a point in the square
  2. If this point is outside the circle then jump to 1
  3. Done


Lerp is the procedure to interpolate between two vectors with a specified weight. An example below

float weight; // the weight for lerping
vec2 input1, input2; // the inputs to lerp between
vec2 lerpResult = input1(1.0-weight)+input2(weight);

In GLSL it’s named mix( genType, genType, float ) instead of lerp which can be misleading. But when testing, it actually works to use lerp instead of mix in GLSL shaders when running them on a Nvidia GeForce 8800GTS although it’s not recommended.

Here’s the OpenGL GLSL quick reference guide which contains all functions you can use inside of a shader (and much more).


 Scaling can be archived by the following matrix:

Scale Matrix

The s components are the scaling factor in the x-,y- and z-directions. If they are all the same then the scaling is uniform.

Uniform Scaling

In OpenGL you can do scaling with the following code:

glScalef(sx,sy, sz);


In 3D programming translation is often represented as a matrix. The following matrix translates a point by a vector Translation Vector.

The matrix for translation is given below (notice that only points can be translated, not vectors)
Translation Matrix
The inverse of the matrix is simply T(-t).

In OpenGL you can do translation on objects with the following two lines of code. The first line sets that it’s the modelview matrix that you want to perform calculations on.

glTranslatef(tx, ty, tz);