I have been looking around for a method to plot points outside a polygon's area(hexagon in my case). Here's the scenario that I want to achieve, I have a small hexagon located inside a big hexagon. The picture is as follows:
我一直在寻找一种方法来绘制多边形区域之外的点(在我的例子中是六边形)。这是我想要实现的场景,我在六角形内部有一个小六边形。图片如下:
In the picture, I created a small hexagon (whom area indicated in pale red) and generate a random points (three in my case) inside it using inpolygon
. Problem arise when I want to plot points (red triangles) in big hexagon (indicated in pale purple) without touching the small hexagon area. I have look around the net for this simple solutions 3 days to no avail.
在图片中,我创建了一个小六边形(其区域用淡红色表示)并使用inpolygon在其中生成随机点(在我的情况下为三个)。当我想要绘制大六角形的点(红色三角形)(用淡紫色表示)而不触及小的六边形区域时出现问题。我已经在网上寻找这个简单的解决方案3天无济于事。
I would really appreciate any helps or guidance I could get. Thank you so much!
我真的很感激我能得到的任何帮助或指导。非常感谢!
My code is as follows:
我的代码如下:
clear
clc
bighexagon = 20;
smallhexagon = 4;
axis_min = 0;
axis_max = 40;
axis([axis_min axis_max axis_min axis_max],'square');
hold on
L = linspace(30,390,7);
bhex_x = bighexagon * (1+cosd(L))';
bhex_y = bighexagon*(1+sind(L))';
L2 = linspace(30,390,7);
shex_x = smallhexagon * (1+cosd(L2))';
shex_y = smallhexagon * (1+sind(L2))';
plot(bhex_x,bhex_y,'LineWidth',3);
%---Move small hexagon into big hexagon
shex_vertices_x2(:,1) = shex_x + 16;
shex_vertices_y2(:,1) = shex_y + 16;
plot(shex_vertices_x2(:,1),shex_vertices_y2(:,1),'--k','LineWidth',3);
%---Plot points in small hexagon
no = 3;
point_x2 = (smallhexagon+20) - rand(1,9*no)*2*smallhexagon;
point_y2 = (smallhexagon+20) - rand(1,9*no)*2*smallhexagon;
inside = inpolygon(point_x2,point_y2,shex_vertices_x2,shex_vertices_y2);
point_x2 = point_x2(inside);
point_y2 = point_y2(inside);
idx2 = randperm(length(point_x2));
point_x2 = point_x2(idx2(1:no));
point_y2 = point_y2(idx2(1:no));
plot(point_x2,point_y2,'ro','MarkerSize',1.5,'LineWidth',1, ...
'MarkerFaceColor','r');
%---Plot points in big hexagon
no2 = 4;
point_x = (bighexagon+20) - rand(1,9*no2)*2*bighexagon;
point_y = (bighexagon+20) - rand(1,9*no2)*2*bighexagon;
inside2 = inpolygon(point_x,point_y,bhex_x,bhex_y);
point_x = point_x(inside2);
point_y = point_y(inside2);
idx = randperm(length(point_x));
point_x = point_x(idx(1:no2));
point_y = point_y(idx(1:no2));
plot(point_x,point_y,'g^','MarkerSize',3,'LineWidth',3, ...
'MarkerFaceColor','g');
3 个解决方案
#1
UPDATE
Thank to Hoki
's suggestion, I finally able to work it out.
感谢Hoki的建议,我终于能够解决了。
Note that I change this part inside the code:
请注意,我在代码中更改了此部分:
validpoint = inpolygon(point_x,point_y,bhex_x,bhex_y) & ~inpolygon(point_x,point_y,shex_vertices_x2,shex_vertices_y2);
validpoint = inpolygon(point_x,point_y,bhex_x,bhex_y)&~ingongon(point_x,point_y,shex_vertices_x2,shex_vertices_y2);
Hopefully, this clear the confusions and would help other users as well. I would like to thank Hoki
and xenoclast
for their help.
希望,这清除了混乱,并将帮助其他用户。我要感谢Hoki和xenoclast的帮助。
The code is as follows:
代码如下:
clear
clc
bighexagon = 20;
smallhexagon = 4;
axis_min = 0;
axis_max = 40;
axis([axis_min axis_max axis_min axis_max],'square');
hold on
L = linspace(30,390,7);
bhex_x = bighexagon * (1+cosd(L))';
bhex_y = bighexagon*(1+sind(L))';
L2 = linspace(30,390,7);
shex_x = smallhexagon * (1+cosd(L2))';
shex_y = smallhexagon * (1+sind(L2))';
plot(bhex_x,bhex_y,'LineWidth',3);
%---Move small hexagon into big hexagon
shex_vertices_x2(:,1) = shex_x + 16;
shex_vertices_y2(:,1) = shex_y + 16;
plot(shex_vertices_x2(:,1),shex_vertices_y2(:,1),'--k','LineWidth',3);
%---Plot points in small hexagon
no = 3;
point_x2 = (smallhexagon+20) - rand(1,9*no)*2*smallhexagon;
point_y2 = (smallhexagon+20) - rand(1,9*no)*2*smallhexagon;
inside = inpolygon(point_x2,point_y2,shex_vertices_x2,shex_vertices_y2);
point_x2 = point_x2(inside);
point_y2 = point_y2(inside);
idx2 = randperm(length(point_x2));
point_x2 = point_x2(idx2(1:no));
point_y2 = point_y2(idx2(1:no));
plot(point_x2,point_y2,'ro','MarkerSize',1.5,'LineWidth',1, ...
'MarkerFaceColor','r');
%---Plot points in big hexagon
no2 = 30;
point_x = (bighexagon+20) - rand(1,9*no2)*2*bighexagon;
point_y = (bighexagon+20) - rand(1,9*no2)*2*bighexagon;
%---As per Hoki's suggestion, it ensure the points are outside the small hexagon
validpoint = inpolygon(point_x,point_y,bhex_x,bhex_y) & ...
~inpolygon(point_x,point_y,shex_vertices_x2,shex_vertices_y2);
point_x = point_x(validpoint);
point_y = point_y(validpoint);
idx = randperm(length(point_x));
point_x = point_x(idx(1:no2));
point_y = point_y(idx(1:no2));
plot(point_x,point_y,'g^','MarkerSize',3,'LineWidth',3, ...
'MarkerFaceColor','g');
#2
If your inner hexagon is defined by the vertices then you could use inpolygon
(link) to test whether a given point is inside it or not.
如果您的内六边形由顶点定义,那么您可以使用inpolygon(link)来测试给定点是否在其中。
#3
Pls check by adding following two lines after inside2 = inpolygon(point_x,point_y,bhex_x,bhex_y);
请在inside2 = inpolygon(point_x,point_y,bhex_x,bhex_y)之后添加以下两行进行检查;
in1 = inpolygon(point_x,point_y,shex_vertices_x2,shex_vertices_y2);
inside2= logical(inside2-in1);
#1
UPDATE
Thank to Hoki
's suggestion, I finally able to work it out.
感谢Hoki的建议,我终于能够解决了。
Note that I change this part inside the code:
请注意,我在代码中更改了此部分:
validpoint = inpolygon(point_x,point_y,bhex_x,bhex_y) & ~inpolygon(point_x,point_y,shex_vertices_x2,shex_vertices_y2);
validpoint = inpolygon(point_x,point_y,bhex_x,bhex_y)&~ingongon(point_x,point_y,shex_vertices_x2,shex_vertices_y2);
Hopefully, this clear the confusions and would help other users as well. I would like to thank Hoki
and xenoclast
for their help.
希望,这清除了混乱,并将帮助其他用户。我要感谢Hoki和xenoclast的帮助。
The code is as follows:
代码如下:
clear
clc
bighexagon = 20;
smallhexagon = 4;
axis_min = 0;
axis_max = 40;
axis([axis_min axis_max axis_min axis_max],'square');
hold on
L = linspace(30,390,7);
bhex_x = bighexagon * (1+cosd(L))';
bhex_y = bighexagon*(1+sind(L))';
L2 = linspace(30,390,7);
shex_x = smallhexagon * (1+cosd(L2))';
shex_y = smallhexagon * (1+sind(L2))';
plot(bhex_x,bhex_y,'LineWidth',3);
%---Move small hexagon into big hexagon
shex_vertices_x2(:,1) = shex_x + 16;
shex_vertices_y2(:,1) = shex_y + 16;
plot(shex_vertices_x2(:,1),shex_vertices_y2(:,1),'--k','LineWidth',3);
%---Plot points in small hexagon
no = 3;
point_x2 = (smallhexagon+20) - rand(1,9*no)*2*smallhexagon;
point_y2 = (smallhexagon+20) - rand(1,9*no)*2*smallhexagon;
inside = inpolygon(point_x2,point_y2,shex_vertices_x2,shex_vertices_y2);
point_x2 = point_x2(inside);
point_y2 = point_y2(inside);
idx2 = randperm(length(point_x2));
point_x2 = point_x2(idx2(1:no));
point_y2 = point_y2(idx2(1:no));
plot(point_x2,point_y2,'ro','MarkerSize',1.5,'LineWidth',1, ...
'MarkerFaceColor','r');
%---Plot points in big hexagon
no2 = 30;
point_x = (bighexagon+20) - rand(1,9*no2)*2*bighexagon;
point_y = (bighexagon+20) - rand(1,9*no2)*2*bighexagon;
%---As per Hoki's suggestion, it ensure the points are outside the small hexagon
validpoint = inpolygon(point_x,point_y,bhex_x,bhex_y) & ...
~inpolygon(point_x,point_y,shex_vertices_x2,shex_vertices_y2);
point_x = point_x(validpoint);
point_y = point_y(validpoint);
idx = randperm(length(point_x));
point_x = point_x(idx(1:no2));
point_y = point_y(idx(1:no2));
plot(point_x,point_y,'g^','MarkerSize',3,'LineWidth',3, ...
'MarkerFaceColor','g');
#2
If your inner hexagon is defined by the vertices then you could use inpolygon
(link) to test whether a given point is inside it or not.
如果您的内六边形由顶点定义,那么您可以使用inpolygon(link)来测试给定点是否在其中。
#3
Pls check by adding following two lines after inside2 = inpolygon(point_x,point_y,bhex_x,bhex_y);
请在inside2 = inpolygon(point_x,point_y,bhex_x,bhex_y)之后添加以下两行进行检查;
in1 = inpolygon(point_x,point_y,shex_vertices_x2,shex_vertices_y2);
inside2= logical(inside2-in1);