Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. /// leastSquaresFit
  2. /// Calculate the least square fit linear regression of provided values
  3. /// @param {map} $map - A SASS map of viewport width and size value combinations
  4. /// @return Linear equation as a calc() function
  5. /// @example
  6. /// font-size: leastSquaresFit((576: 24, 768: 24, 992: 34));
  7. /// @author Jake Wilson <jake.e.wilson@gmail.com>
  8. @function leastSquaresFit($map) {
  9.  
  10. // Get the number of provided breakpoints
  11. $length: length(map-keys($map));
  12.  
  13. // Error if the number of breakpoints is < 2
  14. @if ($length < 2) {
  15. @error "leastSquaresFit() $map must be at least 2 values"
  16. }
  17.  
  18. // Calculate the Means
  19. $resTotal: 0;
  20. $valueTotal: 0;
  21. @each $res, $value in $map {
  22. $resTotal: $resTotal + $res;
  23. $valueTotal: $valueTotal + $value;
  24. }
  25. $resMean: $resTotal/$length;
  26. $valueMean: $valueTotal/$length;
  27.  
  28. // Calculate some other stuff
  29. $multipliedDiff: 0;
  30. $squaredDiff: 0;
  31. @each $res, $value in $map {
  32.  
  33. // Differences from means
  34. $resDiff: $res - $resMean;
  35. $valueDiff: $value - $valueMean;
  36.  
  37. // Sum of multiplied differences
  38. $multipliedDiff: $multipliedDiff + ($resDiff * $valueDiff);
  39.  
  40. // Sum of squared resolution differences
  41. $squaredDiff: $squaredDiff + ($resDiff * $resDiff);
  42. }
  43.  
  44. // Calculate the Slope
  45. $m: $multipliedDiff / $squaredDiff;
  46.  
  47. // Calculate the Y-Intercept
  48. $b: $valueMean - ($m * $resMean);
  49.  
  50. // Return the CSS calc equation
  51. @return calc(#{$m*100}vw + #{$b}px);
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement