From: michael scheinfeild on 22 Feb 2010 15:08 i try check if point inside polygon using sum of all angles inside ... =0 it not work in all case some idea here is the code =========== clc; xy=[ 0.3 0.4 0.3 0.6 0.5 0.6 0.5 0.4 %0.3 0.4 i try to put first point duplicate it not help ]; m=[0.4 0.5]; mx=m(1);my=m(2); x=xy(:,1);y=xy(:,2); alpha1=(180/pi)*(atan2((my-y),(mx-x))); % i try atan not help alpha1(alpha1<0)=alpha1(alpha1<0)+360; if(rem(sum((alpha1)),360)<0.01) disp('in polygon') else disp('out') end kx=1;ky=m(2); beta1=(180/pi)*(atan2((ky-y),(kx-x))); beta1(beta1<0)=beta1(beta1<0)+360; if(rem(sum((beta1)),360)<0.01) disp('in polygon') else disp('out') end figure(1),plot(x,y,'ro');hold on;line([x ],[y ]);plot(mx,my,'ks'); plot(kx,ky,'gs');
From: Matt J on 22 Feb 2010 15:49 "michael scheinfeild" <yomholedet(a)gmail.com> wrote in message <hluo75$lus$1(a)fred.mathworks.com>... > i try check if point inside polygon using sum of all angles inside ... =0 > it not work in all case some idea > here is the code > =========== Here's a better way. Note the example in the help section: function [A,b]=vert2con_special(a) %Finds the expression of a 2D polygon as a set of linear inequalities from %a set of vertices % % [A,b]=vert2con_special(a) % %in: % % a: Nx2 matrix whos rows are polygon vertex coordinates. The rows must % must be ordered so that adjacent rows correspond to adjacent vertices % (which will trivially be the case for triangles). % %out: % % [A,b]: Nx1 vector and Nx2 matrix such that A*x<=b if and only if x is a point % inside the polygon % % %Example: Detect whether a point is in a triangle % % %%%data % Vertices=[0 0; 1 0; 0 1]; %vertices of a triangle % p1=[.5;.25]; %a point inside the triangle % p2=[.5;-.25];%a point outside the triangle % % [A,b]=vert2con_special(Vertices); % % >>all(A*p1<=b) %Test if p1 is in the triangle. % % ans = % % 1 % % >>all(A*p2<=b) %Test if p2 in the triangle. % % ans = % % 0 centroid=mean(a).'; R=[0 1; -1 0]; A=diff([a;a(1,:)])*R; b=sum(A.*a,2); ii=(A*centroid>=b); b(ii)=-b(ii); A(ii,:)=-A(ii,:);
From: ImageAnalyst on 22 Feb 2010 16:04 Or, simply use the built-in function "inpolygon()." Michael - I'm not sure if you had a question or you were just posting some code that you think might help others, despite not working in all the cases.
From: Matt J on 22 Feb 2010 16:32 ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <d0b52e1d-dfdf-4294-b3b4-7d51075f29e7(a)o30g2000yqb.googlegroups.com>... > Or, simply use the built-in function "inpolygon()." ===== One could use inpolygon(), but it has been observed in other posts that inpolygon is sub-optimal in terms of speed, because it has a lot of overhead designed to make it applicable to both convex and non-convex polygons. So, if this is going to be speed-critical code, that should be kept in mind.
From: michael scheinfeild on 23 Feb 2010 01:34
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <hlut4k$jot$1(a)fred.mathworks.com>... > ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <d0b52e1d-dfdf-4294-b3b4-7d51075f29e7(a)o30g2000yqb.googlegroups.com>... > > Or, simply use the built-in function "inpolygon()." > ===== > > One could use inpolygon(), but it has been observed in other posts that inpolygon is sub-optimal in terms of speed, because it has a lot of overhead designed to make it applicable to both convex and non-convex polygons. So, if this is going to be speed-critical code, that should be kept in mind. thanks i just wanted nother way of computing the inpolygon using the internal angles. also what is the theory of ax=b (i know least squares) with relation to point in polygon what is the mean of a,b matrices |