> -----Oorspronkelijk bericht-----
> Van: [email protected] [mailto:programming-
> [email protected]] Namens Dan Bron
> Verzonden: vrijdag 16 april 2010 17:02
> Aan: 'Programming forum'
> Onderwerp: [Jprogramming] Polygon containment
> 
> Guys,
> 
> I need to check whether a point falls within an arbitrary polygon.
> 
(...)



I prefer to solve this problem, assuming the polygon is convex.

pip=: 4 : 0
 NB. point in polygon, x is point
 NB. y is polygon with 2 -: {:$y, such that _2[(,{.)y are edges
 NB. 1 if in polygon, 0 if on edge or vertex, _1 if outside
 E=. _1 ,.~ (%."(2) 2[\(,{.)y) +/ . * 1 1
 s=. * E +/ . *"(1) _2|.y ,. 1
 t=. * E +/ . *"(1) x , 1
 <: (+: s -: t) + (0 e. t)
)

V=: _1 _1, 1 _1, 2 2 ,: _1 1

   (3 4, _1 0 ,: 0 0) pip"1 _ V
_1 0 1


Some efficiency improvements may be possible.


R.E. Boss

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to