# eXorithm – Execute Algorithm: View / Run Algorithm point_in_polygon

`functionÂ point_in_polygonÂ (\$point,Â \$polygon_points{Â Â //Â checkÂ pointsÂ Â ifÂ ((count\$polygon_points)%2)!=0)Â {Â Â Â Â throwÂ newÂ Exception'TheÂ pointsÂ mustÂ beÂ aÂ listÂ likeÂ x1,Â y1,Â x2,Â y2,Â etc.Â TheÂ numberÂ ofÂ pointsÂ thereforeÂ mustÂ beÂ divisibleÂ byÂ two.');Â Â }Â Â Â Â //Â countÂ theÂ numberÂ ofÂ timesÂ aÂ flatÂ lineÂ originatingÂ fromÂ theÂ pointÂ andÂ movingÂ rightÂ crossesÂ Â //Â theÂ edgesÂ ofÂ theÂ polygonÂ --Â evenÂ number:Â outsideÂ polygon,Â oddÂ number:Â insideÂ polygonÂ Â \$intsÂ =Â 0;Â Â Â Â \$countÂ =Â count\$polygon_points);Â Â Â Â \$xÂ =Â \$point[0];Â Â \$yÂ =Â \$point[1];Â Â Â Â forÂ (\$i=2;\$i<=\$count\$i\$i+2)Â {Â Â Â Â \$vertex1xÂ =Â \$polygon_points\$i-2];Â Â Â Â Â \$vertex1yÂ =Â \$polygon_points\$i-1];Â Â Â Â Â \$vertex2xÂ =Â \$polygon_points\$iÂ %Â count\$polygon_points)];Â Â Â Â \$vertex2yÂ =Â \$polygon_points[(\$i+1)Â %Â count\$polygon_points)];Â Â Â Â //Â boundaryÂ condition:Â ifÂ theÂ pointÂ isÂ oneÂ ofÂ theÂ verticesÂ thenÂ weÂ areÂ insideÂ Â Â Â ifÂ ((\$xÂ ==Â \$vertex1x)Â &&Â (\$yÂ ==Â \$vertex1y))Â {Â Â Â Â Â Â returnÂ true;Â Â Â Â }Â Â Â Â ifÂ (\$vertex1yÂ ==Â \$vertex2y)Â {Â //Â horizontalÂ edgeÂ Â Â Â Â Â //Â boundaryÂ condition:Â ifÂ pointÂ isÂ onÂ aÂ horizontalÂ polygonÂ edgeÂ thenÂ weÂ areÂ insideÂ Â Â Â Â Â ifÂ ((\$vertex1yÂ ==Â \$y)Â &&Â (\$xÂ >Â min\$vertex1x,Â \$vertex2x))Â &&Â (\$xÂ <Â max\$vertex1x,Â \$vertex2x)))Â {Â Â Â Â Â Â Â Â returnÂ true;Â Â Â Â Â Â }Â Â Â Â }Â elseÂ {Â Â Â Â Â Â ifÂ ((\$yÂ >Â min\$vertex1y,Â \$vertex2y))Â &&Â (\$yÂ <=Â max\$vertex1y,Â \$vertex2y))Â &&Â (\$xÂ <=Â max\$vertex1x,Â \$vertex2x)))Â {Â Â Â Â Â Â Â Â Â \$xintersÂ =Â (\$yÂ -Â \$vertex1y)Â *Â (\$vertex2xÂ -Â \$vertex1x)Â /Â (\$vertex2yÂ -Â \$vertex1y)Â +Â \$vertex1x;Â Â Â Â Â Â Â Â Â //Â boundaryÂ condition:Â ifÂ pointÂ isÂ onÂ theÂ polygonÂ edgeÂ thenÂ weÂ areÂ insideÂ Â Â Â Â Â Â Â ifÂ (\$xÂ ==Â \$xinters)Â {Â Â Â Â Â Â Â Â Â Â returnÂ true;Â Â Â Â Â Â Â Â }Â Â Â Â Â Â Â Â ifÂ (\$xÂ <Â \$xinters)Â {Â Â Â Â Â Â Â Â Â Â Â \$ints++;Â Â Â Â Â Â Â Â Â }Â Â Â Â Â Â }Â Â Â Â Â }Â Â Â }Â Â Â Â //Â ifÂ lineÂ crossesÂ edgesÂ evenÂ numberÂ ofÂ times,Â thenÂ weÂ areÂ outsideÂ polygonÂ Â ifÂ ((\$intsÂ %Â 2)Â ==Â 0)Â {Â Â Â Â returnÂ false;Â Â }Â elseÂ {Â Â Â Â returnÂ true;Â Â }}Â `