Results 1 to 19 of 19

Thread: Buffer a Polygon

  1. #1

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575

    Buffer a Polygon

    Does anyone have a program (preferably in C++) that can look at a polygon and return another polygon around it or inside it - offset by a certain amount.

    Thanks

    Rob
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  2. #2
    Addicted Member
    Join Date
    Aug 2002
    Location
    London UK
    Posts
    255
    Hmm, although this is the maths forum, I think you're in the wrong place, doh Programming hardly ever seems to come up here. Try the general forum or the C++ one at that...
    Not at all related to sheep...

  3. #3
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787
    i can probably help you if you explain yourself a little better
    Massey RuleZ! ^-^__Cheers!__^-^ Massey RuleZ!


    Did you know that...
    The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!

  4. #4
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    If the new shape is outside your polygon, then you won't get a polygon actually.
    When a point of the polygon sticks out, and you buffer it, you'll get an arc. So, you'll get a shape which consists of lines and arcs.

  5. #5

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575
    What I'm looking for is like the attached image

    Thanks

    Rob
    Attached Images Attached Images  
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  6. #6
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    What you could do right, is pick a point to be the centrepoint of the polygon.
    Then, find out the vertices of the polygon.

    Imaginarily, draw points a small distance from the original vertices, in the direction of the centrepoint.
    Then join up the dots
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  7. #7

  8. #8

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575
    I am using Borland Builder so I use the TPoint, so for a square:
    TPoint P[4];

    P[0].x = 10;
    P[0].y = 10;
    P[1].x = 100;
    P[1].y = 10;
    P[2].x = 100;
    P[2].y = 100;
    P[3].x = 10;
    P[3].y = 100;

    But any other array form will do.

    Rob
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  9. #9

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575
    plenderj - I can't find the center point and apply your rules because if the polygons center or centroid is not inside the polygon then the rules fail.

    Thanks

    Rob
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  10. #10
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Perhaps a function to find what's inside and what's out of the polygon.

    The build onto it.

  11. #11

  12. #12
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Here is some functional, uncommented code:

    VB Code:
    1. Private Type XY
    2.     p_X As Double
    3.     p_Y As Double
    4. End Type
    5. Dim MyPoly() As XY
    6. Dim MyPoly2() As XY
    7. Dim MyMidLines() As XY
    8. Dim MyLine() As XY
    9.  
    10. Private Sub OUTLINE_IT()
    11. 'MyPoly is a matrix from 0 to N, containing the X and Y values of the
    12. 'points of the original polygon
    13. 'MyPoly2 is the Output Poly
    14.  
    15. Dim I As Integer
    16. Dim PI As Integer
    17. Dim NI As Integer '
    18. Dim p_1 As XY
    19. Dim p_2 As XY
    20. Dim t1 As Double
    21. Dim t2 As Double
    22. Dim MidP As XY
    23. Dim MySign As Integer
    24.  
    25. ReDim MyMidLines(UBound(MyPoly))
    26. ReDim MyPoly2(UBound(MyPoly))
    27. ReDim MyLine(UBound(MyPoly))
    28. Dim MyD As Long
    29. MyD = Val(txtSize.Text)
    30. MySign = Sgn(MyD)
    31. Picture1.Cls
    32. If MySign <> 0 Then
    33.     For I = 0 To UBound(MyPoly)
    34.         PI = ((I - 1) + UBound(MyPoly) + 1) Mod (UBound(MyPoly) + 1)
    35.         NI = ((I + 1) + UBound(MyPoly) + 1) Mod (UBound(MyPoly) + 1)
    36.         Picture1.Line (MyPoly(I).p_X, MyPoly(I).p_Y)-(MyPoly(NI).p_X, MyPoly(NI).p_Y), vbBlack
    37.         MyLine(I).p_X = MyPoly(NI).p_X - MyPoly(I).p_X
    38.         MyLine(I).p_Y = MyPoly(NI).p_Y - MyPoly(I).p_Y
    39.         t1 = Sqr(((MyPoly(PI).p_X - MyPoly(I).p_X) ^ 2 + (MyPoly(PI).p_Y - MyPoly(I).p_Y) ^ 2) / _
    40.         ((MyPoly(NI).p_X - MyPoly(I).p_X) ^ 2 + (MyPoly(NI).p_Y - MyPoly(I).p_Y) ^ 2))
    41.         MidP.p_X = (MyPoly(PI).p_X + (MyPoly(NI).p_X - MyPoly(I).p_X) * t1 + MyPoly(I).p_X) / 2
    42.         MidP.p_Y = (MyPoly(PI).p_Y + (MyPoly(NI).p_Y - MyPoly(I).p_Y) * t1 + MyPoly(I).p_Y) / 2
    43.        
    44.         MyMidLines(I).p_X = (MidP.p_X - MyPoly(I).p_X)
    45.         MyMidLines(I).p_Y = (MidP.p_Y - MyPoly(I).p_Y)
    46.    
    47.     Next I
    48.     t1 = MySign * Sqr((MyD ^ 2) / (MyMidLines(0).p_X ^ 2 + MyMidLines(0).p_Y ^ 2))
    49.    
    50.     MyPoly2(0).p_X = t1 * MyMidLines(0).p_X + MyPoly(0).p_X
    51.     MyPoly2(0).p_Y = t1 * MyMidLines(0).p_Y + MyPoly(0).p_Y
    52.    
    53.     For I = 0 To UBound(MyPoly)
    54.         PI = ((I - 1) + UBound(MyPoly) + 1) Mod (UBound(MyPoly) + 1)
    55.         NI = ((I + 1) + UBound(MyPoly) + 1) Mod (UBound(MyPoly) + 1)
    56.         t2 = (MyLine(I).p_Y * MyPoly2(I).p_X - MyLine(I).p_X * MyPoly2(I).p_Y + MyLine(I).p_X * MyPoly(NI).p_Y - _
    57.         MyLine(I).p_Y * MyPoly(NI).p_X) / (MyLine(I).p_Y * MyMidLines(NI).p_X - MyLine(I).p_X * MyMidLines(NI).p_Y)
    58.         MyPoly2(NI).p_X = MyMidLines(NI).p_X * t2 + MyPoly(NI).p_X
    59.         MyPoly2(NI).p_Y = MyMidLines(NI).p_Y * t2 + MyPoly(NI).p_Y
    60.         Picture1.Line (MyPoly2(I).p_X, MyPoly2(I).p_Y)-(MyPoly2(NI).p_X, MyPoly2(NI).p_Y), vbBlack
    61.     Next I
    62. End If
    63. End Sub

    And, attached is an image of what it does:
    Attached Images Attached Images  

  13. #13
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    And here's a Progie.
    Just right click on the picture box to build your original Poly.

    Press the "Complete the Polygon" button to close the Poly.

    By altering the value from Negative to Positive in the textbox,
    You will see the scribed poly built.

    Double click the picture to start over.


    -Lou
    Attached Files Attached Files

  14. #14

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575
    Thanks a million - thats exactly what Iwanted

    Rob
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  15. #15
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397


    BTW, I think you should prefilter your points to make sure you
    don't have two {or more } successive points with the same coordinates, and
    you don't have 3 {Or More} successive points that lie on the same line.

    Obviously, if you have 3 successive linear points, you only need
    the first and third for your poly, and obviously if you have
    two or more successive points with identical coordinates, you
    only need 1.

    I believe those conditions are what causes the division by zero.


    -Lou

  16. #16
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Also, you might want to know beforehand, what Offset Distance values will build
    a poly "inside" vrs "outside" your original.

    I beleive the best way would be a negative value should be the indicator for the progie to build outside, and Positive would be "inside". Build a preflight loop into the code, and have it build a Poly2 only slightly different with an Offset Distance value of, oh, lets say 10.

    Once it builds the Preflight Poly2, it measures the total Point to Point distance of Poly2's perimeter, and Compares it to the original. Obviously, if the perimiter is smaller, then Positive values build inside, else if it was larger, then Negative values build inside.

    So then it would automatically calibrate itself to change the sign of the input value you've entered to build Poly2 in the right direction.


    BTW, You can't rely on measuring Poly2's perimiter to determine if Poly2 is inside vrs Outside, for large values of Offset Distance Input. Poly2 turns itself inside out when building inwards, and starts growing again.




    And, 1 Last Thing. The Offset Distance Value is Not a direct measurement of how far away poly2 is from your original poly.

    Its a measure of how far away their Vertice with index Zero Are from each other.

    If you need to have the Offset Distance Value represent the
    Exact Perpendicular distance from Wall to Wall2, well, you'll have
    to tweak the code a bit, to determine MyPoly2(0) a little
    differently.

    -Lou

  17. #17
    New Member
    Join Date
    Feb 2007
    Posts
    1

    Re: Buffer a Polygon

    Any way to do this in JavaScript?

  18. #18

  19. #19

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575

    Re: Buffer a Polygon

    I think it would be very hard to do in javascript - and very slow. But here is a good place to start:
    http://www.walterzorn.com/jsgraphics/jsgraphics_e.htm
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

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