How can i create random rectangles (automatically)?
이전 댓글 표시
I want to create many rectangles. This should be done automatically. How can i do this without typing thousands of values in my code? Is there an solution?
- In my code i wrote every single coordinate point (4 points of each rectangle) manually in my vector "V".
- Also how to connect them. "F"
- And the value of each rectangle. "C"
Thank you for your input/help. I really appreciate it.
clc
clear all
figure;
V = [0,0;1,0;1,1;0,1;5,5;10,5;10,10;5,10;2,2;4,2;4,4;2,4];
F = [1,2,3,4;5,6,7,8;9,10,11,12];%Dieser Vektor sagt mir in welcher Reihenfolge die Punkte
C = [50;24;99];
patch('Faces',F,'Vertices',V,'FaceVertexCData',C,'FaceColor','flat','EdgeColor','none') %Befehl fürs "zeichnen"
colormap(parula)
colorbar
채택된 답변
추가 답변 (2개)
Adam
2016년 9월 8일
Using
doc rectangle
would be simpler than defining a patch like that I would think. Then if you want random positions it is just a case of creating random numbers and because you are defining a top left location and height, width you will always have a rectangle rather than using patch with random numbers.
댓글 수: 3
Philipp Mueller
2016년 9월 8일
KSSV
2016년 9월 8일
Adam wants to say...you read the command named rectangle in matlab.
Type help rectangle
Guillaume
2016년 9월 8일
@Siva, as per Adam's post
doc rectangle
instead of help rectange. You get nicer documentation.
KSSV
2016년 9월 8일
clc; clear all
for i = 1:10
ver = RandRect(rand(1,2),rand(1,2)) ;
patch(ver(:,1),ver(:,2),'r')
hold on
end
function vertices = RandRect(P1,P3)
x1 = P1(1) ; y1 = P1(2) ;
x2 = P3(1) ; y2 = P3(2) ;
L = x2-x1 ;
B = y2-y1 ;
P2 = [x1+L y1] ;
P4 = [x1 y1+B] ;
vertices = [P1 ; P2; P3; P4] ;
댓글 수: 2
Philipp Mueller
2016년 9월 8일
KSSV
2016년 9월 8일
Copy the lines:
for i = 1:10
ver = RandRect(rand(1,2),rand(1,2)) ;
patch(ver(:,1),ver(:,2),'r')
hold on
end
where you want to use in your code. and keep the function RandRect.m in the same folder.
카테고리
도움말 센터 및 File Exchange에서 Color and Styling에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!