이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Randomly generated complex polygon with user input
조회 수: 2 (최근 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
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에서 Surfaces, Volumes, and Polygons에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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 (한국어)
