RiPoints on a disk#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.
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? |
Download the source code |
||
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 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; } |
|||
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"!
/* xyz[0] *= randomRadius; */ xyz[0] += randomRadius; /* xyz[2] *= randomRadius; */ ...a cylinder is formed. |
|
|||
© 2002-4 Malcolm Kesson. All rights reserved.