2

I'm trying to randomly and uniformly generate points on a cube surface in processing. I'm trying to make an animation so I'd need the x, y and z final position of the points.

Any suggestions? Thanks.

tony danza
  • 306
  • 7
  • 22
  • You want to randomly generate points on the surface of a cube, or within a cube? – aioobe Aug 05 '12 at 10:58
  • Do you have a method for generating random uniform numbers on the interval [0,1) ? – High Performance Mark Aug 05 '12 at 11:21
  • sorry, i meant on the surface of a cube. @HighPerformanceMark I have the random(0,1) method, but how should I use it? – tony danza Aug 05 '12 at 11:40
  • under the assumption that your cube is an axis-aligned unit cube it should be quite easy. For each point one of the axis should be 0 or 1. The remaining axes should be in [0,1]. So first choose a random axis, then randomly choose between 0 or 1 for that axis and finally choose values between [0,1] for the remaining axes. – D-rk Aug 05 '12 at 12:23
  • but i don't know if this solution meets your requirements of the point distribution on the surface. – D-rk Aug 05 '12 at 12:25
  • the points should be distributed like in this sphere http://www.anderswallin.net/wp-content/uploads/2009/05/fig2.jpg (right) – tony danza Aug 05 '12 at 12:30
  • possible duplicate of [uniform generation of points on 3D box](http://stackoverflow.com/questions/2671415/uniform-generation-of-points-on-3d-box) – ltjax Aug 05 '12 at 13:57

1 Answers1

6

Just combine three uniform random distributions. This method assumes you have a unit cube [0..1]^3. If that is not the case, just scale and offset the points after you generate them.

  1. Pick a cube side by getting an integer from [0..6)
  2. Pick an x coordinate in that side's plane
  3. Pick an y coordinate in that side's plane

Here's some C++-ish pseudocode:

vec3 result;
int s=randomSide(); // returns 0 to 5, uniformly distributed
int c=s%3; // get the axis perpendicular to the side you just picked

result[c]=s>2 ? 1.f : 0.f;
result[(c+1)%3]=random01();
result[(c+2)%3]=random01();

If you have a more general box instead of a cube, you have to bias the side-picking according to the areas of the box sides.

ltjax
  • 15,837
  • 3
  • 39
  • 62
  • I'm sorry I'm not used to c++, by result[c] and so on you mean vector.x vector.y vector.z? thanks for your answer – tony danza Aug 05 '12 at 16:07
  • It's supposed to access the n'th coordinate, so result[0] is x, result[1] is y and result[2] is z – ltjax Aug 05 '12 at 16:24
  • got it. this code is tricky to translate in processing cause I have to use PVectors and access them with positions like vector.x – tony danza Aug 05 '12 at 16:38
  • Just write a small helper function that writes to the n'th coordinate. That should make it easier. – ltjax Aug 05 '12 at 19:10
  • I'm still missing a face.. trying with random(0, 6) but it doesn't work :/ – tony danza Aug 05 '12 at 21:17
  • The problem is probably on your end. Double-check that you get integers 0 to 5. 0,1,2 should be one half of the cube, 3,4,5 the other. – ltjax Aug 05 '12 at 22:08