Results 1 to 10 of 43

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #8
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    659
    Rep Power
    13
    Quote Originally Posted by neal_at_sea View Post
    Thanks this is a really useful function. I am using it to estimate soil type from Cone Penetration Test data. I digitized a xy plot from a scientific paper with 9 zones predicting soil type and created polygons for each of the zones to test where the point lies. It works great.
    Wow, great! I am so glad you made that comment as I often wonder if anyone actually puts any of the stuff I post to practical use.


    Quote Originally Posted by neal_at_sea View Post
    Currently the geometry of the polygons is referenced by cell values in a work sheet, I was wondering however whether it would be possible to include the polygon geometry in the VBA code, so that I can create a UDF that I can call at any time, without the need to reference back to the polygon xy data in a worksheet?
    If I understand you correctly, you have predefined polygons that will not change and you want to bundle their coordinates directly into my function. Rather than do that, I would leave my function as is and create new functions that have the coordinates predefined in them and have that new function call my PtInPoly function passing the predefined array of coordinates to it. Here is an example of what I am thinking using a triangle as the predefined polygon. The triangle coordinates will be (1,2), (9,3) and (4,7). Here is the function I am suggesting you create for this fixed polygon...

    Code:
    Function PtInTriangle(Xcoord As Double, Ycoord As Double) As Boolean
    
      '  Remember, the shape must be closed, so there must
      '  be one array element more than shape corners and
      '  the first and last coordinates must be the same
      Dim Triangle(1 To 4, 1 To 2) As Double
      
      '        Define the Triangle
      '        -------------------
      '     Xcoords     :     Ycoords
      Triangle(1, 1) = 1: Triangle(1, 2) = 2
      Triangle(2, 1) = 9: Triangle(2, 2) = 3
      Triangle(3, 1) = 4: Triangle(3, 2) = 7
      Triangle(4, 1) = 1: Triangle(4, 2) = 2
      
      '  Call the PtInPoly function using the above declared coordinates
      PtInTriangle = PtInPoly(Xcoord, Ycoord, Triangle)
    
    
    End Function
    You can separate the assignments to the Triangle array onto separate lines of code if you want, but I chose to put two per line of code (using the colon operator to separate the two assignments) so you could more easily see the X, Y coordinate relationship. Of course, you would create new function for each of your predefined shapes. One thought depending on how you plan to call your new functions... if you will need to iterate them instead of call specific ones by name, then you can create one new function that include all definitions in it, add an Index argument to your new function's argument list and then use that Index value in a Select Case block to call the particularly iterated polygon.
    Last edited by Rick Rothstein; 03-01-2017 at 01:34 AM.

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. Test
    By Excel Fox in forum Den Of The Fox
    Replies: 0
    Last Post: 07-31-2013, 08:15 AM
  4. Replies: 4
    Last Post: 06-10-2013, 01:27 PM
  5. 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
  •