Files
blockly/core/utils/math.js
Neil Fraser d1a29739d8 Move utilities into own directory.
TODO: There are more functions which may be migrated into dom and string.
2019-06-07 10:32:57 -07:00

71 lines
2.0 KiB
JavaScript

/**
* @license
* Visual Blocks Editor
*
* Copyright 2019 Google Inc.
* https://developers.google.com/blockly/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Utility methods for math.
* These methods are not specific to Blockly, and could be factored out into
* a JavaScript framework such as Closure.
* @author fraser@google.com (Neil Fraser)
*/
'use strict';
/**
* @name Blockly.utils.math
* @namespace
*/
goog.provide('Blockly.utils.math');
/**
* Converts degrees to radians.
* Copied from Closure's goog.math.toRadians.
* @param {number} angleDegrees Angle in degrees.
* @return {number} Angle in radians.
*/
Blockly.utils.math.toRadians = function(angleDegrees) {
return angleDegrees * Math.PI / 180;
};
/**
* Converts radians to degrees.
* Copied from Closure's goog.math.toDegrees.
* @param {number} angleRadians Angle in radians.
* @return {number} Angle in degrees.
*/
Blockly.utils.math.toDegrees = function(angleRadians) {
return angleRadians * 180 / Math.PI;
};
/**
* Clamp the provided number between the lower bound and the upper bound.
* @param {number} lowerBound The desired lower bound.
* @param {number} number The number to clamp.
* @param {number} upperBound The desired upper bound.
* @return {number} The clamped number.
*/
Blockly.utils.math.clamp = function(lowerBound, number, upperBound) {
if (upperBound < lowerBound) {
var temp = upperBound;
upperBound = lowerBound;
lowerBound = temp;
}
return Math.max(lowerBound, Math.min(number, upperBound));
};