RiPoints on a disk


return to main index


#include <stdlib.h>


Another tutorial, random points on a sphere covers material that is very similiar to this web page. It is recommended you review that material.

Being able to generate random points that are distributed across a disk, or part of a disk, can be helpful when trying to portray a variety of phenomena - spiral arms of hurricanes, galaxies and catherine-wheel firework displays.

The first step is to generate a number of vectors in a square - well actually, a cube of zero thickness!

float xyz[3];
  
/* generate random points */
for(n = 0; n < 1000; n++)
    {
    xyz[0] = randBetween(-1, 1); /* x */
    xyz[1] = 0;                      /* y */
    xyz[2] = randBetween(-1, 1); /* z */
    /* use xyz in some way, then generate another point */
    }

Next we constrain the random vectors to lie on the rim of a disk?


square

Download the source code
Download the RIB file

next >>





































USING UNIT VECTORS

Instead of considering the random xyz coordinates as defining 3D points consider them as defining the coordinates of random 3D vectors. Therefore, we now have a 1000 vectors of different lengths. By normalizing each vector ie. making them one unit in length, we are in effect distributing them around the perimeter of a disk or circle of radius 1 unit.

The xyz coordinate of each position vector can be used to locate a small object, such as a sphere or a (ri)point.

A function that performs the normalization is shown opposite.

circle

The code on the previous page would be modified to use the normalize() function...

/* normalize the vectors before using them */
normalize(xyz);

The next page shows how the points can be randomly distributed to form a disk.

void normalize(float pnt[3])
{
 float length = sqrt(pnt[0] * pnt[0] + 
                     pnt[1] * pnt[1] + 
                     pnt[2] * pnt[2]);
pnt[0] /= length;
pnt[1] /= length;
pnt[2] /= length;
}

<< prev

next >>





































DISTRIBUTION ACROSS A DISK

After normalization the vectors should be scaled by a random value between 0.0 and "radius" - where radius defines the size of a disk. As a consequence the vectors will randomly fill the disk.

/* normalize the vectors then randomize
their positions - ignore the y coordinate */
normalize(xyz);
float randomRadius = randBetween(0, radius);
xyz[0] *= randomRadius;
xyz[2] *= randomRadius;

If instead of ignoring the y coordinate we do this,

xyz[0] *= randomRadius;
xyz[0] += randomRadius;
xyz[2] *= randomRadius;

...a cone is formed because the y coordinate of each vector is proportional to its "radius"!

Alternatively, if we randomize the y coordinate without effecting the x and z coordinates ie.

/* xyz[0] *= randomRadius; */
xyz[0] += randomRadius;
/* xyz[2] *= randomRadius; */

...a cylinder is formed.


disk


cone
cylinder

<< prev

next >>


© 2002-4 Malcolm Kesson. All rights reserved.