28 Apr 2021

How to test if a point is inside a sector?

收藏到CSDN网摘

If we have a sector (partial circle) defined by the centre C, radius R, starting angle point S, and ending angle point E. How can we test if a given point P is inside or outside the sector? The idea is to define 2 function: the one is to test if one vector can be obtained by rotating from another vector clockwise (or anti-clockwise if needed); the other is to test if the distance from the point to the centre is less than or equal to the radius. Then, the problem becomes 3 tests: (1) clockwise rotation from starting vector to the point; (2) anti-clockwise rotation from the ending vector to the point; and (3) the distance from P to C is less than R.
% 2 vectors if v2 can rotate clockwise to v1
isClockwise = @(v1,v2) -v1(1)*v2(2) + v1(2)*v2(1) > 0;
% close enough to the centre
isInside = @(p,c,r) pdist([p;c]) <= r;
% is P inside the sector defined by centre C, radisu R and 2 points on the
% sector edge S and E
withinSector = @(p,c,r,s,e) isClockwise(s-c,p-c) & ~isClockwise(e-c,p-c) & ...
    isInside(p,c,r);

No comments :

Post a Comment