Skip to content

Instantly share code, notes, and snippets.

@hochan222
Created October 28, 2020 09:47
Show Gist options
  • Save hochan222/576c54c540585dc2481afa7ba00f1c99 to your computer and use it in GitHub Desktop.
Save hochan222/576c54c540585dc2481afa7ba00f1c99 to your computer and use it in GitHub Desktop.
math for scss
/*
** 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