Faster way to create Rectangles

19 ビュー (過去 30 日間)
vivek
vivek 2014 年 10 月 13 日
コメント済み: giannit 2021 年 5 月 7 日
I currently have a long 'for loop' over which I want to create rectangles. Is there a faster way i can do the below.The TPT on the below is not good.
for ii=1:10000
rectangle('Position',[xy(ii,1),xy(ii,2),xy(ii,3)-xy(ii,1),xy(ii,4)-xy(ii,2)],'EdgeColor','r');
end
I tried using patch function to create an object but it does not behave like the above loop.
patch('XData',[x; (x+w); (x+w); x],'YData',[y; y; (y+h); (y+h)],'FaceColor','white');
thanks

採用された回答

Oleg Komarov
Oleg Komarov 2014 年 10 月 13 日
To use patches you need first to organize the Xdata and Ydata in matrices of 4 rows, one per vertex, and as many columns as rectangles. You need to keep in mind that vertices follow the clockwise convention.
Consider the following simple example with two rectangles:
t = table([2 3; 2 3; 5 7;5 7],[1 10; 2 11; 2 11; 1 10], ones(4,2),...
'VariableNames',{'Xdata','Ydata','Zdata'},'RowNames',{'A','B','C','D'})
t =
Xdata Ydata Zdata
______ _______ ______
A 2 3 1 10 1 1
B 2 3 2 11 1 1
C 5 7 2 11 1 1
D 5 7 1 10 1 1
% Plot patch
patch(t.Xdata,t.Ydata,t.Zdata)
% (optional for clarity)
axis([0 10 0 15])
text(t.Xdata(:),t.Ydata(:),repmat(t.Properties.RowNames,2,1),'Color','red')
The result
  1 件のコメント
giannit
giannit 2021 年 5 月 7 日
Thank you this is very fast!

サインインしてコメントする。

その他の回答 (1 件)

matt dash
matt dash 2014 年 10 月 13 日
The short answer is that yes you should use patch, and no you should not call patch in a loop. Instead make the data for all 10000 rectangles (in a loop if necessary) and then call patch once using the face/vertex structure format.
Slowness of graphics is much more strongly related to the number of objects than to the complexity of each one... so 10000 simple patches will be a nightmare but 1 patch with 10000 faces should be ok.

カテゴリ

Help Center および File ExchangePolygons についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by