How to generate a random point in the volume of a cylinder

์กฐํšŒ ์ˆ˜: 15 (์ตœ๊ทผ 30์ผ)
Musa Mustapha
Musa Mustapha 2019๋…„ 6์›” 24์ผ
๋Œ“๊ธ€: John D'Errico 2019๋…„ 6์›” 24์ผ
I have a cylinder in three-dimensions with a long-axis defined by the endpoints ?1 and ?2, and radius ?. p1=[0.5, 0.3, 1]; p2=[0.4, 0.5, 0.7]; R=0.5;
  ๋Œ“๊ธ€ ์ˆ˜: 2
James Tursa
James Tursa 2019๋…„ 6์›” 24์ผ
What have you done so far? What specific problems are you having with your code? Do you know how to generate a random point inside a circle? Do you know how to rotate vectors in 3D space?
John D'Errico
John D'Errico 2019๋…„ 6์›” 24์ผ
I showed you how to rotate vectors in 3-d space.

๋Œ“๊ธ€์„ ๋‹ฌ๋ ค๋ฉด ๋กœ๊ทธ์ธํ•˜์‹ญ์‹œ์˜ค.

์ฑ„ํƒ๋œ ๋‹ต๋ณ€

John D'Errico
John D'Errico 2019๋…„ 6์›” 24์ผ
Since you already have an answer posted... Pretty easy, actually. So two points that define the ends of the cylinder, and a known radius.
p1=[0.5, 0.3, 1];
p2=[0.4, 0.5, 0.7];
R=0.5;
N = 100000; % # of points to generate
First, generate a random point along the cylinder axis. The vector that defines the axis is given by:
axialvec = p2 - p1;
axialvec = axialvec/norm(axialvec);
axialpoints = p1 + (p2 - p1).*rand(N,1);
Next, we work in a cylindrical coordinate system around the centerline. Generate points at random inside a circle of radius R, in TWO dimensions.
circr = sqrt(rand(N,1))*R;
circtheta = rand(N,1)*2*pi;
circpoints = [cos(circtheta).*circr,sin(circtheta).*circr];
Rotate the points into the plane perpendicular to the axis.
axnull = null(axialvec);
points = axialpoints + circpoints*axnull.';
And plot.
plot3(points(:,1),points(:,2),points(:,3),'.')
grid on
box on
axis equal
Rotate it around to convince yourself the points form a cylinder.
untitled.jpg

์ถ”๊ฐ€ ๋‹ต๋ณ€ (0๊ฐœ)

์นดํ…Œ๊ณ ๋ฆฌ

Help Center ๋ฐ File Exchange์—์„œ Spectral Measurements์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by