MATLAB programme for checking if a given point (x, y) is within the square with a bottom left corner at (p, q) and side s. The input arguments are x, y, p, q and s, and the output is either true (in the square) or false (not in the square).

29 views (last 30 days)
Write a MATLAB programme for checking if a given point (x, y) is within the square with a bottom left corner at (p, q) and side s. The input arguments are x, y, p, q and s, and the output is either true (the point is in the square) or false (the point is not in the square).
Find the answer for: x = 15, y = 14, p = 13, q = 2, s = 11.
  1 Comment
Steven Lord
Steven Lord on 27 Sep 2020
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the MATLAB Onramp tutorial (https://www.mathworks.com/support/learn-with-matlab-tutorials.html) to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.

Sign in to comment.

Answers (2)

Nabil Farah
Nabil Farah on 30 Sep 2020
Edited: Nabil Farah on 30 Sep 2020
this might help
function result= IsWithinSquare()
%%The input arguments are x, y, p, q and s, and the output is either true (the point is in the square) or false (the point is not in the square).
% x = 15;
% y = 14;
% p = 13;
% q = 2;
% s = 11;
clc, close, clear
x = input ('the first point (x) = ');
y = input ('the second point(y) = ');
p = input ('bottom left corner point (p) = ');
q = input ('bottom left corner point (q) = ');
s = input ('the side (s) = ');
% if the (x) coordinate lies within bottom_left (p) and
% bottom_left(p)+side (S)
%and y coordinate lies within bottom_left(q) and bottom_left(q)+side(S) then the point (x,y) is inside the
% square and set result to true else set result to false
% The edges/corners are treated as part of the square.
if(((x >= p) && (x <= (x+s))) && (((y >=q) && y <= (q + s))))
result = true;
% disp('the point ',x,y,'is in the square')
else
result = false;
disp(' the point(x,y)is not in the square')
end

Ameer Hamza
Ameer Hamza on 27 Sep 2020
Edited: Ameer Hamza on 27 Sep 2020
If this is not a homework question, then directly use inpolygon(): https://www.mathworks.com/help/matlab/ref/inpolygon.html. Otherwise, you will need to develop the logical steps to solve this problem.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!