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).

10 次查看(过去 30 天)
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 个评论
Steven Lord
Steven Lord 2020-9-27
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.

请先登录,再进行评论。

回答(2 个)

Nabil Farah
Nabil Farah 2020-9-30
编辑:Nabil Farah 2020-9-30
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 2020-9-27
编辑:Ameer Hamza 2020-9-27
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.

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by