3

I need to generate random coplanar points in a 3-D space. The plane equation is:

a*x + b*y + c*z = d

I generate the plane by randomizing a, b, c and d. In order to generate random points on this plane, I use this code:

    switch(random.nextInt(3))
    {
        case 0:
        {
            x = random.nextInt(length);
            y = random.nextInt(width);
            z = (d - (a*x) - (b*y))/c;
            break;
        }
        case 1:
        {
            x = random.nextInt(length);
            z = random.nextInt(height);
            y = (d - (a*x) - (c*z))/b;
            break;
        }
        case 2:
        {
            y = random.nextInt(width);
            z = random.nextInt(height);
            x = (d - (b*y) - (c*z))/a;
            break;
        }
    }

But when I use Cayley-Menger Determinant to check if the points are coplanar, the determinant is never equal to zero (i.e. points are not coplanar).

The interesting point is, when I generate points on x = 0 plane, Cayley-Menger determinant works perfectly fine!

Is this a rounding error or something else?

EDIT: length, width and height are integer values.

Community
  • 1
  • 1
  • 2
    Looks like an integer division problem. – McLovin Feb 19 '14 at 19:00
  • So, why does the second case work fine? –  Feb 19 '14 at 19:10
  • Try using float/double instead of integers – korhner Feb 19 '14 at 19:41
  • I use the exact same data types in second case. –  Feb 19 '14 at 19:44
  • When I ran the code, it appeared to generate points on the plane for my hard coded values of a,b,c and d. You should print your randomly generated values of a,b,c,d and points to see if you can find a case where your code doesn't generate points on the plane. If you are calculating the determinant by hand, perhaps consider checking your work to see if there may be an error in your calculations. – Daniel Jacobson Feb 19 '14 at 20:22
  • 3
    Hi, perhaps a bit off topic, but (aside from using floats/doubles) I find it more natural to generate the points as random combinations of two linearly independent vectors (since they span the plane). – Nicko Po Feb 19 '14 at 22:44
  • your problem is integer division rounding, if your second case work that could be just coincidence. It could be done on integers but then the values must be big enough and no division use (for example like Nicko Po wrote) and also the Cayley-Menger Determinant has to be computed and checked with integer round accuracy error in mind (generated points will be very near your plane most the time not on it +/- ~one unit) – Spektre Feb 26 '14 at 10:58
  • @NickoPo hi! could you please give some detail on the method? How can one generate two independent vectors and create random points on the plane they define? – padawan Sep 06 '17 at 17:04
  • @padawan Hi, consider canonical basis vectors (3d) for X and Y axis: i=(1,0,0) and j=(0,1,0). Those two vectors span the XY plane.Specifically, any point on the XY plane can be written as point = a*i + b*j, where a and b are some numbers. i.e. point (4,-7,0) = 4*(1,0,0) + (-7)*(0,1,0). To extend this to arbitrary plane, choose two random linearly independent vectors (in 3d). Linear independence can be checked via dot product. Plane normal generated via cross product. This plane will pass through origin. If you want off origin, just add a random vector to equation. Hope that helps. – Nicko Po Sep 06 '17 at 19:16
  • @NickoPo thank you! that is a really good answer! – padawan Sep 06 '17 at 19:29

0 Answers0