From c2db2e1922d79a34f1c5a3a8787474d9f39714ae Mon Sep 17 00:00:00 2001 From: Lucas Vallenet Date: Fri, 20 May 2022 11:42:06 +0200 Subject: [PATCH] Add SCSS Math functions --- assets/styles/main.scss | 1 + assets/styles/tools/_maths.scss | 123 ++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 assets/styles/tools/_maths.scss diff --git a/assets/styles/main.scss b/assets/styles/main.scss index db4f9bb..89910d3 100644 --- a/assets/styles/main.scss +++ b/assets/styles/main.scss @@ -13,6 +13,7 @@ // Tools // ========================================================================== +@import "tools/maths"; @import "tools/functions"; @import "tools/mixins"; @import "tools/fonts"; diff --git a/assets/styles/tools/_maths.scss b/assets/styles/tools/_maths.scss new file mode 100644 index 0000000..8396353 --- /dev/null +++ b/assets/styles/tools/_maths.scss @@ -0,0 +1,123 @@ +// ========================================================================== +// Tools / Maths +// ========================================================================== + +// Remove units from the given number +// @param {number} $number The number with units +// @return {number} + +@function strip-units($number) { + @return $number / ($number * 0 + 1); +} + +// Square root function +// +// @param {string} $number The number +// @return {string} The sqrt number + +@function sqrt($number) { + $x: 1; + $value: $x; + @for $i from 1 through 10 { + $value: $x - ($x * $x - abs($number)) / (2 * $x); + $x: $value; + } + @return $value; +} + +// Power function +// +// @param {number} $number Number to apply power +// @param {number} $exp The exponant for the power +// @return {number} The powered number + +@function pow($number, $exp) { + $value: 1; + @if $exp > 0 { + @for $i from 1 through $exp { + $value: $value * $number; + } + } @else if $exp < 0 { + @for $i from 1 through -$exp { + $value: $value / $number; + } + } + @return $value; +} + +// Factorial function +// +// @param {number} $number The number to factorize +// @return {number} The factorised number + +@function fact($number) { + $value: 1; + @if $number > 0 { + @for $i from 1 through $number { + $value: $value * $i; + } + } + @return $value; +} + +// Pi reference +// +// @return {number} Retrun PI with 11 decimals + +@function pi() { + @return 3.14159265359; +} + +// Convert deg to rad +// +// @param {string} $angle The angle to convert +// @return {number} The unitless angle converted to rad + +@function rad($angle) { + $unit: unit($angle); + $unitless: $angle / ($angle * 0 + 1); + // If the angle has 'deg' as unit, convert to radians. + @if $unit == deg { + $unitless: $unitless / 180 * pi(); + } + @return $unitless; +} + +// Calculate the sinus of an angle +// +// @param {string} $angle The angle to compute +// @return {number} The sinus of the given angle + +@function sin($angle) { + $sin: 0; + $angle: rad($angle); + // Iterate a bunch of times. + @for $i from 0 through 10 { + $sin: $sin + pow(-1, $i) * pow($angle, (2 * $i + 1)) / fact(2 * $i + 1); + } + @return $sin; +} + +// Calculate the cosinus of an angle +// +// @param {string} $angle The angle to compute +// @return {number} The cosinus of the given angle + +@function cos($angle) { + $cos: 0; + $angle: rad($angle); + // Iterate a bunch of times. + @for $i from 0 through 10 { + $cos: $cos + pow(-1, $i) * pow($angle, 2 * $i) / fact(2 * $i); + } + @return $cos; +} + +// Calculate the tangent of an angle +// +// @param {string} $angle The angle to compute +// @return {number} The tangent of the given angle + +@function tan($angle) { + @return sin($angle) / cos($angle); +}