Oops...

This website is made for modern browsers. You are seeing this, because your browser is missing a feature or two. Please install the latest update or switch to a modern browser. See you soon.

Lines and polylines

#curves #lines

This how-to guide provides the basic operations for lines and polylines. A line is, simply put, the straight connection between two points. Poly, from the Greek πολύς, means many or much and thus, a polyline is a construct that consists of several line segments. A polyline, or polygonal chain, is specified by a sequence of points; its vertices. The segments of a polyline can be converted to individual lines.

The edges of polygons, polyhedrons, and meshes are lines. The other way around, lines can be used to define these geometric shapes. A line is linear and its properties, except for the curve parameter, remain constant at every point. Lines are used in mathematical simulations, such as the Finite Element Method (FEM), Dynamic Relaxation, or particle-spring systems. If we wish to pursue such computations, we need to convert all curves to lines. Though a curve may be straight and resemble a line, it is another data type, and might therefore not be allowed. If a curve has changing properties along its course, it can be broken down into multiple lines with linearized properties.

Lines

Create lines

The simplest way to create a line is using the component Lineand setting the start and end point.

An alternative is to use Line SDL, which takes the start point S, the direction vector D and the length L as input.

A line can also be created as a line of best fit for a collection of points, using the component Fit Line.

Convert curves to lines

If we have a curve that we need to convert to a line, we can extract the End Pointsof the curve and use them to create a Line. Depending on the curve, a single line might be a bad approximation. For these cases, we should convert the curve to a polyline and then convert the line segments to lines.

Convert polylines to lines

We can use Explodeto split a polyline into its individual segments. This component will also output the vertices of the polyline.

Convert closed curve to lines

To convert a closed curve, like a circle, to lines, we first have to create a set of points on this curve and then we can use Shift Listto offset the list indices of end points. Then we use Lineto create the lines.

An alternative is using the points as vertices for a PolyLineand we get the individual lines with Explode. We have to set input C of PolyLine to True to get a closed polyline.

Convert grid points to lines

If we have a grid, or any ordered list of points, there are two methods to create lines between the points. The first one is to create a PolyLinewith the list points as vertices and then use Explodeto extract the lines.

The second method uses the Linecomponent: As start points we need every point except the last one and as end points we need all but the first one. We can use Cull Indexwith index I set to -1 to remove the last item and Cull Indexwith input I set to 0 to remove the first item.

Flip Matrix is used to get the lines in the other direction.

Polylines

Construct polylines

The component PolyLinecreates a polyline with the provided vertices V. The polygonal chain runs in the order that the provided list of points has. At input C we can toggle whether the last and first point are connected to create a closed curve. This is False by default.

If we have a collection of lines, we can use Join Curvesto combine them to segments in a polyline.

Also, every polygonal shape will automatically be converted to a polyline at a component’s input, which is requiring a curve.

Deconstruct polylines

To deconstruct a polyline, there are two components that can be used. Explodesplits the polyline at its vertices and outputs the segments S and the vertices V. The component Discontinuitysplits a polyline at the kinks and outputs the vertices P and the curve parameters t. There can be fewer kinks than vertices in a polyline. See the section about rebuilding polylines below to see how this can be used as an advantage.

Convert curves to polylines

To convert a curve to a polyline, we can use Curve To Polyline. We can set different parameters for the approximation: deviation tolerance Td and angle tolerance Ta and also minimum E- and maximum E+ allowed segment length.

We can also divide a curve into segments of equal length with Divide Curve. Afterward, we create a PolyLinefrom the resulting points. This method should only be used if we need the equal length feature, for example to subdivide lines for FEM analysis. In general, Curve To Polylineis the better approach.

Input C was set to True to generate a closed curve for the approximation of the circle.

Rebuild polylines

In this section, we look at two approaches to rebuild or simplify a polyline based on its general characteristic. The first one is to remove some points while maintaining most of the polyline’s characteristics, the second approach is to create some kind of fitting polyline, thus reducing some of the peaks. If you would like to add or remove specific vertices or tweak a polyline at a certain point, you find the answers in the how-to Modify polylines.

First, we take a look at a discreet modification of the polyline: If there are segments that have the same direction vector as the previous one, thus there is no kink at a vertex, we can remove this vertex without changing the characteristic of the polyline. To illustrate this, we use a polyline with two legs, but one leg has a point P in the middle, which can be set to deviate from its initial position to P' (see figure). Thus, the initial polyline consists of three segments; but visually, only two are recognizable.

With no kink, we have three options: The combination of Discontinuityand PolyLine, Reduceand Simplify Curve. They all return a new polyline with 2 instead of 3 segments.

Removing a vertex when P = P'

Now, we move the point P by 0.5 to position P'. Also, we assign 0.5 as tolerance T to Reduceand 1 as tolerance t and as angle a to Simplify Curve. We can see that both components rebuild the curve by jumping over the deviating point. Thus, we get 2 segments instead of 3. The set tolerance is actually the same at both components and is best illustrated by reference to a circle: input T at Reduce resembles a radius while t at Simplify Curve resembles the diameter.

How do both components differ? Simplify Curve will handle all kinds of curves and we can set a deviation angle. Reduce will, if the polyline has a high amplitude and short frequency, reduce vertices and so the oscillation, while keeping the peak values. The component calls it the reduction of the least significant points and this could be useful for a dataset of measurements.

Removing a vertex when P ≠ P'

Another component to reduce the number of segments is Polyline Collapse. As visualized in the figure, we continue with the setup, but point P is moved towards point Q. At Polyline Collapse, we set a segment length tolerance t of 2. The component will now remove segments that are shorter than the tolerance and create a new vertex V' at the midpoint of the removed segment. The length and direction of the adjacent segments will change accordingly.

This kind of operation can be useful, when lots of short lines are imported from another source, for example an area map. We can use Join Curvesto combine the lines to segments of a polyline and then generate a polyline with fewer vertices.

Removing a segment with Polyline Collapse

To address the second approach, creating some kind of fitting curve with the initial vertices, we can use the component Smooth Polyline. Though, the components above can be used (when provided with large tolerances) to radically change the polyline, it is not what they were meant to do. Smooth Polyline rebuilds the polyline by reducing the height of the peaks; the polyline’s oscillation. We can set the strength of the smoothing S and the number of times T this function is applied. After several iterations with high strength, the polyline will lose its oscillation and become a line, stretching from its initial start to end point.

Creating a fitting curve with the initial vertices.
This page is open source. Edit it on GitHub or see how you can contribute.

Up next