現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Randomly generated complex polygon with user input
1 回表示 (過去 30 日間)
古いコメントを表示
Matthew
2013 年 4 月 5 日
I want to generate a randomly shaped complex polygon which has start point (0,0) and has vertices defined by user input.
採用された回答
Image Analyst
2013 年 4 月 5 日
How about using ginput() or impoly()?
17 件のコメント
Matthew
2013 年 4 月 5 日
No i need the function to draw the polygon not the user the user just supplies the number of vertices.
Image Analyst
2013 年 4 月 5 日
Use inputdlg() to get an integer. Then
coordinates = rand(numberOfVertices, 2);
coordinates(1,:) = [0, 0]; % Force first coord to 0,0
plot(coordinates(:,1), coordinates(:,2), 'r-');
Note - it's very "complex" = it can even have overlapping regions (figure 8's). If you don't want that, you have to find the average (centroid) and find all the angles, and sort by angle. At least that's one way.
Matthew
2013 年 4 月 5 日
I tried but i just got this error.
Undefined function or variable 'numberOfVertices'.
Error in plotfunc (line 3) coordinates = rand(numberOfVertices, 2);
prompt = {'Enter Number of verticies:'};
numberOfVerticies = inputdlg(prompt);
coordinates = rand(numberOfVertices, 2);
coordinates(1,:) = [0, 0]; % Force first coord to 0,0
plot(coordinates(:,1), coordinates(:,2), 'r-');
Image Analyst
2013 年 4 月 5 日
編集済み: Image Analyst
2013 年 4 月 5 日
Sorry - misspelled it. Try this:
prompt = {'Enter Number of vertices:'};
numberOfVertices = str2num(cell2mat(inputdlg(prompt, 'enter a number', 5)))
coordinates = rand(numberOfVertices, 2);
coordinates(1,:) = [0, 0]; % Force first coord to 0,0
coordinates(end+1,:) = [0, 0]; % Force last coord to 0,0
plot(coordinates(:,1), coordinates(:,2), 'bo-');
grid on;
Matthew
2013 年 4 月 6 日
Exactly what i need thank you for the help. One last thing, is there an easy way to fill the plot with a colour?
Matthew
2013 年 4 月 7 日
i tried both i just cant get them to work. No way of typing fill works i'm stuck on it. I want it to use 'RGB::random()' for the color so all would be different.
function plotfunc()
prompt = {'Enter Number of vertices:',}
numberOfVertices = str2num(cell2mat(inputdlg(prompt, 'enter a number', 5)))
coordinates = rand(numberOfVertices, 2);
coordinates(1,:) = [0, 0]; % Force first coord to 0,0
coordinates(end+1,:) = [0, 0]; % Force last coord to 0,0
plot(coordinates(:,1), coordinates(:,2));
fill(plot(coordinates(:,1), coordinates(:,2)),'RGB::random()');
grid on;
end
Matthew
2013 年 4 月 7 日
編集済み: Image Analyst
2013 年 4 月 7 日
Is it just me getting the fill(X,Y,C) wrong?
Image Analyst
2013 年 4 月 7 日
編集済み: Image Analyst
2013 年 4 月 7 日
You can't put plot() inside fill(). Take it out. Try this:
% prompt = {'Enter Number of vertices:',}
% numberOfVertices = str2num(cell2mat(inputdlg(prompt, 'enter a number', 5)))
numberOfVertices = 7; % Fixed at 7 for this demo.
coordinates = rand(numberOfVertices, 2);
coordinates(1,:) = [0, 0]; % Force first coord to 0,0
coordinates(end+1,:) = [0, 0]; % Force last coord to 0,0
plot(coordinates(:,1), coordinates(:,2));
grid on;
patch(coordinates(:,1), coordinates(:,2),'r'); % Can use fill() also.
grid on;
Matthew
2013 年 4 月 7 日
Yep that works :) but I can't get the 'RGB::random()' to work as I want it to make a random colour each time.
Image Analyst
2013 年 4 月 7 日
Where did you see 'RGB::random()' ? I don't see it in the help as one of the 'ColorSpec' types.
Image Analyst
2013 年 4 月 7 日
If it ever was allowed, it's not now, not that I can find. See the help. You can use
fill(x, y, 'Color', rand());
if you want a random color.
Matthew
2013 年 4 月 7 日
For some reason this keeps giving me green.
patch(coordinates(:,1), coordinates(:,2),rand());
Image Analyst
2013 年 4 月 7 日
Sorry, you need 3 numbers. Try this:
randomColor = rand(1,3)
patch(coordinates(:,1), coordinates(:,2), randomColor); % Can use fill() also.
Image Analyst
2013 年 4 月 7 日
There is an 'xdir' property where you can reverse the direction. I think it's something like
set(gca, 'xdir', 'reverse');
or something like that. If this is solved, then mark the answer as "Accepted."
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Contour Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)