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 Kommentare

Adam Danz
Adam Danz am 28 Mär. 2021
What do you mean "plot the box around the figure"?
For random number generation, use rand or randi.
Mari Teli
Mari Teli am 28 Mär. 2021
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.
Adam Danz
Adam Danz am 30 Mär. 2021
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Adam Danz
Adam Danz am 29 Mär. 2021
Bearbeitet: Adam Danz am 29 Mär. 2021
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 Kommentare

Mari Teli
Mari Teli am 30 Mär. 2021
Bearbeitet: Adam Danz am 30 Mär. 2021
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.
Adam Danz
Adam Danz am 30 Mär. 2021
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 am 1 Apr. 2021
Bearbeitet: Adam Danz am 1 Apr. 2021
> 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Chemistry finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 28 Mär. 2021

Kommentiert:

am 1 Apr. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by