mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-08-28 22:30:40 -04:00
Added new util function that sends tan function infinities correctly
Changed mathop to call new math util Changed sin & cos to round correctly (to get 0) Added testing for the new math util function Added testing for the new mathop functions
This commit is contained in:
parent
fff63e3af2
commit
1ac89f5aa4
4 changed files with 39 additions and 6 deletions
|
@ -1,4 +1,5 @@
|
|||
var Cast = require('../util/cast.js');
|
||||
var MathUtil = require('../util/math-util.js');
|
||||
|
||||
var Scratch3OperatorsBlocks = function (runtime) {
|
||||
/**
|
||||
|
@ -126,9 +127,9 @@ Scratch3OperatorsBlocks.prototype.mathop = function (args) {
|
|||
case 'floor': return Math.floor(n);
|
||||
case 'ceiling': return Math.ceil(n);
|
||||
case 'sqrt': return Math.sqrt(n);
|
||||
case 'sin': return Math.sin((Math.PI * n) / 180);
|
||||
case 'cos': return Math.cos((Math.PI * n) / 180);
|
||||
case 'tan': return Math.tan((Math.PI * n) / 180);
|
||||
case 'sin': return parseFloat(Math.sin((Math.PI * n) / 180).toFixed(10));
|
||||
case 'cos': return parseFloat(Math.cos((Math.PI * n) / 180).toFixed(10));
|
||||
case 'tan': return MathUtil.tan(n);
|
||||
case 'asin': return (Math.asin(n) * 180) / Math.PI;
|
||||
case 'acos': return (Math.acos(n) * 180) / Math.PI;
|
||||
case 'atan': return (Math.atan(n) * 180) / Math.PI;
|
||||
|
|
|
@ -45,4 +45,24 @@ MathUtil.wrapClamp = function (n, min, max) {
|
|||
return n - (Math.floor((n - min) / range) * range);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Convert a value from tan function in degrees.
|
||||
* @param {!number} angle in degrees
|
||||
* @return {!number} Correct tan value
|
||||
*/
|
||||
MathUtil.tan = function (angle) {
|
||||
angle = angle % 360;
|
||||
switch (angle) {
|
||||
case -270:
|
||||
case 90:
|
||||
return Infinity;
|
||||
case -90:
|
||||
case 270:
|
||||
return -Infinity;
|
||||
default:
|
||||
return parseFloat(Math.tan((Math.PI * angle) / 180).toFixed(10));
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = MathUtil;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue