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

7 vues (au cours des 30 derniers jours)
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');

Réponse acceptée

Image Analyst
Image Analyst le 15 Nov 2022
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 commentaires
Image Analyst
Image Analyst le 15 Nov 2022
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Elementary Polygons dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by