Skip to content

Instantly share code, notes, and snippets.

@danydodson
Last active November 15, 2022 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danydodson/a2715b8356088590505e2f6802a5e0eb to your computer and use it in GitHub Desktop.
Save danydodson/a2715b8356088590505e2f6802a5e0eb to your computer and use it in GitHub Desktop.
sass calculations
// Plain SASS Trigonometry Algorithm in Taylor Expansion
$pi: 3.14159265359;
$_precision: 10;
@function pow($base, $exp) {
$value: $base;
@if $exp > 1 {
@for $i from 2 through $exp {
$value: $value * $base;
}
}
@if $exp < 1 {
@for $i from 0 through -$exp {
$value: $value / $base;
}
}
@return $value;
}
@function fact($num) {
$fact: 1;
@if $num > 0 {
@for $i from 1 through $num {
$fact: $fact * $i;
}
}
@return $fact;
}
@function _to_unitless_rad($angle) {
@if unit($angle) == "deg" {
$angle: $angle / 180deg * $pi;
}
@if unit($angle) == "rad" {
$angle: $angle / 1rad;
}
@return $angle;
}
@function sin($angle) {
$a: _to_unitless_rad($angle);
$sin: $a;
@for $n from 1 through $_precision {
$sin: $sin + (pow(-1, $n) / fact(2 * $n + 1)) * pow($a, (2 * $n + 1));
}
@return $sin;
}
@function cos($angle) {
$a: _to_unitless_rad($angle);
$cos: 1;
@for $n from 1 through $_precision {
$cos: $cos + (pow(-1, $n) / fact(2 * $n)) * pow($a, 2 * $n);
}
@return $cos;
}
@function tan($angle) {
@return sin($angle) / cos($angle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment