2022-05-20 11:42:06 +02:00
|
|
|
// ==========================================================================
|
|
|
|
|
// Tools / Maths
|
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
2023-01-05 09:58:01 +01:00
|
|
|
// Remove the unit of a length
|
2022-05-25 13:57:30 +02:00
|
|
|
//
|
2023-01-05 09:58:01 +01:00
|
|
|
// @param {Number} $number Number to remove unit from
|
|
|
|
|
// @return {function<number>}
|
2023-06-08 10:57:59 +02:00
|
|
|
@function strip-unit($value) {
|
2023-01-05 09:58:01 +01:00
|
|
|
@if type-of($value) != "number" {
|
|
|
|
|
@error "Invalid `#{type-of($value)}` type. Choose a number type instead.";
|
2023-06-08 10:57:59 +02:00
|
|
|
} @else if type-of($value) == "number" and not is-unitless($value) {
|
|
|
|
|
@return math.div($value, $value * 0 + 1);
|
2023-01-05 09:58:01 +01:00
|
|
|
}
|
2022-05-20 11:42:06 +02:00
|
|
|
|
2023-06-08 10:57:59 +02:00
|
|
|
@return $value;
|
2022-05-20 11:42:06 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 16:02:15 +02:00
|
|
|
// Returns the square root of the given number.
|
2022-05-20 11:42:06 +02:00
|
|
|
//
|
2022-05-25 16:02:15 +02:00
|
|
|
// @param {number} $number The number to calculate.
|
|
|
|
|
// @return {number}
|
2022-05-20 11:42:06 +02:00
|
|
|
|
|
|
|
|
@function sqrt($number) {
|
|
|
|
|
$x: 1;
|
|
|
|
|
$value: $x;
|
2022-05-25 13:57:30 +02:00
|
|
|
|
2022-05-20 11:42:06 +02:00
|
|
|
@for $i from 1 through 10 {
|
2023-04-05 14:40:28 +02:00
|
|
|
$value: $x - math.div(($x * $x - abs($number)), (2 * $x));
|
2022-05-25 13:57:30 +02:00
|
|
|
$x: $value;
|
2022-05-20 11:42:06 +02:00
|
|
|
}
|
2022-05-25 13:57:30 +02:00
|
|
|
|
2022-05-20 11:42:06 +02:00
|
|
|
@return $value;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-01 11:02:26 +02:00
|
|
|
// Returns a number raised to the power of an exponent.
|
2022-05-20 11:42:06 +02:00
|
|
|
//
|
2022-06-01 11:02:26 +02:00
|
|
|
// @param {number} $number The base number.
|
|
|
|
|
// @param {number} $exp The exponent.
|
|
|
|
|
// @return {number}
|
2022-05-20 11:42:06 +02:00
|
|
|
|
|
|
|
|
@function pow($number, $exp) {
|
|
|
|
|
$value: 1;
|
2022-05-25 13:57:30 +02:00
|
|
|
|
2022-05-20 11:42:06 +02:00
|
|
|
@if $exp > 0 {
|
|
|
|
|
@for $i from 1 through $exp {
|
|
|
|
|
$value: $value * $number;
|
|
|
|
|
}
|
|
|
|
|
} @else if $exp < 0 {
|
|
|
|
|
@for $i from 1 through -$exp {
|
2023-04-05 14:40:28 +02:00
|
|
|
$value: math.div($value, $number);
|
2022-05-20 11:42:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-05-25 13:57:30 +02:00
|
|
|
|
2022-05-20 11:42:06 +02:00
|
|
|
@return $value;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-01 11:02:26 +02:00
|
|
|
// Returns the factorial of the given number.
|
2022-05-20 11:42:06 +02:00
|
|
|
//
|
2022-06-01 11:02:26 +02:00
|
|
|
// @param {number} $number The number to calculate.
|
|
|
|
|
// @return {number}
|
2022-05-20 11:42:06 +02:00
|
|
|
|
|
|
|
|
@function fact($number) {
|
|
|
|
|
$value: 1;
|
2022-05-25 13:57:30 +02:00
|
|
|
|
2022-05-20 11:42:06 +02:00
|
|
|
@if $number > 0 {
|
|
|
|
|
@for $i from 1 through $number {
|
|
|
|
|
$value: $value * $i;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-25 13:57:30 +02:00
|
|
|
|
2022-05-20 11:42:06 +02:00
|
|
|
@return $value;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-01 11:02:26 +02:00
|
|
|
// Returns an approximation of pi, with 11 decimals.
|
2022-05-20 11:42:06 +02:00
|
|
|
//
|
2022-06-01 11:02:26 +02:00
|
|
|
// @return {number}
|
2022-05-20 11:42:06 +02:00
|
|
|
|
|
|
|
|
@function pi() {
|
|
|
|
|
@return 3.14159265359;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 16:02:15 +02:00
|
|
|
// Converts the number in degrees to the radian equivalent .
|
2022-05-20 11:42:06 +02:00
|
|
|
//
|
2022-05-25 16:02:15 +02:00
|
|
|
// @param {number} $angle The angular value to calculate.
|
|
|
|
|
// @return {number} If $angle has the `deg` unit,
|
|
|
|
|
// the radian equivalent is returned.
|
|
|
|
|
// Otherwise, the unitless value of $angle is returned.
|
2022-05-20 11:42:06 +02:00
|
|
|
|
|
|
|
|
@function rad($angle) {
|
|
|
|
|
$unit: unit($angle);
|
2022-05-25 16:02:15 +02:00
|
|
|
$angle: strip-units($angle);
|
2022-05-25 13:57:30 +02:00
|
|
|
|
2022-05-25 16:02:15 +02:00
|
|
|
// If the angle has `deg` as unit, convert to radians.
|
|
|
|
|
@if ($unit == deg) {
|
2023-04-05 14:40:28 +02:00
|
|
|
@return math.div($angle, 180) * pi();
|
2022-05-20 11:42:06 +02:00
|
|
|
}
|
2022-05-25 13:57:30 +02:00
|
|
|
|
2022-05-25 16:02:15 +02:00
|
|
|
@return $angle;
|
2022-05-20 11:42:06 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 13:57:30 +02:00
|
|
|
// Returns the sine of the given number.
|
2022-05-20 11:42:06 +02:00
|
|
|
//
|
2022-05-25 13:57:30 +02:00
|
|
|
// @param {number} $angle The angle to calculate.
|
|
|
|
|
// @return {number}
|
2022-05-20 11:42:06 +02:00
|
|
|
|
|
|
|
|
@function sin($angle) {
|
|
|
|
|
$sin: 0;
|
|
|
|
|
$angle: rad($angle);
|
2022-05-25 13:57:30 +02:00
|
|
|
|
2022-05-20 11:42:06 +02:00
|
|
|
@for $i from 0 through 10 {
|
2023-04-05 14:40:28 +02:00
|
|
|
$sin: $sin + pow(-1, $i) * math.div(pow($angle, (2 * $i + 1)), fact(2 * $i + 1));
|
2022-05-20 11:42:06 +02:00
|
|
|
}
|
2022-05-25 13:57:30 +02:00
|
|
|
|
2022-05-20 11:42:06 +02:00
|
|
|
@return $sin;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 13:57:30 +02:00
|
|
|
// Returns the cosine of the given number.
|
2022-05-20 11:42:06 +02:00
|
|
|
//
|
2022-05-25 13:57:30 +02:00
|
|
|
// @param {string} $angle The angle to calculate.
|
|
|
|
|
// @return {number}
|
2022-05-20 11:42:06 +02:00
|
|
|
|
|
|
|
|
@function cos($angle) {
|
|
|
|
|
$cos: 0;
|
|
|
|
|
$angle: rad($angle);
|
2022-05-25 13:57:30 +02:00
|
|
|
|
2022-05-20 11:42:06 +02:00
|
|
|
@for $i from 0 through 10 {
|
2023-04-05 14:40:28 +02:00
|
|
|
$cos: $cos + pow(-1, $i) * math.div(pow($angle, 2 * $i), fact(2 * $i));
|
2022-05-20 11:42:06 +02:00
|
|
|
}
|
2022-05-25 13:57:30 +02:00
|
|
|
|
2022-05-20 11:42:06 +02:00
|
|
|
@return $cos;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 13:57:30 +02:00
|
|
|
// Returns the tangent of the given number.
|
2022-05-20 11:42:06 +02:00
|
|
|
//
|
2022-05-25 13:57:30 +02:00
|
|
|
// @param {string} $angle The angle to calculate.
|
|
|
|
|
// @return {number}
|
2022-05-20 11:42:06 +02:00
|
|
|
|
|
|
|
|
@function tan($angle) {
|
2023-04-05 14:40:28 +02:00
|
|
|
@return math.div(sin($angle), cos($angle));
|
2022-05-20 11:42:06 +02:00
|
|
|
}
|