How can I tell MATLAB to stay within coordinates of a rectangle?
조회 수: 2 (최근 30일)
이전 댓글 표시
I am trying to create a room with a couple obstacles in it. I want the obstacles to stay inside the room and not overlap each other. There can be anywhere between 1 and 3 obstacles that a randomly generated.
clear;
clc;
fprintf('Please select one of the following options:\n New Room\n Saved Room');
Selection1 = input('\n\n ','s');
if Selection1 == 'New Room' | Selection1 == 'New room' | Selection1 == 'new room'
roomname = input('Please enter the name of the room: ', 's');
roomwidth = randi([5 20]);
roomlength = randi([5 20]);
rectangle('Position',[0 0 roomwidth roomlength])
axis([-1 (roomwidth+1) -1 (roomlength+1)])
obstaclecount = randi([1 3]);
disp(obstaclecount);
for i = 1:obstaclecount
x = randi([1 5]);
y = randi([1 5]);
a = randi([1 5]);
b = randi([1 5]);
wow = rectangle('Position',[x y a b], 'LineStyle','--');
end
end
So doing this creates a room with a couple random obstacle inside but they are not confined within any boundries so sometimes it looks good, but sometimes the obstacles go outside the room or overlap each other.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/961415/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/961420/image.jpeg)
댓글 수: 0
답변 (1개)
Chunru
2022년 4월 12일
You can use polygon/polyshape for room and obstacles. Then use "overlap" function to test if obstacles are inside the room.
doc overlaps
overlaps
Determine whether polyshape objects overlap
Syntax
Description
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Elementary Polygons에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!