Sorry if this is in the wrong forum, but I didnt think it would fit in the others! I have been attempting to convert a small bit of C to VB from this site: http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html[^] It determines if a given point is either inside, outside, or on the edge, of a polygon shape. I want to use this in a GPS mapping application I am working on, but its not working, I think I have some of the syntax slightly wrong. The code to convert:
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
{
int i, j, c = 0;
for (i = 0, j = nvert-1; i < nvert; j = i++) {
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
c = !c;
}
return c;
}
Here is what I have converted it to:
Function pnpoly(ByVal nvert As Int16, ByVal vertx() As Decimal, ByVal verty() As Decimal, ByVal testx As Decimal, ByVal testy As Decimal) As Int16
' RETURNS:
' -1 IF THE POINT IS OUTSIDE OF THE POLYGON,
' 0 IF THE POINT IS ON AN EDGE OR AT A VERTEX,
' 1 IF THE POINT IS INSIDE OF THE POLYGON
Dim i As Int16 = 0
Dim j As Int16 = 0
Dim c As Int16 = 0
i = 0
j = nvert - 1
Do
If Not ((verty(i) > testy) = (verty(j) > testy)) Then
If testx < (vertx(j) - vertx(i)) \* (testy - verty(i)) / (verty(j) - verty(i)) + vertx(i) Then
c = Not c
End If
End If
j = i
i = i + 1
Loop Until Not i < nvert
pnpoly = c
End Function
I have converted it based on notes in the source article, specifically: in the C language, when executing the code a&&b, if a is false, then b must not be evaluated. If your compiler doesn't do this, then it's not implementing C, and you will get a divide-by-zero and Explanation of "for (i = 0, j = nvert-1; i < nvert; j = i++):" The intention is to execute the loop for each i from 0 to nvert-1. For each iteration, j is i-1. However that wraps, so if i=0 then j=nvert-1. Therefore the current edge runs between verts j and i, and the loop is done once per edge. In det