clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
x1=0;
x2=10;
y1=0;
y2=10;
wholeHousex = [x1, x2, x2, x1, x1];
wholeHousey = [y1, y1, y2, y2, y1];
plot(wholeHousex, wholeHousey, 'b-', 'LineWidth', 3);
xlabel('Length')
ylabel('Width')
hold on;
text(3,5,'total house W-L')
x1=0;
y1=0;
x2=2;
y2=3;
room1x = [x1, x2, x2, x1,x1];
room1y = [y1, y1, y2, y2,y1];
plot(room1x, room1y, 'r', 'LineWidth', 3);
hold on;
x1=2;
y1=0;
x2=5;
y2=3;
room2x = [x1, x2, x2, x1,x1];
room2y = [y1, y1, y2, y2,y1];
plot(room2x, room2y, 'g', 'LineWidth', 3);
hold on;
x1=5;
y1=0;
x2=10;
y2=3;
room3x = [x1, x2, x2, x1,x1];
room3y = [y1, y1, y2, y2,y1];
plot(room3x, room3y, 'k', 'LineWidth', 3);
hold on;
x1=0;
y1=6;
x2=3;
y2=10;
room4x = [x1, x2, x2, x1, x1];
room4y = [y1, y1, y2, y2, y1];
uiwait(helpdlg('Click on two points.'))
[xUser, yUser] = ginput(2)
plot(xUser, yUser, 'ro--', 'LineWidth', 2);
inRoom = false(3, 2);
if inpolygon(xUser(1), yUser(1), room1x, room1y)
inRoom(1, 1) = true;
end
if inpolygon(xUser(2), yUser(2), room1x, room1y)
inRoom(1, 2) = true;
end
if inpolygon(xUser(1), yUser(1), room2x, room2y)
inRoom(2, 1) = true;
end
if inpolygon(xUser(2), yUser(2), room2x, room2y)
inRoom(2, 2) = true;
end
if inpolygon(xUser(1), yUser(1), room3x, room3y)
inRoom(3, 1) = true;
end
if inpolygon(xUser(2), yUser(2), room3x, room3y)
inRoom(3, 2) = true;
end
inRoom
roomCount = sum(inRoom, 2)
if any(roomCount == 2)
message = 'Both points are in the same room';
uiwait(helpdlg(message));
return;
end
point1room = find(inRoom(:, 1))
point2room = find(inRoom(:, 2))
if isempty(point1room)
point1room = 0;
end
if isempty(point2room)
point2room = 0;
end
message = sprintf('Point 1 is in room %d, and Point 2 is in room %d', ...
point1room, point2room);
uiwait(helpdlg(message));
Best Answer