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 2016
    Posts
    2
    Rep Power
    0
    Quote Originally Posted by Rick Rothstein View Post
    The Polygon argument can be either a two-dimensional VBA array or a two-dimensional range. If it is a range, assigning that range to a Variant variable converts it to a two-dimensional VBA array so that the rest of the code ends up with the same kind of array as when a real two-dimensional array is inputted. The reason the assignment has to take place within the code is because the coercion to an array does not take place via the argument itself even if that argument is a Variant. So, that line of code makes the overall function flexible enough to handle a real array (assigning such an array to the Variant variable does not change its structure) or a range (because the range gets converted to a real array).




    I wanted the function to return either TRUE or FALSE directly in case the function was to be used directly in a MessageBox or to fill a TextBox or any other number of uses besides as the argument to an IF function... the idea being the user can implement the function without having to take special steps to format the output for the occasion. As for why PtInPoly is a Variant and not a Boolean... that was so it could accommodate the error messages when called as a UDF directly on a worksheet.




    It was a design decision to help a user not make a mistake. If the user accidentally grabbed the wrong number of rows from a table of coordinates and inputted that too short range of values, the function would alert the user to the error because the last point did not equal the first point... if I made the function close the polygon automatically, it would blindly calculate values and the user would not be aware some of the answers were incorrect... by forcing the user to make sure the polygon closed, he/she could never make a mistake of accidentally inputting to few points.
    Rick, thank you for the quick response and clarifications. Anyway, if i want to include each vertex of the polygon only once, not doubling the first and last point, how should i modify the code? is it correct if
    Code:
    For x = LBound(Poly) To UBound(Poly) - 1
    is changed to
    Code:
    For x = LBound(Poly) To UBound(Poly) - 2
    ? thanks

  2. #2
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    15
    Quote Originally Posted by qetuol View Post
    Rick, thank you for the quick response and clarifications. Anyway, if i want to include each vertex of the polygon only once, not doubling the first and last point, how should i modify the code? is it correct if
    Code:
    For x = LBound(Poly) To UBound(Poly) - 1
    is changed to
    Code:
    For x = LBound(Poly) To UBound(Poly) - 2
    ? thanks
    No, that part of the code would not change... however, you would have to add code to extend the array one additional array element in order to add the first point as a last point so that the For..Next loop would have a last side to test against. Unfortunately, you cannot use ReDim Preserve to expand the array as it is the first element that needs to be increased by one, so you will need to create a new array that is one row bigger than the passed-in array, transfer the elements of the passed-in array to the new array one element at a time and then duplication the first element into the new array's last element and change the array name in the loop to the newly created array's name. Doing this will slow down the function slightly. Personally, forcing the polygon closed before it is passed into the procedure seems more logical to me as the alternative does not seem worth the extra code that would be needed or time necessary to actually encode it. You may feel free to do so if you really think that extra array element to close the polygon is "unsightly" in some way as you have the base code to work from and the above outline of the steps that would be needed.
    Last edited by DocAElstein; 06-20-2025 at 12:58 PM.

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
  •