Created
October 28, 2020 09:47
-
-
Save hochan222/576c54c540585dc2481afa7ba00f1c99 to your computer and use it in GitHub Desktop.
math for scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
** File: ft_math | |
** Description: This is the math library you need in css. | |
** Connect: hochan222 - https://github.com/hochan222 | |
** | |
** List: factorial, sin, cos, tan | |
*/ | |
/* | |
** factorial | |
** | |
** Detail: This is a function that implements a factorial operation. | |
** Precautions: px or other units were not processed. | |
** Handle:: - | |
** Cause :: - | |
** | |
** @param number $end Factorial end value | |
** @param number $start Factorial start value | |
** @param number $currentVal Value to be accumulated | |
** @return number Success: Positive integer / Failure: -1 | |
*/ | |
@function factorial($end, $start: 1, $currentVal: 1) { | |
@if type-of($end) != number { | |
@return -1; | |
} @else if $end < 0 { | |
@return -1; | |
} @else if $end == 0 { | |
@return 1; | |
} | |
$accVal: $currentVal; | |
@for $i from $start to $end + 1 { | |
$accVal: $i * $accVal; | |
} | |
@return $accVal; | |
} | |
/* | |
** sin, cos, tan | |
** | |
** Detail: These are a function that created using the Taylor series principle. | |
** - sin (x) = cos (x-90º) | |
** - tan (x) = sin (x) / cos (x) | |
** | |
** | |
** Precautions: - | |
** Handle:: - | |
** Cause :: - | |
** | |
** @param number $rad Radian | |
** @param number $TAYLOR_SERIES_ITER Number of Taylor series to repeat | |
** @return number | |
*/ | |
$PI: 3.14159265358979323846; | |
@function sin($rad, $TAYLOR_SERIES_ITER: 10) { | |
// Normalize radian, 0 to 2π | |
@if $rad < 0 { | |
@return sin((-1) * $rad + $PI, $TAYLOR_SERIES_ITER); | |
} | |
@while ($rad >= 2 * $PI) { | |
$rad: $rad - 2 * $PI; | |
} | |
$curTerm: $rad; | |
$estimated: $curTerm; | |
// Taylor series | |
@for $i from 1 to $TAYLOR_SERIES_ITER + 1 { | |
// Multiply the previous term by -x^2 / (2n * (2n-1)) | |
$curTerm: $curTerm * ((-1) * $rad * $rad) / (2 * $i * (2 * $i + 1)); | |
$estimated: $estimated + $curTerm; | |
} | |
@return $estimated; | |
} | |
@function cos($rad, $TAYLOR_SERIES_ITER: 10) { | |
@return sin($rad + $PI / 2, $TAYLOR_SERIES_ITER); | |
} | |
@function tan($rad, $TAYLOR_SERIES_ITER: 10) { | |
@return sin($rad, $TAYLOR_SERIES_ITER) / cos($rad, $TAYLOR_SERIES_ITER); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment