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
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
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
댓글 수: 0
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!