Results 1 to 10 of 43

Thread: Test Whether A Point Is In A Polygon Or Not

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member
    Join Date
    Mar 2017
    Posts
    1
    Rep Power
    0
    Hi Rick, I am trying to use your function (PtInPoly) inside a function i've made (APPROACH). It's inside an If;

    Code:
    Function APPROACH(P1 As Range, x1 As Double, y1 As Double) As Double
    
    Dim A1(3, 3) As Double
    Dim A2(5, 2) As Double
    
    For i = 1 To 5 Step 2
    A1(1, 1) = P1(i, 1)
    A1(1, 2) = P1(i, 2)
    A1(1, 3) = P1(i, 3)
    A1(2, 1) = P1(i + 1, 1)
    A1(2, 2) = P1(i + 1, 2)
    A1(2, 3) = P1(i + 1, 3)
    A1(3, 1) = P1(i + 2, 1)
    A1(3, 2) = P1(i + 2, 2)
    A1(3, 3) = P1(i + 2, 3)
    
    A2(1, 1) = P1(i, 1)
    A2(1, 2) = P1(i, 2)
    A2(2, 1) = P1(i + 1, 1)
    A2(2, 2) = P1(i + 1, 2)
    A2(3, 1) = P1(i + 3, 1)
    A2(3, 2) = P1(i + 3, 2)
    A2(4, 1) = P1(i + 2, 1)
    A2(4, 2) = P1(i + 2, 2)
    A2(5, 1) = P1(i, 1)
    A2(5, 2) = P1(i, 2)
    
    If PtInPoly(x1, y1, A2) = 0 Then
        APPROACH = AltXY(A1, x1, y1)
        Exit For
    End If
    Next i
    
    End Function
    I want the function, in case the point (x1,y1) is INSIDE the polygon, to apply function AltXY (If PtInPoly(x1, y1, A2) = 0 Then...) and if not, check for other areas but for some reason its giving me the wrong result. AltXY is not badly written, as if I wanted it to give me APPROACH = 10, it still keeps giving me zeros but I don't understand why. Any help is appreciated

  2. #2
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    15
    Quote Originally Posted by gonurvia View Post
    Hi Rick, I am trying to use your function (PtInPoly) inside a function i've made (APPROACH). It's inside an If;

    Code:
    Function APPROACH(P1 As Range, x1 As Double, y1 As Double) As Double
    
    Dim A1(3, 3) As Double
    Dim A2(5, 2) As Double
    
    For i = 1 To 5 Step 2
    A1(1, 1) = P1(i, 1)
    A1(1, 2) = P1(i, 2)
    A1(1, 3) = P1(i, 3)
    A1(2, 1) = P1(i + 1, 1)
    A1(2, 2) = P1(i + 1, 2)
    A1(2, 3) = P1(i + 1, 3)
    A1(3, 1) = P1(i + 2, 1)
    A1(3, 2) = P1(i + 2, 2)
    A1(3, 3) = P1(i + 2, 3)
    
    A2(1, 1) = P1(i, 1)
    A2(1, 2) = P1(i, 2)
    A2(2, 1) = P1(i + 1, 1)
    A2(2, 2) = P1(i + 1, 2)
    A2(3, 1) = P1(i + 3, 1)
    A2(3, 2) = P1(i + 3, 2)
    A2(4, 1) = P1(i + 2, 1)
    A2(4, 2) = P1(i + 2, 2)
    A2(5, 1) = P1(i, 1)
    A2(5, 2) = P1(i, 2)
    
    If PtInPoly(x1, y1, A2) = 0 Then
        APPROACH = AltXY(A1, x1, y1)
        Exit For
    End If
    Next i
    
    End Function
    I want the function, in case the point (x1,y1) is INSIDE the polygon, to apply function AltXY (If PtInPoly(x1, y1, A2) = 0 Then...) and if not, check for other areas but for some reason its giving me the wrong result. AltXY is not badly written, as if I wanted it to give me APPROACH = 10, it still keeps giving me zeros but I don't understand why. Any help is appreciated
    You did not supply an x1,y1 point that fails to work so that I could test it out; however, I am suspicious of your A1 array declaration (probably your A2 array's declaration as well). You have the A1 array declared as...

    Dim A1(3, 3) As Double

    but then you start filling it at element number 1. Unless you are using Option Base 1, your declaration set the lower bound for the array at 0, not 1 and that might be screwing up the calculations in my code (not sure, but I think it would). Try changing your declaration to this and see if that solves the problem...

    Dim A1(1 To 3, 1 To 3) As Double

    This declaration set the lower bound to 1, not 0. While I don't know if it matters to your AltXY function or not, but for consistency, I would change the declaration for the A2 array in a similar way.

Similar Threads

  1. This is a test Test Let it be
    By Admin in forum Test Area
    Replies: 6
    Last Post: 05-30-2014, 09:44 AM
  2. This is a test of Signature Block Variable Dim
    By alansidman in forum Test Area
    Replies: 0
    Last Post: 10-17-2013, 07:42 PM
  3. Replies: 4
    Last Post: 06-10-2013, 01:27 PM
  4. Test
    By Excel Fox in forum Word Help
    Replies: 0
    Last Post: 07-05-2011, 01:51 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •