Inside or outside a convex polygon?
We want to find if a point is outside or inside a convex polygon. The
reasoning is as follows:
If the point to be examined is inside the polygon, the maximum angle
from the point to two of the vertexes has to be greater than p.
The verb within is used to this purpose:
within = 4 : 0 NB.x is point, y the vertexes
reg =:y-x
ang =:v360 "0 (1{"1 *. reg)
if. (ang>o.1) do.ang =:(o.2)-ang end.
(range ang)>o.1
)
where
v360 =: 3 : 'if. (y<0) do. (o.2)+y else. y end.'
range =: >./ - <./
A simple example:
The vertexes of the polygon are given by
vertexes=: 3j1 1j3 0j2 2j0
2j1 within vertexes
1
2j0 within vertexes
0
In a previous message to JForum (Jan 11- 19:55 "manuscript") I presented
a script to find the vertexes of a closed polygon when a number of
points are given.. The script gave as result the vertexes:
bpts=:.2j3.2 4j0.5 5.5j0.5 9.8j4.7 7.6j7.9 4.6j8.4 3j6.1
To see if a point is inside/outside::
5j9 within bpts
0 (inside!)
5j7 within bpts
1 (outside!)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm