An algorithm for inflating/deflating (offsetting, buffering) polygons

后端 未结 12 2153
醉话见心
醉话见心 2020-11-22 12:58

How would I "inflate" a polygon? That is, I want to do something similar to this:

\"alt

<
12条回答
  •  忘掉有多难
    2020-11-22 13:07

    Each line should split the plane to "inside" and "outline"; you can find this out using the usual inner-product method.

    Move all lines outward by some distance.

    Consider all pair of neighbor lines (lines, not line segment), find the intersection. These are the new vertex.

    Cleanup the new vertex by removing any intersecting parts. -- we have a few case here

    (a) Case 1:

     0--7  4--3
     |  |  |  |
     |  6--5  |
     |        |
     1--------2
    

    if you expend it by one, you got this:

    0----a----3
    |    |    |
    |    |    |
    |    b    |
    |         |
    |         |
    1---------2
    

    7 and 4 overlap.. if you see this, you remove this point and all points in between.

    (b) case 2

     0--7  4--3
     |  |  |  |
     |  6--5  |
     |        |
     1--------2
    

    if you expend it by two, you got this:

    0----47----3
    |    ||    |
    |    ||    |
    |    ||    |
    |    56    |
    |          |
    |          |
    |          |
    1----------2
    

    to resolve this, for each segment of line, you have to check if it overlap with latter segments.

    (c) case 3

           4--3
     0--X9 |  |
     |  78 |  |
     |  6--5  |
     |        |
     1--------2
    

    expend by 1. this is a more general case for case 1.

    (d) case 4

    same as case3, but expend by two.

    Actually, if you can handle case 4. All other cases are just special case of it with some line or vertex overlapping.

    To do case 4, you keep a stack of vertex.. you push when you find lines overlapping with latter line, pop it when you get the latter line. -- just like what you do in convex-hull.

提交回复
热议问题