Generating Random Points within Box Around Figure
Show older comments
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 Comments
Adam Danz
on 28 Mar 2021
What do you mean "plot the box around the figure"?
For random number generation, use rand or randi.
Mari Teli
on 28 Mar 2021
Adam Danz
on 30 Mar 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.
Answers (1)
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 Comments
Adam Danz
on 30 Mar 2021
What are some example inputs (pos, vec, totalLength, AR)?
Mari Teli
on 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(:))
Mari Teli
on 1 Apr 2021
Adam Danz
on 1 Apr 2021
That's what my answer does.
Is there something about the answer that's not clear?
Mari Teli
on 1 Apr 2021
Categories
Find more on Chemistry in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!