Re: mathmatical question..

new topic     » goto parent     » topic index » view thread      » older message » newer message

> Hi everyone!
>
> Can anyone give me nice answer over the problem below;
>
> --- here goes problem,
>
> Assumes the polygon with arbitrary numbers of points is drawn on the
> pixel-graphics screen, for example;
>
> polygon(RED,1,{{100,200},{200,200},......,}
>
> Problem is:
> How can I know whether the mouse pointer is INSIDE the polygon or not,
> at the time the mouse button is pressed?

Here's a function that will do this. It computes the number of line
intersections mathematically, and will work for any polygon.

Regards,
              Michael Bolin
---------------------------------------------------
function inside(sequence point,sequence points)
    atom x1,y1,x2,y2,x,y,min,max
    integer intersections

    intersections=0
    x=point[1]
    y=point[2]
    x2=points[length(points)][1]
    y2=points[length(points)][2]
    for t=1 to length(points) do
        point=points[t]
        x1=point[1]
        y1=point[2]
        if y1!=y2 then
            min=y1
            max=y2
            if y2<y1 then
                min=y2
                max=y1
            else
                min=y1
                max=y2
            end if
            if y>min and y<=max then
                if x<=x1+(x2-x1)/(y2-y1)*(y-y1) then
                    intersections=intersections+1
                end if
            end if
        end if
        x2=x1
        y2=y1
    end for
    return remainder(intersections,2)
end function
------------------------------------

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu