Generating Random Points within Box Around Figure

Hello! I'm stuck on a project which involves generating random points within a box around a figure.
% length, width and height of box
L=0.65; W=0.65; H=2;
% pick plenty of random points
x= L*rand(1); %x-coordinate of a point
y= W*rand(1); %y-coordinate of a point
z= H*rand(1); %z-coordinate of a point
Rectangle (Position, [x y w h])
This is currently what I have and I would like to set up the box around another figure however I don't know how to plot the box around my figure as well as generate random points within the box? If anyone can direct me that would be great. Thank you.

댓글 수: 3

What do you mean "plot the box around the figure"?
For random number generation, use rand or randi.
I have a sphereocylinder and I need to generate a random point within it but I'm not sure how. I can also send the spherocylinder code. I was thinking of plotting a 3D rectangle around the spherocylinder and generating a point within that but I don't know which method works better.
You wouldn't need to plot the 3D cube to produce random points within the 3D area. More importantly, it sounds like the 3D object is rounded so some of the cube would be outside of the 3D object.

댓글을 달려면 로그인하십시오.

답변 (1개)

Adam Danz
Adam Danz 2021년 3월 29일
편집: Adam Danz 2021년 3월 29일
xl yl and zl are 1x2 vectors defining the range of x y and z coordinates of the 3D object.
n is the number of random values to generate.
x = rand(1,n)*range(xl)+xl(1);
y = rand(1,n)*range(yl)+yl(1);
z = rand(1,n)*range(zl)+zl(1);
plot3(x,y,z,'o')
This does not guarantee that all random points will be within the 3D object unless the orientation of the object is parallel to all 3 axes. Otherwise, you can determine which points are outside of the object and remove or replace them.

댓글 수: 7

Mari Teli
Mari Teli 2021년 3월 30일
편집: Adam Danz 2021년 3월 30일
Thank you! I am having trouble specifying the ranges for x,y,z. I put the code for the spherocylinder below It makes a pill capsule shape, and I want the range for each value to be within the shape. So for x and z between (-0.325, 0.325) and for y from (-1,1).
function [X,Y,Z]=spherocylinder(pos, vec, totalLength, AR)
%% preliminary
points = 20;        
vec = vec/norm(vec);
D = (0.65);
radius = D/2;
bottom = -0.675; % where bottom of cylinder is
top = 0.675        % where top of cylinder is
%% Generate cylinder with center at origin
[xcyl,ycyl,zcyl]=cylinder(radius,points);
zcyl(1,:) = bottom;
zcyl(2,:) = top;
%% Generate points for sphere
[x,y,z] = sphere(points);
pdist = points/2+1;
% split sphere into top and bottom hemispheres
xtop = x(pdist:end,:)*radius;
ytop = y(pdist:end,:)*radius;
ztop = z(pdist:end,:)*radius+top;
xbot = x(1:pdist,:)*radius;
ybot = y(1:pdist,:)*radius;
zbot = z(1:pdist,:)*radius+bottom;
%% Combine data for spherocylinder oriented along z-direction
X = [xbot; xcyl; xtop];
Y = [ybot; ycyl; ytop];
Z = [zbot; zcyl; ztop];
%% Plot spherocylinder
surf(X, Y, Z, 'EdgeColor','none',...
  'LineStyle','none','FaceColor',[0.85 0.85 0.85]);
axis equal;
Also, once I have the point, do you know how I could use normrnd to simulate the point taking s “step”? I want to choose a value from the normal dis in each dimension and then have the point move by that amount in each direction.
What are some example inputs (pos, vec, totalLength, AR)?
Thank you for your response! The example input would be spherocylinder(0,[0,0,1],2,2).
Adam Danz
Adam Danz 2021년 4월 1일
편집: Adam Danz 2021년 4월 1일
> I am having trouble specifying the ranges for x,y,z.
Use the min and max functions
min(x(:)), max(x(:))
or use
[a,b] = bounds(x(:))
That sounds good. How would I incorporate the min and max functions in order to generate the random values within the min and max?
This would be my code:
min (x (-0.325)), max (x (0.325));
min (y (-1)), max (y (1));
min (z (-0.325)), max (z (0.325));
Thank you for your help!
That's what my answer does.
Is there something about the answer that's not clear?
Your answer is clear! I've run into another issue. I'm trying to run my spherocylinder code, but to do that I have to save the code and then run it as a command "run spherocylinder.m" This code has worked before, but now I'm getting this error (shown in screenshot). This is my code:
function [X,Y,Z]=spherocylinder(pos, vec, totalLength, AR)
%% preliminary
points = 20;
vec = vec/norm(vec);
D = (0.65);
radius = D/2;
bottom = -0.675; % where bottom of cylinder is
top = 0.675 % where top of cylinder is
%% Generate cylinder with center at origin
[xcyl,ycyl,zcyl]=cylinder(radius,points);
zcyl(1,:) = bottom;
zcyl(2,:) = top;
%% Generate points for sphere
[x,y,z] = sphere(points);
pdist = points/2+1;
% split sphere into top and bottom hemispheres
xtop = x(pdist:end,:)*radius;
ytop = y(pdist:end,:)*radius;
ztop = z(pdist:end,:)*radius+top;
xbot = x(1:pdist,:)*radius;
ybot = y(1:pdist,:)*radius;
zbot = z(1:pdist,:)*radius+bottom;
%% Combine data for spherocylinder oriented along z-direction
X = [xbot; xcyl; xtop];
Y = [ybot; ycyl; ytop];
Z = [zbot; zcyl; ztop];
%% Plot spherocylinder
surf(X, Y, Z, 'EdgeColor','none',...
'LineStyle','none','FaceColor',[0.85 0.85 0.85]);
axis equal;
Do you know what the error message I get means, and what I have to fix to get it to work again. It's weird because I haven't changed any part of the code and it used to work so I'm extremely confused.
Thank you.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Chemistry에 대해 자세히 알아보기

질문:

2021년 3월 28일

댓글:

2021년 4월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by