matlabcartesian-coordinatescylindrical

How to find points inside, where sphere intersects with cylinder in MATLAB?


I am trying to identify points of sphere that are inside union of both sphere and cylinder, I have generated random points in side a cylinder as below

pts = 3000;
r= 3*((rand(pts,1)).^(1/3));
theta = 2*pi*rand(pts,1);
x= r.*cos(theta);
y= r.*sin(theta);
z=50*rand(size(x));

and generated random points inside a sphere as below

radius=10;
rvals = (2)*rand(pts,1)-(1);
elevation = asin(rvals);
azimuth = 2*pi*rand(pts,1);
radii =(rand(pts,1).^(1/3))*radius;
[point_x,point_y,point_z] = sph2cart(azimuth,elevation,radii);

The result will be as below

I need to find sphere points that is in intersection with cylinder and cylinder points that are in intersection with sphere.

Is there any general method to identify points inside intersection of different volumes ??

Can anyone help me? Thanks in advance, Manu


Solution

  • Apparently, you have a sphere with center at (0, 0, 0) and radius 10. To check whether points of the cylinder are in the sphere, you can use:

    cylinder_in_sphere = (x.^2 + y.^2 + z.^2) < 100
    

    Your cylinder has radius 3 and a height of 50, so to check whether points of the sphere are in the cylinder, you can use:

    sphere_in_cylinder = ((points_x.^2 + points_y.^2) < 9) & (points_z >= 0) & (points_z < 50)
    

    Note that in this particular case points_z < 50 is always satisfied, so you can remove it in this particular case.