How can I tell MATLAB to stay within coordinates of a rectangle?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Chunru
el 12 de Abr. de 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 comentarios
Ver también
Categorías
Más información sobre Elementary Polygons en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!