Hi Alex,

thanks a lot for your solution. I came up with another solution, using a
"scanline approach", following a horizontal scanline (with a random
vertical coordinate within the polygon's "drawrect")  from outside the
polygon and then finding the first line crossing with help of getPixel
(the first point which doesn't have the backgroundcolor, and then taking
the first point which has the background color again, which now should
be inside the polygon, as long as the line crossing wasn't a (and was
not too close to a) corner point.
I will compare both approaches with random ploygons, and use the one
producing less errors.

Thanks again,
Valentin

Alex da Franca wrote:
> At 22:38 Uhr +0100 12.12.2003, Valentin Schmidt wrote:
>> hi list,
>>
>> is there anybody out there who is good at geometry? I need a solution
>> for the following problem:
>> I have a list of points [p1,p2,...] which form a polygon. The points
>> can be arbitrary, the only restriction is that the lines between the
>> points (where lines are the connections from point n to point n+1)
>> don't cross each other.
>
>
> my solution may seem clumsy and there sure is an easier one...
> ...but since nobody answered...
>
>
> below is a handler which checks if a given point is inside a polygon.
>
> now I think we can just take any of the lines of
> the polygon and check a point 'left' and 'right'
> to this line.
> below I do it with a point with the distance of 1
> ( => point(cos(angle), sin(angle))) if you know
> that your polygon won't have very small angles
> you can simply multiply 'offs' by a higher value
> in order to get a point further away from the
> edge line, which may floodfill cause to fail when
> it 'touches' the edge line.
>
>
> pts => linear list with points
>
>
> center = (pts[1] + pts[2]) / 2.0 -- point between 1. and 2. point
>
> slope = pts[2] - pts[1]
> angle = atan(float(slope [2]), float(slope [1]))
> + (pi() / 2) -- vector angle + 90�
>
> offs = point(cos(angle), sin(angle))
> checkpoint = center + offs
>
> if polygonContainsPoint(pts, checkpoint[1],
> checkpoint[2]) then return checkpoint
> else return center - offs
>
>
>
> a kind collegue once posted the following
> handler, which works so far (with the exception
> of crossing lines)
>
> unfortunately I can't remember where I picked it
> up once and only have the snippet without the
> name.
> so forgive me, author of these lines, not being
> able to mention your name, and instead take my
> kudos, since this handler does work very well
> (with some cosmetic changes from my part) in many
> of my movies !
>
> :-)
>
> --
>
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> -- xxxxxxxxxxxxxxxxxx check if point(<x>, <y>) is
> inside of a poligon defined by a list of points
> <pts>
>
> on polygonContainsPoint pts, x, y
>    j = pts.count
>    tot = pts.count
>    containsP = 0
>
>    repeat with i = 1 to tot
>      yI = pts[i][2]
>      yJ = pts[j][2]
>      xI = pts[i][1]
>      if ((yI <= y) and (y < yJ)) or ((yJ <= y) and (y < yI)) then
>        if x < ((pts[j][1] - xI) * (y - yI) / float(yJ - yI)) + xI then
>          containsP = not containsP
>        end if
>      end if
>      j = i
>    end repeat
>
>    return containsP
> end

[To remove yourself from this list, or to change to digest mode, go to 
http://www.penworks.com/lingo-l.cgi  To post messages to the list, email [EMAIL 
PROTECTED]  (Problems, email [EMAIL PROTECTED]). Lingo-L is for learning and helping 
with programming Lingo.  Thanks!]

Reply via email to