필터 지우기
필터 지우기

how to fill a specific region with color if that's region contain my target point?

조회 수: 5 (최근 30일)
hy guys.
I would like to fill a specific region automatically with the color red in my plot, if that's region contains my target point.
Any idea how to do that's ?
Thank you in advance
my code:
clear all
clc
x = [1 3 4 3 1 0 1];
y = [0 0 2 4 4 2 0];
plot(x,y,'k')
hold on
plot(x+2,y,'k')
plot(x,y+2,'k')
x_target=3;
y_target=3;
plot(x_target,y_target,'or');

채택된 답변

Image Analyst
Image Analyst 2022년 11월 15일
x = [1 3 4 3 1 0 1];
y = [0 0 2 4 4 2 0];
plot(x,y,'k')
hold on
plot(x+2,y,'k')
plot(x,y+2,'k')
x_target=3;
y_target=3;
plot(x_target,y_target,'or');
% Generate polyshapes
p1 = polyshape(x, y);
p2 = polyshape(x+2, y);
p3 = polyshape(x, y+2);
% Find overlap
p = p1.intersect(p2);
p = p.intersect(p3);
plot(p);
  댓글 수: 3
Image Analyst
Image Analyst 2022년 11월 15일
It's more complicated. You have to use inpolygon to check if the point is inside or not. Once you've found out which polygon it's inside, you have to subtract or add in the other polygons depending if they contain the point or not. Here's a start
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 22;
markerSize = 20;
x = [1 3 4 3 1 0 1];
y = [0 0 2 4 4 2 0];
x2 = x+2;
y2 = y + 2;
plot(x, y, 'k-', 'LineWidth', 2)
hold on
plot(x2, y, 'b-', 'LineWidth', 2)
darkGreen = [0, 0.5, 0];
plot(x, y2, '-', 'Color', darkGreen, 'LineWidth', 2)
x_target = 4.5;
y_target = 2.5;
plot(x_target,y_target,'r.', 'MarkerSize', 30);
grid on;
legend('p1', 'p2', 'p3', 'target point');
% Generate polyshapes
p1 = polyshape(x, y);
p2 = polyshape(x+2, y);
p3 = polyshape(x, y+2);
% Find which polygon contains the point.
checkedThisPolygon = [false, false, false];
itsInside = inpolygon(x_target, y_target, x, y)
checkedThisPolygon(1) = true;
if itsInside
p = p1;
else
% It's not in polygon 1. Check to see if it's in polygon #2.
itsInside = inpolygon(x_target, y_target, x2, y);
checkedThisPolygon(2) = true;
if itsInside
p = p2;
else
% It's not in polygon 2. Check to see if it's in polygon #3.
itsInside = inpolygon(x_target, y_target, x, y2);
checkedThisPolygon(3) = true;
if itsInside
p = p3;
end
end
end
% p = p.intersect(p3);
plot(p);
It'll take more work to finish than I have time to donate to you, so good luck.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Elementary Polygons에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by