Mustapha Musa
Mustapha Musa

Reputation: 1

How generate random points within cylinder

how to generate uniformly distributed points within cylinders giving these parameters, Center point denoted as C, C1 is bottom and C2 is Top and R is Radius. C1=[5.697592e-01, 3.159923e-01, 1.037074e+00];C2=[4.211637e-01, 5.624242e-01, 7.937364e-02];R=5.000000e-01;

%the cordinate 
        C1=[5.697592e-01, 3.159923e-01, 1.037074e+00];
        C2=[4.211637e-01, 5.624242e-01, 7.937364e-02];
        N=600;
        CylinderX = rand(1,N);
        CylinderY = rand(1,N);
        CylinderZ = rand(1,N);
        CylinderHeight = norm(C1 - C2);

        for i = 1 : N
            A = [0 0 0 1];
            x = rand;
            y = rand;
            Px = sqrt(x)*R*cos(2*pi*y);
            Py = sqrt(x)*R*sin(2*pi*y);
            Pz = CylinderHeight * rand;
            %I need the coordinate transformation  logic here

            CylinderX(i) = A(1);
            CylinderY(i) = A(2);
            CylinderZ(i) = A(3);
        end
        plot3(CylinderX , CylinderY , CylinderZ ,'G.');hold on;

Upvotes: 0

Views: 294

Answers (2)

It looks like you are doing a fine job of sampling an axis-aligned cylinder with the center of one end at the origin.

But your target cylinder is neither axis aligned nor nailed onto the origin.

You need to transform the points you are generating to the proper region in space.

Construct a unit vector in the direction from C1 to C2, and two mutually perpendicular unit normals to it.1 These three vectors (which I will call b1, b2, and b3) form a basis and the transformed point is

p = C1 + (b1 * x + b2 * y + b3 * z)

where I have called the unit vector along the axis b3.


1 What pair of these you construct is quite arbitrary, and the naive way would be to take a cross product of b3 with any vector not (anti-)parallel with it to get the first normal direction (b1) and then b3 cross b1 to get the second normal direction. The only wrinkle is selecting an initial vector not parallel to b3.

Upvotes: 1

John
John

Reputation: 2127

The easiest way to generate uniformly distributed points within a 3D object like a sphere or a cylinder is to generate uniformly distributed points within a cube and discard any points that lie outside your desired shape.

Upvotes: 0

Related Questions