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

24 ビュー (過去 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 Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by