Reputation: 55
I am trying to generate random spheres inside a cube model using MATLAB. I have posted one part of the code that am trying to develop. One constraint is to make sure the random sphere is bounded inside a cube. Radius is between (0.15 mm - 0.55 mm). Variable (dims) is the dimension of cube. dims = [ 10 10 10 ] (Cube dimension is of 10mm * 10mm * 10mm)
function [ c, r ] = randomSphere( dims )
r = 0.15 + ( 0.55 - 0.15) .* rand(1);
x = (10 - r) * rand(1) + r;
y = (10 - r) * rand(1) + r;
z = (10 - r) * rand(1) + r;
c = [ x y z];
Any ideas on improving the code. All the co-ordinates generated are less than 1 i.e between (0,1). How should i scale the co ordinates? Say something like (3.5, 5.6, 6.7) is also bounded inside a cube.
Upvotes: 0
Views: 1187
Reputation: 26069
You almost got that right, here's a way to do it:
function [ c, r ] = randomSphereGen()
r = 0.15 + ( 0.55 - 0.15) .* rand(1);
c = bsxfun(@times,(10 - 2*r) , rand(1,3)) + r;
Upvotes: 1