Polygon offset algorithm [Polygon Offsetting]

In this tutorial we describe an algorithm for expanding or shrinking polygons.

insert image description here

Recommendation: Use NSDT Designer to quickly build programmable 3D scenes.

1. General form of homology

For simplicity, let's start with one shape, a square. We're probably familiar with the idea of ​​scaling a shape by making it bigger or smaller. Technically, this idea is called a homothetic transformation or homogeneous dilation of space:
insert image description here

We start with any polygon defined by a set of n vertices v:
insert image description here

We then determine a point S = (s_x, s_y), which we call the center of similarity as follows:
insert image description here

After choosing the similarity ratio lambda (any real number), we can compute the corresponding new vertex v'=H(v) in the transformed polygon as follows:
insert image description here

If we know the coordinates v = (v_x, v_y) of the vertex, then its corresponding similar transformation image can be calculated as:

insert image description here

2. Uniform scaling is a special case

In the special case of similarity center coordinates (S_x, S_y) = (0,0), similarity occurs relative to the origin. This significantly simplifies the computation of transforming polygonal images. Therefore, the previous formula simplifies to:
insert image description here

This special type of similarity transformation is called uniform scaling. In this case, factor lambda is assumed to be the name of the transform's scaling factor.

3. Similarity transformation and offset shape

A related problem concerns the offset of shapes or polygons. In some cases we may want to hypothetically transform polygons, but also impose additional constraints on the resulting shape. For example, we might wish to satisfy the added constraint that all points in the new shape's perimeter are equidistant from the closest point in the original shape's perimeter:
insert image description here

For example, if the original shape is a circle, and we scale it around its center, the similarity transformation follows this rule.

Note, however, that similarity usually does not satisfy this constraint. This is because each corner point or vertex in the transformed polygon is actually usually farther from its reversed image than any other point in the transformed shape.

A transformation that satisfies the constraints is called Polygon offsetting, also known as Polygon buffering in geospatial analysis.

4. Program for offsetting polygons

In order to offset a polygon with known vertices, we need an algorithm that performs the following tasks in order. First, we take each vertex and duplicate it. We then connect each new vertex with its previous old vertex in clockwise order.

This allows us to define segments that can move independently of each other. Having chosen an arbitrary length δ, we shift the segment along its axis by that δ.

By remembering the definition of perpendicularity between line segments, we can first calculate the slope m of each line segment AB as follows:

insert image description here

Lines perpendicular to this line segment have a slope that is the reciprocal of the reciprocal of m. For each vertex of a line segment with slope m, the new coordinates (Xnew, Ynew) need to satisfy the following two equations:

insert image description here

for m ≠ 0. These are two equations for two unknowns ynew, xnew that we can solve to find the new coordinates of each vertex.

5. String the points together

The final remaining steps consist of connecting the translated fragments to each other. This can be done by creating a circular arc of radius δ. Each arc is centered at each original vertex and constrained within the two images of the copied vertex.

This animation shows the full process of inflation, from polygon definition to final shape:
insert image description here

Note that this process also works for concave polygons: we need to take care to check that all offset vertices lie outside the transformed polygon, though. If not, we need to delete them and any lines that fall inside the polygon. If we need a refresher on how to do this, we can refer to our geofencing tutorial .

This process also works for downscaling polygons. In this case, we need to reverse the direction of the translated fragment. This can be done by swapping the signs of the delta factors we defined earlier. We then again remove all points that fall outside the shape of the new offset polygon.


Original Link: Polygon Offset Algorithm—BimAnt

Guess you like

Origin blog.csdn.net/shebao3333/article/details/131547551