1. Re: mathmatical question... to Jiri - Reply

Hi everybody,

I had a second (longer) look at my inside() function last night, and as a result
I have to ingests a bit of a humble pie. While the simple version below works
well for all interior points, it leaves the points on the polygon's periphery
indeterminate. The longer version will include all such points. Jiri


-- inpoly.ex -------------------------------------------------------------------
-- j.babor at gns.cri.nz
--------------------------------------------------------------------------------

include graphics.e
include get.e

object junk
sequence s,tp
integer key

function simple_inside(sequence p, sequence P)
   atom x
   integer count,n
   n=length(P)
   P=append(P,P[1])
   count=0
   for i=1 to n do
      if (P[i][2]<=p[2] and P[i+1][2]>p[2]) or (P[i][2]>p[2] and P[i+1][2]<=p[2]
) then
         x=P[i][1]+(p[2]-P[i][2])*(P[i+1][1]-P[i][1])/(P[i+1][2]-P[i][2])
         if x<p[1] then
            count=count+1
         end if
      end if
   end for
   return remainder(count,2)  -- return 1 (true) if count is odd
end function

function inside(sequence p, sequence P)
   atom x
   integer count,n
   n=length(P)
   P=append(P,P[1])
   count=0
   for i=1 to n do
      if not compare(p,P[i]) then
         return 1    -- include all vertices
      elsif p[2]=P[i][2] and p[2]=P[i+1][2] and ((p[1]>P[i][1] and
         p[1]<P[i+1][1]) or (p[1]<P[i][1] and p[1]>P[i+1][1])) then
         return 1    -- include all points on horizontal polygon sides
      end if
      if (P[i][2]<=p[2] and P[i+1][2]>p[2]) or (P[i][2]>p[2] and P[i+1][2]<=p[2]
) then
         x=P[i][1]+(p[2]-P[i][2])*(P[i+1][1]-P[i][1])/(P[i+1][2]-P[i][2])
         if x=p[1] then
            return 1 -- include all points on non-horizontal polygon sides
         elsif x<p[1] then
            count=count+1
         end if
      end if
   end for
   return remainder(count,2)  -- return 1 (true) if count is odd
end function

-- main ------------------------------------------------------------------------

junk=graphics_mode(18)

   {100,180},{205,190},{190,200},{40,240},{100,240},{200,240},{40,280},
   {80,280},{160,280},{180,280},{60,320}}

0,180}}
polygon(1,1,s)
for i=1 to length(tp) do
   pixel(15,tp[i])
   print(1,inside(tp[i],s))
   if not remainder(i,4) then puts(1,"\n") end if
end for
key = wait_key()
junk=graphics_mode(-1)

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu