이 inpolygon
가 갈 수있는 방법입니다. 여기에 빠른 데모입니다, 자세한 내용은 주석을 참조하십시오
% Create 4 random rectangles, defined by their x and y coords in rectX and rectY.
% Each column defines a different rectangle.
sizes = randi([5,10], 2, 4)./10;
rectX = randi([1,5], 1, 4); rectX = [rectX; rectX; rectX + sizes(1,:); rectX + sizes(1,:)];
rectY = randi([1,5], 1, 4); rectY = [rectY; rectY + sizes(2,:); rectY + sizes(2,:); rectY];
% Create a random set of 1000 points for testing
points = [rand(1000, 1)*range(rectX(:))+min(rectX(:)), rand(1000, 1)*range(rectY(:))+min(rectY(:))];
% Set up logical matrix of test results
inrect = logical(zeros(size(points,1), size(rectX,2)));
% Plot the rectangles using patch
figure;
patch(rectX,rectY,'red')
% Hold on and plot all of the points as black dots
hold on;
plot(points(:,1),points(:,2),'.k');
% Loop through each rectangle, testing the points
for r = 1:size(rectX, 2)
% Test points using inpolygon, store results to inrect matrix
inrect(:,r) = inpolygon(points(:,1), points(:,2), rectX(:,r), rectY(:,r));
end
% Plot all points which are in any rectangle as blue circles
plot(points(any(inrect,2), 1), points(any(inrect,2), 2), 'bo');
결과 :
참고 이제 사각형 당 하나의 열에서 (사실 논리적 매트릭스 inrect
을 가지고, 점이 사각형 안에있을 때 점 당 하나의 행). 위 코드는 포인트가 인 경우 플로팅에 any
연산자를 사용하여 모든 직사각형을 나타냅니다.
"inpolygon"기능을 사용하십시오. – Ozcan
@Ozcan 감사합니다. 그게 내가 원하는거야! 나는 그런 기능이 존재한다고 생각조차 할 수 없었다! – Mechatronics
여기에서 MATLAB의 함수 목록을 확인할 수 있습니다. https://www.mathworks.com/help/matlab/functionlist.html – Ozcan