如何编程判定 点 和 四边形 的位置关系。
10 views (last 30 days)
Show older comments
求高手指教,如何用MATLAB编程,判断平面上的一个已知点在一个四边形的里面还是外面呢?
比如点P(x,y),四边形的控制顶点分别为:A(x1,y1) , B(x2,y2) , C(x3,y3) , D(x4,y4) 。应该怎么编写代码判断点P是否在四边形围成的区域里面呢?
0 Comments
Accepted Answer
vatiha
on 20 Nov 2022
inpolygon
给个例子。
%%
% 要判断的点
p1 =[0.5, 0];
p2 =[0.5, 0.4];
p3 =[0.5, 1];
p4 =[1.5, 1.5];
p5 =[-0.2, 4];
xv = [0 0 1 1] % 多边形的横坐标 向量
yv = [0 1 1 0] % 多边形的横坐标 向量
X = [p1(1),p2(1),p3(1),p4(1),p5(1)] ; % 要判断点的横坐标
Y = [p1(2),p2(2),p3(2),p4(2),p5(2)] ; % 要判断点的纵坐标
IN = inpolygon(X,Y,xv,yv) %在多边形线上 也称在内部
%%
figure,
h = fill(xv,yv,'r')
axis([-0.5 1.6, -0.2, 4.5])
hold on
h2 = plot(X,Y,'o','markersize',10)
set(h2, 'markerfacecolor',[0 1 0])
str = {'out','in(/on)'};
for i =1: length(X)
text(X(i),Y(i)+.2,str(IN(i)+1))
end
[attach]153728[/attach]
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!