How can I tell MATLAB to stay within coordinates of a rectangle?
2 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Chunru
on 12 Apr 2022
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 Comments
See Also
Categories
Find more on Elementary Polygons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!