# CubicSegment

A cubic bezier segment of a path.

## Constructors

CubicSegment(s,cs,ce,e)→[CubicSegment](https://cuttle.xyz/learn/reference/CubicSegment)

Constructs a cubic segment.

`s`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

`cs`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

`ce`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

`e`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

CubicSegment.fromAnchors(startAnchor,endAnchor)→[CubicSegment](https://cuttle.xyz/learn/reference/CubicSegment)

Constructs a cubic segment from two anchors.

The `handleOut` of `startAnchor` and `handleIn` of `endAnchor` will be used, but the other handles will be ignored.

`startAnchor`

`[Anchor](https://cuttle.xyz/learn/reference/Anchor)`

The first anchor of the segment

`endAnchor`

`[Anchor](https://cuttle.xyz/learn/reference/Anchor)`

The second anchor of the segment

CubicSegment.fromLineSegment(segment)→[CubicSegment](https://cuttle.xyz/learn/reference/CubicSegment)

Constructs a cubic segment from a line segment.

The control points of the cubic segment are calculated such that they lie on the line segment between `segment.s` and `segment.e`, evenly spaced in time at 1/3, 2/3, and 1 respectively. This keeps the segment linear with a constant derivative.

`segment`

`[LineSegment](https://cuttle.xyz/learn/reference/LineSegment)`

A line segment to convert into a cubic segment

## Properties

.s[Vec](https://cuttle.xyz/learn/reference/Vec)

The start position

.cs[Vec](https://cuttle.xyz/learn/reference/Vec)

The first cubic "handle" position

.ce[Vec](https://cuttle.xyz/learn/reference/Vec)

The second cubic "handle" position

.e[Vec](https://cuttle.xyz/learn/reference/Vec)

The end position

## Methods

.isLinear()→boolean

Returns `true` if this segment is linear. A cubic segment is linear if its control points lie on the line segment between its start and end points, and they are evenly spaced.

.isStraight(tolerance)→boolean

`tolerance`

`number`

`GEOMETRIC_TOLERANCE`

Returns `true` if this segment is straight. A cubic segment is straight if its control points lie on the line segment between its start and end points.

.equals(segment)→boolean

`segment`

`[CubicSegment](https://cuttle.xyz/learn/reference/CubicSegment)`

Another cubic segment

Returns `true` if this segment is exactly equal to `segment`.

.copy(segment)→[CubicSegment](https://cuttle.xyz/learn/reference/CubicSegment)chainable

Sets the positions of this segments so that it equals `segment`.

`segment`

`[CubicSegment](https://cuttle.xyz/learn/reference/CubicSegment)`

.mix(segment,t)→[CubicSegment](https://cuttle.xyz/learn/reference/CubicSegment)chainable

Linearly interpolates this segment's control points to `segment`'s control points by the mixing factor `t`.

`segment`

`[CubicSegment](https://cuttle.xyz/learn/reference/CubicSegment)`

The cubic segment to interpolate to.

`t`

`number`

The mixing factor between 0 and 1.

.setFromAnchors(startAnchor,endAnchor)→[CubicSegment](https://cuttle.xyz/learn/reference/CubicSegment)chainable

Sets the positions of this segment from two anchors.

`startAnchor`

`[Anchor](https://cuttle.xyz/learn/reference/Anchor)`

The first anchor

`endAnchor`

`[Anchor](https://cuttle.xyz/learn/reference/Anchor)`

The second anchor

.length()→number

Returns the approximate arc length of the segment.

.timeAtDistance(distance)→number

`distance`

`number`

A positive distance

Returns the approximate time at `distance` along the segment.

If `distance` is greater than the length of the segment, this will return

1.  Negative distances will return 0.

.distanceAtTime(time)→number

`time`

`number`

A segment time between 0 and 1

Returns the approximate distance along the segment at `time`.

.positionAtTime(time)→[Vec](https://cuttle.xyz/learn/reference/Vec)

`time`

`number`

A segment time between 0 and 1

Returns the position on the segment at `time`.

.derivativeAtTime(time)→[Vec](https://cuttle.xyz/learn/reference/Vec)

`time`

`number`

A segment time between 0 and 1

Returns the first derivative of the segment at `time`.

The first derivative can also be thought of as the "velocity" of a point moving along the curve.

.secondDerivativeAtTime(time)→[Vec](https://cuttle.xyz/learn/reference/Vec)

`time`

`number`

A segment time between 0 and 1

Returns the second derivative of the segment at `time`.

The second derivative can also be thought of as the "acceleration", or change in velocity of a point moving along the curve.

.curvatureAtTime(time)→number

`time`

`number`

A segment time between 0 and 1

Returns the curvature of the segment at `time`.

Curvature describes how "bent" a segment is at a given point. It can be thought of as the reciprocal of the radius of a circle with the same curvature. To find the radius, one can use the formula `1 / curvature`.

.tangentAtTime(time)→[Vec](https://cuttle.xyz/learn/reference/Vec)

`time`

`number`

A segment time between 0 and 1

Returns a unit-length vector tangent to the segment at `time`.

.normalAtTime(time)→[Vec](https://cuttle.xyz/learn/reference/Vec)

`time`

`number`

A segment time between 0 and 1

Returns a unit-length vector perpendicular to the segment at `time`.

.slice(startTime,endTime)→[CubicSegment](https://cuttle.xyz/learn/reference/CubicSegment)

`startTime`

`number`

A segment time between 0 and 1

`endTime`

`number`

A segment time between 0 and 1 greater than `startTime`

Returns A new cubic segment that is the subset of the segment between `startTime` and `endTime`.

.splitAtTime(time)→[CubicSegment](https://cuttle.xyz/learn/reference/CubicSegment)\[\]

Splits the segment at a specific `time`.

`time`

`number`

A segment time between 0 and 1

Returns two cubic segments.

## Methods inherited from [Geometry](https://cuttle.xyz/learn/reference/Geometry)

.clone()→[CubicSegment](https://cuttle.xyz/learn/reference/CubicSegment)

Clone is useful when you need to make a change to geometry without changing the original.

```
const transformedPath = path.clone().transform({ rotation: 45 });
```

Returns A deep copy of this geometry

.isValid()→boolean

Returns `true` if this geometry is valid, or `false` otherwise.

.affineTransform(affineMatrix)→[CubicSegment](https://cuttle.xyz/learn/reference/CubicSegment)chainable

Spatially transforms this geometry by an affine transformation matrix.

Affine matrices can represent any 2-dimensional transformation that keeps lines parallel.

`affineMatrix`

`[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)`

.affineTransformWithoutTranslation(affineMatrix)→[CubicSegment](https://cuttle.xyz/learn/reference/CubicSegment)chainable

Spatially transforms this geometry by an affine transformation matrix, excluding translation. Only rotation, scale, and skew transformations will be applied.

`affineMatrix`

`[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)`

.reverse()→[CubicSegment](https://cuttle.xyz/learn/reference/CubicSegment)chainable

Reverses this cubic segment.

Swaps the start and end points of the segment as well as their corresponding control points.

.closestPoint(point,areaOfInterest)→([ClosestPointResultWithTime](https://cuttle.xyz/learn/reference/interfaces#ClosestPointResultWithTime) | undefined)

`point`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

The target point

`areaOfInterest`

`[BoundingBox](https://cuttle.xyz/learn/reference/BoundingBox)`

optional

If supplied, only results inside this bounding box will be returned. This can save a lot of computation if you only need to find closest points within a small area. Typically `areaOfInterest` is centered on `point`, but this isn't required.

Returns The closest point to `point` that lies on this geometry, or `undefined` if no point is found.

.boundingBox()→[BoundingBox](https://cuttle.xyz/learn/reference/BoundingBox)

Returns the smallest axis-aligned bounding box that contains this geometry.

.looseBoundingBox()→[BoundingBox](https://cuttle.xyz/learn/reference/BoundingBox)

The loose bounding box may not be the smallest possible, but it's usually cheaper to compute. Use this when the exact bounding box isn't required.

Returns an axis-aligned bounding box that contains this geometry.

.isContainedByBoundingBox(box)→boolean

Geometry is contained by a bounding box if no part of it lies beyond it's minimum and maximum.

`box`

`[BoundingBox](https://cuttle.xyz/learn/reference/BoundingBox)`

.isIntersectedByBoundingBox(box)→boolean

Geometry intersects a bounding box if part of the geometry crosses the boundary between the inside and outside of the box.

`box`

`[BoundingBox](https://cuttle.xyz/learn/reference/BoundingBox)`

.isOverlappedByBoundingBox(box)→boolean

Geometry is overlapped by a bounding box if a point can be chosen that is indside both the geometry and the box.

Geometry is overlapped by a bounding box if it's contained inside, or intersected by it.

`box`

`[BoundingBox](https://cuttle.xyz/learn/reference/BoundingBox)`

.isValid(a)→

`a`

`unknown`

Returns `true` if `geom` is valid geometry

.transform(transform)→thischainable

Transforms this geometry.

A transform can optionally specify any of `position`, `rotation`, `scale`, `skew` and `origin`.

`origin` defines the center (in pre-transform coordinates) of the transformation for `rotation`, `scale` and `skew`.

```
// Simple translation
geometry.translate({
  position: Vec(1, 0),
});

// Rotation and scale
geometry.transform({
  rotation: 45,
  scale: 2,
});

// Complicated transform
geometry.transform({
  position: Vec(1, 1),
  rotation: 180,
  scale: Vec(2, 1),
  skew: 45,
  origin: Vec(-0.5, 0.5),
};
```

`transform`

`[TransformArgs](https://cuttle.xyz/learn/reference/interfaces#TransformArgs)`

.intersectionsWith(geometries,areaOfInterest)→[IntersectionResult](https://cuttle.xyz/learn/reference/interfaces#IntersectionResult)\[\]

`geometries`

`[Geometry](https://cuttle.xyz/learn/reference/Geometry)[]`

An array of geometries to intersect with.

`areaOfInterest`

`[BoundingBox](https://cuttle.xyz/learn/reference/BoundingBox)`

optional

If supplied, only intersection results inside this bounding box will be returned. This can save a lot of computation if you only need to find intersections within a small area.

Returns An array of intersections between this geometry and `geometries`

.overlapsWith(geometries,areaOfInterest,tolerance)→[OverlapResult](https://cuttle.xyz/learn/reference/OverlapResult)\[\]

`geometries`

`[Geometry](https://cuttle.xyz/learn/reference/Geometry)[]`

An array of geometries to find overlaps with.

`areaOfInterest`

`[BoundingBox](https://cuttle.xyz/learn/reference/BoundingBox)`

optional

If supplied, input geometry will be filtered so that only parts that intersect this bounding box will be tested. This can save a lot of time if only need to find overlaps within a small area.

`tolerance`

`number`

optional

The maximum distance apart two segments can be to be considered overlapping.

Returns An array of overlaps between this geometry and `geometries`

.distanceToIntersect(targetGeometry,direction,options)→(number | undefined)

`targetGeometry`

`[Geometry](https://cuttle.xyz/learn/reference/Geometry)`

The geometry to move toward.

`direction`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

The direction to move in. This should point towards the target geometry, otherwise no intersection may be found.

`options`

`[DistanceToIntersectOptions](https://cuttle.xyz/learn/reference/interfaces#DistanceToIntersectOptions)`

optional

Returns approximately the smallest distance to move until this geometry intersects the target geometry.

If `minOverlap` is specified, the distance to move until the geometry overlaps by some minimum width will be returned instead.

Returns `undefined` if no intersection is found.