MATLAB: How to take a value if it goes through a straight line or another specific point

homeworkMATLABpoints

I have a plot that has a few straight lines. I want to take 2 point from the figure using ginput. Now I have a code to get all the points withing that 2 points using linspace command. Now I am looking for a way that when i take those initial 2 points from mouse , if the points within the 2 points ever go through the straight lines in the plot it will give me some sort of value or signal. It can be some sort of if-else command. Can anyone help me ?

Best Answer

Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
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')
% Room 1
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;
% Room 2
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;
% Room 3
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;
% Room 4
x1=0;
y1=6;
x2=3;
y2=10;
room4x = [x1, x2, x2, x1, x1];
room4y = [y1, y1, y2, y2, y1];
% Ask the user to click two lines. You can use improfile() or imdistline() of you have the Image Processing Toolbox.
uiwait(helpdlg('Click on two points.'))
[xUser, yUser] = ginput(2) %this gives me the 2 points
plot(xUser, yUser, 'ro--', 'LineWidth', 2);
% Make flag array to find out if the point is in there.
% One row for every room. We don't need to check if it's in the entire house since we assume it will be.
inRoom = false(3, 2); % Column 1 for point 1, column 2 for point 2.
% See if points 1 and 2 are in room 1.
if inpolygon(xUser(1), yUser(1), room1x, room1y)
% The first point is in the whole house

inRoom(1, 1) = true;
end
if inpolygon(xUser(2), yUser(2), room1x, room1y)
% The first point is in the whole house
inRoom(1, 2) = true;
end
% See if points 1 and 2 are in room 2.
if inpolygon(xUser(1), yUser(1), room2x, room2y)
% The first point is in room 2.
inRoom(2, 1) = true;
end
if inpolygon(xUser(2), yUser(2), room2x, room2y)
% The second point is in room 2.
inRoom(2, 2) = true;
end
% See if points 1 and 2 are in room 3.
if inpolygon(xUser(1), yUser(1), room3x, room3y)
% The first point is in room 3.
inRoom(3, 1) = true;
end
if inpolygon(xUser(2), yUser(2), room3x, room3y)
% The second point is in room 3.
inRoom(3, 2) = true;
end
% Display results:
inRoom
% Find out if both points are in the same room
roomCount = sum(inRoom, 2)
if any(roomCount == 2)
% Then both points are in the same room
message = 'Both points are in the same room';
uiwait(helpdlg(message));
return;
end
% Find out where point 1 is by inspecting column 1 for a 1.
point1room = find(inRoom(:, 1))
% Find out where point 2 is by inspecting column 2 for a 1.
point2room = find(inRoom(:, 2))
if isempty(point1room)
% In whole house but not in any room. Just say it's room 0.
point1room = 0;
end
if isempty(point2room)
% In whole house but not in any room.
point2room = 0;
end
message = sprintf('Point 1 is in room %d, and Point 2 is in room %d', ...
point1room, point2room);
uiwait(helpdlg(message));
0000 Screenshot.png
Related Question