mirror of
https://github.com/google/blockly.git
synced 2026-01-10 10:27:08 +01:00
Add new atan2 block (PR #2079)
Merge pull request from nadyafebi/add-atan2-support-1835
This commit is contained in:
@@ -372,6 +372,29 @@ Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT
|
||||
"colour": "%{BKY_MATH_HUE}",
|
||||
"tooltip": "%{BKY_MATH_RANDOM_FLOAT_TOOLTIP}",
|
||||
"helpUrl": "%{BKY_MATH_RANDOM_FLOAT_HELPURL}"
|
||||
},
|
||||
|
||||
// Block for calculating atan2 of [X] and [Y].
|
||||
{
|
||||
"type": "math_atan2",
|
||||
"message0": "%{BKY_MATH_ATAN2_TITLE}",
|
||||
"args0": [
|
||||
{
|
||||
"type": "input_value",
|
||||
"name": "X",
|
||||
"check": "Number"
|
||||
},
|
||||
{
|
||||
"type": "input_value",
|
||||
"name": "Y",
|
||||
"check": "Number"
|
||||
}
|
||||
],
|
||||
"inputsInline": true,
|
||||
"output": "Number",
|
||||
"colour": "%{BKY_MATH_HUE}",
|
||||
"tooltip": "%{BKY_MATH_ATAN2_TOOLTIP}",
|
||||
"helpUrl": "%{BKY_MATH_ATAN2_HELPURL}"
|
||||
}
|
||||
]); // END JSON EXTRACT (Do not delete this comment.)
|
||||
|
||||
|
||||
@@ -200,6 +200,18 @@
|
||||
</value>
|
||||
</block>
|
||||
<block type="math_random_float"></block>
|
||||
<block type="math_atan2">
|
||||
<value name="X">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">1</field>
|
||||
</shadow>
|
||||
</value>
|
||||
<value name="Y">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">1</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
</category>
|
||||
<category name="%{BKY_CATTEXT}" colour="%{BKY_TEXTS_HUE}">
|
||||
<block type="text"></block>
|
||||
|
||||
@@ -145,6 +145,18 @@
|
||||
</value>
|
||||
</block>
|
||||
<block type="math_random_float"></block>
|
||||
<block type="math_atan2">
|
||||
<value name="X">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">1</field>
|
||||
</shadow>
|
||||
</value>
|
||||
<value name="Y">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">1</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
</category>
|
||||
<category name="Variables" colour="%{BKY_VARIABLES_HUE}">
|
||||
<block type="graph_get_x"></block>
|
||||
|
||||
@@ -117,6 +117,7 @@
|
||||
</value>
|
||||
</block>
|
||||
<block type="math_random_float"></block>
|
||||
<block type="math_atan2"></block>
|
||||
</category>
|
||||
<category name="نص" colour="%{BKY_TEXTS_HUE}">
|
||||
<block type="text"></block>
|
||||
|
||||
@@ -114,6 +114,7 @@
|
||||
</value>
|
||||
</block>
|
||||
<block type="math_random_float"></block>
|
||||
<block type="math_atan2"></block>
|
||||
</category>
|
||||
<category name="Lists" colour="%{BKY_LISTS_HUE}">
|
||||
<block type="lists_create_empty"></block>
|
||||
|
||||
@@ -485,3 +485,15 @@ Blockly.Dart['math_random_float'] = function(block) {
|
||||
'import \'dart:math\' as Math;';
|
||||
return ['new Math.Random().nextDouble()', Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['math_atan2'] = function(block) {
|
||||
// Arctangent of point (X, Y) in degrees from -180 to 180.
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'X',
|
||||
Blockly.Dart.ORDER_NONE) || '0';
|
||||
var argument1 = Blockly.Dart.valueToCode(block, 'Y',
|
||||
Blockly.Dart.ORDER_NONE) || '0';
|
||||
return ['Math.atan2(' + argument1 + ', ' + argument0 + ') / Math.pi * 180',
|
||||
Blockly.Dart.ORDER_MULTIPLICATIVE];
|
||||
};
|
||||
|
||||
@@ -411,3 +411,13 @@ Blockly.JavaScript['math_random_float'] = function(block) {
|
||||
// Random fraction between 0 and 1.
|
||||
return ['Math.random()', Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['math_atan2'] = function(block) {
|
||||
// Arctangent of point (X, Y) in degrees from -180 to 180.
|
||||
var argument0 = Blockly.JavaScript.valueToCode(block, 'X',
|
||||
Blockly.JavaScript.ORDER_COMMA) || '0';
|
||||
var argument1 = Blockly.JavaScript.valueToCode(block, 'Y',
|
||||
Blockly.JavaScript.ORDER_COMMA) || '0';
|
||||
return ['Math.atan2(' + argument1 + ', ' + argument0 + ') / Math.PI * 180',
|
||||
Blockly.JavaScript.ORDER_DIVISION];
|
||||
};
|
||||
|
||||
@@ -425,3 +425,13 @@ Blockly.Lua['math_random_float'] = function(block) {
|
||||
// Random fraction between 0 and 1.
|
||||
return ['math.random()', Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['math_atan2'] = function(block) {
|
||||
// Arctangent of point (X, Y) in degrees from -180 to 180.
|
||||
var argument0 = Blockly.Lua.valueToCode(block, 'X',
|
||||
Blockly.Lua.ORDER_NONE) || '0';
|
||||
var argument1 = Blockly.Lua.valueToCode(block, 'Y',
|
||||
Blockly.Lua.ORDER_NONE) || '0';
|
||||
return ['math.deg(math.atan2(' + argument1 + ', ' + argument0 + '))',
|
||||
Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
@@ -372,3 +372,13 @@ Blockly.PHP['math_random_float'] = function(block) {
|
||||
// Random fraction between 0 and 1.
|
||||
return ['(float)rand()/(float)getrandmax()', Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['math_atan2'] = function(block) {
|
||||
// Arctangent of point (X, Y) in degrees from -180 to 180.
|
||||
var argument0 = Blockly.PHP.valueToCode(block, 'X',
|
||||
Blockly.PHP.ORDER_COMMA) || '0';
|
||||
var argument1 = Blockly.PHP.valueToCode(block, 'Y',
|
||||
Blockly.PHP.ORDER_COMMA) || '0';
|
||||
return ['atan2(' + argument1 + ', ' + argument0 + ') / pi() * 180',
|
||||
Blockly.PHP.ORDER_DIVISION];
|
||||
};
|
||||
@@ -386,3 +386,14 @@ Blockly.Python['math_random_float'] = function(block) {
|
||||
Blockly.Python.definitions_['import_random'] = 'import random';
|
||||
return ['random.random()', Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.Python['math_atan2'] = function(block) {
|
||||
// Arctangent of point (X, Y) in degrees from -180 to 180.
|
||||
Blockly.Python.definitions_['import_math'] = 'import math';
|
||||
var argument0 = Blockly.Python.valueToCode(block, 'X',
|
||||
Blockly.Python.ORDER_NONE) || '0';
|
||||
var argument1 = Blockly.Python.valueToCode(block, 'Y',
|
||||
Blockly.Python.ORDER_NONE) || '0';
|
||||
return ['math.atan2(' + argument1 + ', ' + argument0 + ') / math.pi * 180',
|
||||
Blockly.Python.ORDER_MULTIPLICATIVE];
|
||||
};
|
||||
|
||||
@@ -557,6 +557,13 @@ Blockly.Msg.MATH_RANDOM_FLOAT_TITLE_RANDOM = 'random fraction';
|
||||
/// tooltip - Return a random fraction between 0 and 1. The value may be equal to 0 but must be less than 1.
|
||||
Blockly.Msg.MATH_RANDOM_FLOAT_TOOLTIP = 'Return a random fraction between 0.0 (inclusive) and 1.0 (exclusive).';
|
||||
|
||||
/// {{Optional}} url - Information about how to calculate atan2.
|
||||
Blockly.Msg.MATH_ATAN2_HELPURL = 'https://en.wikipedia.org/wiki/Atan2';
|
||||
/// block text - The title of the block that calculates atan2 of point (X, Y). For example, if the point is (-1, -1), this returns -135. %1 is a placeholder for the X coordinate, %2 is the placeholder for the Y coordinate.
|
||||
Blockly.Msg.MATH_ATAN2_TITLE = 'atan2 of X:%1 Y:%2';
|
||||
/// tooltip - Return the arctangent of point (X, Y) in degrees from -180 to 180. For example, if the point is (-1, -1) this returns -135.
|
||||
Blockly.Msg.MATH_ATAN2_TOOLTIP = 'Return the arctangent of point (X, Y) in degrees from -180 to 180.';
|
||||
|
||||
// Text Blocks.
|
||||
/// {{Optional}} url - Information about how computers represent text (sometimes referred to as ''string''s).
|
||||
Blockly.Msg.TEXT_TEXT_HELPURL = 'https://en.wikipedia.org/wiki/String_(computer_science)';
|
||||
|
||||
@@ -320,6 +320,7 @@ h1 {
|
||||
<block type="math_constrain"></block>
|
||||
<block type="math_random_int"></block>
|
||||
<block type="math_random_float"></block>
|
||||
<block type="math_atan2"></block>
|
||||
</category>
|
||||
<category name="Text" colour="160">
|
||||
<block type="text"></block>
|
||||
|
||||
@@ -36,6 +36,11 @@
|
||||
<next>
|
||||
<block type="procedures_callnoreturn">
|
||||
<mutation name="test random fraction"></mutation>
|
||||
<next>
|
||||
<block type="procedures_callnoreturn">
|
||||
<mutation name="test atan2"></mutation>
|
||||
</block>
|
||||
</next>
|
||||
</block>
|
||||
</next>
|
||||
</block>
|
||||
@@ -1881,4 +1886,64 @@
|
||||
</block>
|
||||
</statement>
|
||||
</block>
|
||||
<block type="procedures_defnoreturn" y="5938" x="13">
|
||||
<field name="NAME">test atan2</field>
|
||||
<comment w="160" h="80" pinned="false">Describe this function...</comment>
|
||||
<statement name="STACK">
|
||||
<block type="unittest_assertequals">
|
||||
<value name="MESSAGE">
|
||||
<block type="text">
|
||||
<field name="TEXT">atan2</field>
|
||||
</block>
|
||||
</value>
|
||||
<value name="ACTUAL">
|
||||
<block type="math_atan2">
|
||||
<value name="X">
|
||||
<block type="math_number">
|
||||
<field name="NUM">-5</field>
|
||||
</block>
|
||||
</value>
|
||||
<value name="Y">
|
||||
<block type="math_number">
|
||||
<field name="NUM">5</field>
|
||||
</block>
|
||||
</value>
|
||||
</block>
|
||||
</value>
|
||||
<value name="EXPECTED">
|
||||
<block type="math_number">
|
||||
<field name="NUM">135</field>
|
||||
</block>
|
||||
</value>
|
||||
<next>
|
||||
<block type="unittest_assertequals">
|
||||
<value name="MESSAGE">
|
||||
<block type="text">
|
||||
<field name="TEXT">atan2</field>
|
||||
</block>
|
||||
</value>
|
||||
<value name="ACTUAL">
|
||||
<block type="math_atan2">
|
||||
<value name="X">
|
||||
<block type="math_number">
|
||||
<field name="NUM">0</field>
|
||||
</block>
|
||||
</value>
|
||||
<value name="Y">
|
||||
<block type="math_number">
|
||||
<field name="NUM">-12</field>
|
||||
</block>
|
||||
</value>
|
||||
</block>
|
||||
</value>
|
||||
<value name="EXPECTED">
|
||||
<block type="math_number">
|
||||
<field name="NUM">-90</field>
|
||||
</block>
|
||||
</value>
|
||||
</block>
|
||||
</next>
|
||||
</block>
|
||||
</statement>
|
||||
</block>
|
||||
</xml>
|
||||
@@ -289,6 +289,18 @@ h1 {
|
||||
</value>
|
||||
</block>
|
||||
<block type="math_random_float"></block>
|
||||
<block type="math_atan2">
|
||||
<value name="X">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">1</field>
|
||||
</shadow>
|
||||
</value>
|
||||
<value name="Y">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">1</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
</category>
|
||||
<category name="Text" colour="%{BKY_TEXTS_HUE}">
|
||||
<block type="text"></block>
|
||||
|
||||
@@ -519,6 +519,18 @@ h1 {
|
||||
</value>
|
||||
</block>
|
||||
<block type="math_random_float"></block>
|
||||
<block type="math_atan2">
|
||||
<value name="X">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">1</field>
|
||||
</shadow>
|
||||
</value>
|
||||
<value name="Y">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">1</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
</category>
|
||||
<category name="Text" colour="%{BKY_TEXTS_HUE}">
|
||||
<block type="text"></block>
|
||||
@@ -848,6 +860,18 @@ h1 {
|
||||
</value>
|
||||
</block>
|
||||
<block type="math_random_float"></block>
|
||||
<block type="math_atan2">
|
||||
<value name="X">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">1</field>
|
||||
</shadow>
|
||||
</value>
|
||||
<value name="Y">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">1</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
</category>
|
||||
<category name="Text" colour="%{BKY_TEXTS_HUE}">
|
||||
<block type="text"></block>
|
||||
|
||||
@@ -202,6 +202,18 @@ h1 {
|
||||
</value>
|
||||
</block>
|
||||
<block type="math_random_float"></block>
|
||||
<block type="math_atan2">
|
||||
<value name="X">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">1</field>
|
||||
</shadow>
|
||||
</value>
|
||||
<value name="Y">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">1</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
</category>
|
||||
<category name="Text" colour="160">
|
||||
<block type="text"></block>
|
||||
|
||||
Reference in New Issue
Block a user