Results 1 to 10 of 10

Thread: Polygon Intersection

  1. #1

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Exclamation Polygon Intersection

    How do you detect intersections between polygons?
    (VB.NET 2005)

  2. #2
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: Polygon Intersection

    I haven't really looked into the matter much but you might find this article useful: http://gpwiki.org/index.php/Polygon_Collision

  3. #3

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Polygon Intersection

    Ok, I looked at it, but...
    1. It's to complicated for me.
    2. I can't use a line, because VB can only use points, really.

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Polygon Intersection

    Quote Originally Posted by minitech
    Ok, I looked at it, but...
    1. It's to complicated for me.
    2. I can't use a line, because VB can only use points, really.
    1. Polygon intersection is not a simple thing.
    2. But what is a line of not two points?



    Edit: You could also take a look at the links I posted in this thread http://www.vbforums.com/showthread.php?t=556888
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Polygon Intersection

    I'd be very very surprised if this code hasn't been written. After a brief Google search I found http://www.cs.man.ac.uk/~toby/alan/software/, which has a .NET wrapper. It's overkill but the code should all be there in some language or another.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  6. #6

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Talking Re: Polygon Intersection

    [QUOTE=Atheist]1. Polygon intersection is not a simple thing.
    2. But what is a line of not two points?



    1. I know, but that is really complicated for VB.
    2. Two points are two points, they represent a line sometimes, but seriously, it's very hard for me to calculate things like lines. I'm not a math genius, like everyone else here

  7. #7
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Polygon Intersection

    I think that in general this problem is much more difficult than converting points into lines. I would be very happy to go through an algorithm for convex polygons, assuming as little as possible math-wise and including things like converting a couple of points to a line, though it would be lengthy, possibly not what you're after (maybe your polygons aren't always convex), and almost certainly less optimized than whatever you'd find online. Sorry

    Oh, also the wiki linked earlier has the following edge intersection code. I haven't checked the reasoning or the code but I'd assume it's fine. The Vector2D object is in VB6 terms a UDT with x and y members. Note vector subtraction is used in the "b-a" portions. You could use this to check whether every possible pair of edges intersect, though that scales pretty horribly if you're using large polygons.

    C++ Code:
    1. double determinant(Vector2D vec1, Vector2D vec2){
    2.     return vec1.x*vec2.y-vec1.y*vec2.x;
    3. }
    4.  
    5. //one edge is a-b, the other is c-d
    6. Vector2D edgeIntersection(Vector2D a, Vector2D b, Vector2D c, Vector2D d){
    7.     double det=determinant(b-a,c-d);
    8.     double t=determinant(c-a,c-d)/det;
    9.     double u=determinant(b-a,c-a)/det;
    10.     if ((t<0)||(u<0)||(t>1)||(u>1))return NO_INTERSECTION;
    11.     return a*(1-t)+t*b;
    12. }
    Last edited by jemidiah; Feb 9th, 2009 at 03:45 AM.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  8. #8

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Polygon Intersection

    Heh...
    I don't even know what those are, and I use VB, not C++.

  9. #9
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Polygon Intersection

    I guess my point is that the math required for this is much higher than what you've called "...really complicated for VB". Again I'd advise you to find something already written and muddle through fitting it to your needs, because this is not a trivial problem. But, below is the edge intersection code translated to VB6 (sorry, haven't had to switch to .NET yet).

    VB Code:
    1. Option Explicit
    2.  
    3. Private Type Vector2D
    4.     x As Single
    5.     y As Single
    6. End Type
    7.  
    8. Private Function Vector2DSubtract(vec1 As Vector2D, vec2 As Vector2D) As Vector2D
    9.     Vector2DSubtract.x = vec1.x - vec2.x
    10.     Vector2DSubtract.y = vec1.y - vec2.y
    11. End Function
    12.  
    13. Private Function determinant(vec1 As Vector2D, vec2 As Vector2D) As Double
    14.     determinant = vec1.x * vec2.y - vec1.y * vec2.x
    15. End Function
    16.  
    17. 'One edge is a-b, the other is c-d
    18. Private Function edgeIntersection(a As Vector2D, b As Vector2D, c As Vector2D, d As Vector2D) As Vector2D
    19.     Dim det As Double, t As Double, u As Double
    20.     det = determinant(Vector2DSubtract(b, a), Vector2DSubtract(c, d))
    21.     t = determinant(Vector2DSubtract(c, a), Vector2DSubtract(c, d)) / det
    22.     u = determinant(Vector2DSubtract(b, a), Vector2DSubtract(c, a)) / det
    23.    
    24.     If ((t < 0) Or (u < 0) Or (t > 1) Or (u > 1)) Then
    25.         MsgBox "no intersection"
    26.     Else
    27.         edgeIntersection.x = a.x * (1 - t) + t * b.x
    28.         edgeIntersection.y = a.y * (1 - t) + t * b.y
    29.     End If
    30. End Function
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  10. #10
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Polygon Intersection

    Some time ago I was trying to write an application for polygon clipping based on the Weiler-Atherton algorithm. However it became very difficult for me to deal with degenerate cases (i.e. vertices lying on one of the other polygon's sides, etc) so if you run the code you'll see that it fails in some cases.

    As a matter of fact I also came across that link transcribed above by jemidiah and the wrapper dll worked like charm, so that's why I discontinued writing my own code.

    However, the function for polygon intersection works quite satisfactorily. Only it is all written in VB6 and it can be messy due to the structures I've used, but if you study them closely maybe the code could be useful to you.
    Attached Files Attached Files
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

Posting Permissions

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



Click Here to Expand Forum to Full Width