Borrar filtros
Borrar filtros

How can I tell MATLAB to stay within coordinates of a rectangle?

2 visualizaciones (últimos 30 días)
Abdul-Aleem Mevawala
Abdul-Aleem Mevawala el 12 de Abr. de 2022
Respondida: Chunru el 12 de Abr. de 2022
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.

Respuestas (1)

Chunru
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
TF = overlaps(poly1,poly2) returns a logical array whose elements are 1 when the corresponding element pairs of two polyshape arrays with compatible sizes overlap. TF(i,j) is 1 when the ith polyshape in poly1 overlaps the jth polyshape in poly2.
TF = overlaps(polyvec) returns a logical array whose elements are 1 (true) when the corresponding element pairs of a vector of polyshape objects overlap. TF(i,j) is 1 when the ith polyshape in polyvec overlaps the jth polyshape.

Categorías

Más información sobre Elementary Polygons en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by