Plot to avoid certain regions in 2D plot

조회 수: 1 (최근 30일)
Ananya Malik
Ananya Malik 2018년 11월 2일
댓글: Ananya Malik 2018년 11월 2일
I have to plot some random points in 500 x 500 area by avoiding certain area (poly0 and poly1 in the example below) in the 2D-plot.
x = 350.5; y = 350.5; r = 50;
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
poly0 = polyshape(xunit(1:end-1),yunit(1:end-1));
plot(poly0);
hold on;
ploy1 = polyshape([100 100 200 200],[100 200 200 100]); %square
plot(poly1);
hold on
loc = randi([1,500],50, 2);
If I do this, random points overlap the poly0 and poly1 areas. Can anyone help? TIA.
  댓글 수: 1
KSSV
KSSV 2018년 11월 2일
x = 350.5; y = 350.5; r = 50;
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
poly0 = polyshape(xunit(1:end-1),yunit(1:end-1));
plot(poly0);
hold on;
poly1 = polyshape([100 100 200 200],[100 200 200 100]); %square
plot(poly1);
hold on
loc = randi([1,500],50, 2);
There was a typo error. What you want to avoid?

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

채택된 답변

KSSV
KSSV 2018년 11월 2일
편집: KSSV 2018년 11월 2일
Read about inpolygon
x = 350.5; y = 350.5; r = 50;
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
poly0 = polyshape(xunit(1:end-1),yunit(1:end-1));
plot(poly0);
hold on;
poly1 = polyshape([100 100 200 200],[100 200 200 100]); %square
plot(poly1);
hold on
loc = randi([1,500],50, 2);
% avoid poly0
idx = inpolygon(loc(:,1),loc(:,2),poly0.Vertices(:,1),poly0.Vertices(:,2)) ;
loc(idx,:) = NaN ;
% avoid poly1
idx = inpolygon(loc(:,1),loc(:,2),poly1.Vertices(:,1),poly1.Vertices(:,2)) ;
loc(idx,:) = NaN ;
plot(loc(:,1),loc(:,2),'.r')
  댓글 수: 1
Ananya Malik
Ananya Malik 2018년 11월 2일
Thanks a lot.. Exact solution as always (y).

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

추가 답변 (1개)

madhan ravi
madhan ravi 2018년 11월 2일
how about?
plot(loc(loc~=all(poly0.Vertices) & loc~=all(poly1.Vertices)))
  댓글 수: 1
Ananya Malik
Ananya Malik 2018년 11월 2일
편집: Ananya Malik 2018년 11월 2일
Thank you for the ans however, this is giving me 1D array of results. Cannot be plotted.

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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by