Any CGPath
can be split into subpaths, of which there are three types:
When calculating the length of a CGPath
we actually calculate the sum of the lengths of all its subpaths.
Linear is the most simple type, as it defines a straight line by two points.
The following formula calculates the exact distance between two points:
The order of the points is not important. The distance from
A
toB
is equal to the distance fromB
toA
.
Although there exists a formula to get the exact length between two points, it is useful to get a point on the path (meaning between the two points). For this a parametric formula is needed:
t
is defined in the range0...1
(inclusive), wheret = 0
definesP0
andt = 1
definesP1
This parametric formula needs to be evaluated for both the x and y coordinates of the two points, with an equal value for
t
.Note that the
t
factors add up to 1 ((1 - t) + t
), this is required for this type of formulas to work.See Linear Parametric Function.gcx for an interactive example. 1
Quadratic curves are defined by two points and one control point. The control point does not necessarily represent a point on the curve, but can rather be thought of as a "magnetic point" which attracts a parabolic-shaped curve to it.
t
is defined in the range0...1
(inclusive), wheret = 0
definesP0
andt = 1
definesP2
P0
is the start point,P1
the control point andP2
the end point.This parametric formula needs to be evaluated for both the x and y coordinates of the two points, with an equal value for
t
.This animation shows
P1
oscillating itsx
value between1
and4
.Note that when
P0
andP1
have the same coordinates, the path is linear andP1
becomes redundant.See Quadratic Parametric Function.gcx for an interactive example. 1
Cubic curves are defined by a start and end point and two control points.
t
is defined in the range0...1
(inclusive), wheret = 0
definesP0
andt = 1
definesP3
P0
is the start point,P1
andP2
are control points andP3
is the end point.This parametric formula needs to be evaluated for both the x and y coordinates of the two points, with an equal value for
t
.See Cubic Parametric Function.gcx for an interactive example. 1
The three subpath types essentially embody an increasingly higher degree of polynomials, as can be seen in the first term:
Name | First term |
---|---|
Linear | |
Quadratic | |
Cubic |
Furthermore, these parametric functions can easily be memorized. The number of terms is equal to the degree + 1
:
Name | Number of terms |
---|---|
Linear | 2 |
Quadratic | 3 |
Cubic | 4 |
Each term, the factor (1 - t)
decreases its power by 1
, starting at degree
.
And each term the factor t
increases its power by 1
, starting at 0
.
Finally, the coefficients of the terms can be found using Pascal's triangle:
source: Wikipedia
Name | Coefficients |
---|---|
Linear | 1 1 |
Quadratic | 1 2 1 |
Cubic | 1 3 3 1 |
When showing the functions in one table, the pattern becomes clear:
Name | Parametric function |
---|---|
Linear | |
Quadratic | |
Cubic |
This pattern continues beyond cubic, but is out of scope for this project.
To see more, check: Bézier curve - Higher order curves on Wikipedia
1: Grapher is included on every Mac and can be found in /Applications/Utilities