How can i generate random distributed bar objects

2 views (last 30 days)
I need to generate some random bar objects to test my collision avoidance algorithm hoe can I do that?

Answers (3)

KSSV
KSSV on 23 Oct 2018
S = [0 0 ; 1 0 ; 1 1 ; 0 1]/4 ;
figure
hold on
N = 5 ;
for i = 1:N
C = rand(1,2) ; % center of sqaure
x = S(:,1)+C(1) ;
y = S(:,2)+C(2) ;
patch(x,y,'r')
end
  5 Comments
muhammad ahmad
muhammad ahmad on 23 Oct 2018
I need to this kind of job with cubes as well how ca i do it
KSSV
KSSV on 23 Oct 2018
N = 5 ;
alpha=0.5; % transparency (max=1=opaque)
X = [0 0 0 0 0 1; 1 0 1 1 1 1; 1 0 1 1 1 1; 0 0 0 0 0 1];
Y = [0 0 0 0 1 0; 0 1 0 0 1 1; 0 1 1 1 1 1; 0 0 1 1 1 0];
Z = [0 0 1 0 0 0; 0 0 1 0 0 0; 1 1 1 0 1 1; 1 1 1 0 1 1];
X = X/4 ; Y = Y/4 ; Z = Z/4 ;
figure
hold on
for i = 1:N
C = rand(1,3) ;
x = X + C(1);
y = Y + C(2);
z = Z + C(3);
fill3(x,y,z,C,'FaceAlpha',alpha); % draw cube
end

Sign in to comment.


muhammad ahmad
muhammad ahmad on 23 Oct 2018
Thank you for your contribution one more thing can we extend these cubes in z axixs from 0 to different heights?
  1 Comment
KSSV
KSSV on 23 Oct 2018
N = 5 ;
alpha=0.5; % transparency (max=1=opaque)
X = [0 0 0 0 0 1; 1 0 1 1 1 1; 1 0 1 1 1 1; 0 0 0 0 0 1];
Y = [0 0 0 0 1 0; 0 1 0 0 1 1; 0 1 1 1 1 1; 0 0 1 1 1 0];
Z = [0 0 1 0 0 0; 0 0 1 0 0 0; 1 1 1 0 1 1; 1 1 1 0 1 1];
X = X/4 ; Y = Y/4 ; Z = Z/4 ;
figure
hold on
for i = 1:N
C = rand(1,3) ;
x = X ;
y = Y ;
z = Z + i;
fill3(x,y,z,C,'FaceAlpha',alpha); % draw cube
end
Thank you officially for accepting this answer.

Sign in to comment.


muhammad ahmad
muhammad ahmad on 23 Oct 2018
well this code is stacking cubes along z-axis but I didn't mean that I was asking about the extension of individual cube simply i just want to do the same work as scattere3(x,y,z) does and generate the random dots instead of dots i want to plot bars or cubes

Categories

Find more on Data Distribution Plots 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!