# Segment

The Segment object represents the points of a path through which its `Curve`

objects pass. The segments of a path can be accessed through its `path.segments`

array.

Each segment consists of an anchor point (`segment.point`

) and optionaly an incoming and an outgoing handle (`segment.handleIn`

and `segment.handleOut`

), describing the tangents of the two `Curve`

objects that are connected by this segment.

## Constructors

`Segment([point[, handleIn[, handleOut]]])`

Creates a new Segment object.

Parameters:

`point:`

`Point`

โ the anchor point of the segment โ optional, default:`{x: 0, y: 0}`

`handleIn:`

`Point`

โ the handle point relative to the anchor point of the segment that describes the in tangent of the segment โ optional, default:`{x: 0, y: 0}`

`handleOut:`

`Point`

โ the handle point relative to the anchor point of the segment that describes the out tangent of the segment โ optional, default:`{x: 0, y: 0}`

Returns:

`Segment`

Example:

`Segment(object)`

Creates a new Segment object.

Parameters:

`object:`

`Object`

โ an object containing properties to be set on the segmentReturns:

`Segment`

Example:Creating segments using object notation:

## Properties

`point`

The anchor point of the segment.

Type:

`Point`

`handleIn`

The handle point relative to the anchor point of the segment that describes the in tangent of the segment.

Type:

`Point`

`handleOut`

The handle point relative to the anchor point of the segment that describes the out tangent of the segment.

Type:

`Point`

`selected`

Specifies whether the segment is selected.

Type:

`Boolean`

Example:

### Hierarchy

`index`

The index of the segment in the

`path.segments`

array that the segment belongs to.Read only.

Type:

`Number`

`path`

The path that the segment belongs to.

Read only.

Type:

`Path`

`curve`

The curve that the segment belongs to. For the last segment of an open path, the previous segment is returned.

Read only.

Type:

`Curve`

`location`

The curve location that describes this segmentโs position on the path.

Read only.

Type:

`CurveLocation`

### Sibling Segments

`next`

The next segment in the

`path.segments`

array that the segment belongs to. If the segments belongs to a closed path, the first segment is returned for the last segment of the path.Read only.

Type:

`Segment`

`previous`

The previous segment in the

`path.segments`

array that the segment belongs to. If the segments belongs to a closed path, the last segment is returned for the first segment of the path.Read only.

Type:

`Segment`

## Methods

`hasHandles()`

Checks if the segment has any curve handles set.

Returns:

`Boolean`

โ`true`

if the segment has handles set,`false`

otherwiseSee also:

`segment.handleIn`

`segment.handleOut`

`curve.hasHandles`

()`path.hasHandles`

()

`isSmooth()`

Checks if the segment connects two curves smoothly, meaning that its two handles are collinear and segment does not form a corner.

Returns:

`Boolean`

โ`true`

if the segment is smooth,`false`

otherwiseSee also:

`point.isCollinear`

()

`clearHandles()`

Clears the segmentโs handles by setting their coordinates to zero, turning the segment into a corner.

`smooth([options])`

Smooths the bezier curves that pass through this segment by taking into account the segmentโs position and distance to the neighboring segments and changing the direction and length of the segmentโs handles accordingly without moving the segment itself.

Two different smoothing methods are available:

`'catmull-rom'`

uses the Catmull-Rom spline to smooth the segment.The optionally passed factor controls the knot parametrization of the algorithm:

`0.0`

: the standard, uniform Catmull-Rom spline`0.5`

: the centripetal Catmull-Rom spline, guaranteeing no self-intersections`1.0`

: the chordal Catmull-Rom spline

`'geometric'`

use a simple heuristic and empiric geometric method to smooth the segmentโs handles. The handles were weighted, meaning that big differences in distances between the segments will lead to probably undesired results.The optionally passed factor defines the tension parameter (

`0โฆ1`

), controlling the amount of smoothing as a factor by which to scale each handle.Options:

`options.type: String`

โ the type of smoothing method:`โcatmull-romโ`

,`โgeometricโ`

โ default:`โcatmull-romโ`

`options.factor: Number`

โ the factor parameterizing the smoothing method โ default:`0.5`

for`'catmull-rom'`

,`0.4`

for`'geometric'`

Parameters:

`options:`

`Object`

โ the smoothing options โ optionalSee also:

`pathItem.smooth([options])`

`isFirst()`

Checks if the this is the first segment in the

`path.segments`

array.Returns:

`Boolean`

โ`true`

if this is the first segment,`false`

otherwise

`isLast()`

Checks if the this is the last segment in the

`path.segments`

array.Returns:

`Boolean`

โ`true`

if this is the last segment,`false`

otherwise

`reverse()`

Reverses the

`handleIn`

and`handleOut`

vectors of this segment, modifying the actual segment without creating a copy.Returns:

`Segment`

โ the reversed segment

`reversed()`

Returns the reversed the segment, without modifying the segment itself.

Returns:

`Segment`

โ the reversed segment

`remove()`

Removes the segment from the path that it belongs to.

Returns:

`Boolean`

โ`true`

if the segment was removed,`false`

otherwise

`clone()`

Returns:

`Segment`

`toString()`

Returns:

`String`

โ a string representation of the segment

`transform(matrix)`

Transform the segment by the specified matrix.

Parameters:

`matrix:`

`Matrix`

โ the matrix to transform the segment by

`interpolate(from, to, factor)`

Interpolates between the two specified segments and sets the point and handles of this segment accordingly.

Parameters:

`from:`

`Segment`

โ the segment defining the geometry when`factor`

is`0`

`to:`

`Segment`

โ the segment defining the geometry when`factor`

is`1`

`factor:`

`Number`

โ the interpolation coefficient, typically between`0`

and`1`

, but extrapolation is possible too

Last updated