CGMath is a collection of extensions and functions around CGGeometry types,
CGVector. It includes some other functions for
clamp is a function that takes three parameters,
max. It will return a value that will be
value <= min,
value >= max and
value in any other case.
lerp is the linear interpolation function. Given two values,
end, and a
progress (usually in the range
[0, 1]), it will perform a linear interpolation between
end and return that value.
You can read more about linear interpolation here but the basic gist of it is that it will return
end when progress is
1, and for any value in
[0, 1] it will return the percentage of the difference between
start. For example,
lerp(start: 5, end: 15, progress: 0.5) will return
10, which is right between
15, because progress is
This method can take a
progress value outside the
[0, 1] range, this can be useful in some cases, like when making a bouncy animation, but you can prevent this by using
clamp() on the progress before calling the function.
inverseLerp is the opposite of
lerp. Given three values,
end and a
value (usually in the
[start, end] range), it will return a
progress value such that using it in
lerp with the same
end values, it will return
value. For example,
lerp(start: 5, end: 15, value: 10) will return
10 is right between
remap takes to ranges and converts the value from one range to another. For example, if you have a few values in the
[20, 40] range and you want them in the
[50, 100] range, you would call
value.remap(from: (20, 40), to: (50, 100)).
20 would become
40 would become
30 would become
CGGeometry and DoubleListRepresentable
CGVector conform to the protocols in
DoubleListRepresentable. This means that they can be initialized and converted to a list of doubles. In the case of
CGRect, this list is exactly four elements long, in all other cases the list should have exactly two elements.
DoubleListRepresentable adds the
magnitude variable and the
lerp methods, which should be quite self explanatory. It also adds four basic arithmetic functions:
/. In the case of the multiplication and division, this is done with a scalar (
Double) that multiplies or divides each component of the element.
Besides the functions and variables provided by
DoubleListRepresentable, each of the four
CGGeometry types contains at least one convenience initializer, which should be very easy to understand, and
CGVector contain variables to transform the value from one type to any of the other two types.
To run the example project, clone the repo, and run
pod install from the Example directory first.
CGMath is available through CocoaPods. To install it, simply add the following line to your Podfile:
Emilio Peláez, firstname.lastname@example.org
CGMath is available under the MIT license. See the LICENSE file for more info.