【Google Blocklyを使って遊ぼう】math_blocks.jsonのブロック

このエントリーを Google ブックマーク に追加
Pocket
[`yahoo` not found]

Blocklyに最初から組み込まれているブロックが沢山あります。
今回はmath_blocks.jsonに定義されているブロックについて説明します。
このファイルには数値を操作するブロックが定義されています。

math_numberについて

math_numberは数値を扱うブロックです。
また、このブロックには一つの入力フィールドが存在し、この入力フィールドの内容が出力端子に出力されます。
何も設定されなかった場合は0が出力されます。

math_numberの定義

  {
    "type": "math_number",
    "message0": "%1",
    "args0": [
      {
        "type": "field_number",
        "name": "NUM",
        "text": "0"
      }
    ],
    "colour": 230,
    "output": "Number",
    "tooltip": "A number.",
    "helpUrl": "https://en.wikipedia.org/wiki/Number"
  }

math_numberの生成コード


// 一つ目の入力フィールドを出力する
[出力端子] = [入力フィールド値];

math_arithmeticについて

math_arithmeticは2つの数値に対して演算を行うブロックです。
このブロックは2つの入力端子と一つのセレクトボックスがあります。
2つの入力端子には数値ブロックを設定することができます。
また、セレクトボックスで「和(+)」・「(差-)」・「積(×)」・「商(÷)」・「乗(^)」を選択することができます。

math_arithmeticの定義

  {
    "type": "math_arithmetic",
    "message0": "%1 %2 %3",
    "args0": [
      {
        "type": "input_value",
        "name": "A",
        "check": "Number"
      },
      {
        "type": "field_dropdown",
        "name": "OP",
        "options": [
          ["+", "ADD"],
          ["-", "MINUS"],
          ["×", "MULTIPLY"],
          ["÷", "DIVIDE"],
          ["^", "POWER"]
        ]
      },
      {
        "type": "input_value",
        "name": "B",
        "check": "Number"
      }
    ],
    "inputsInline": true,
    "output": "Number",
    "colour": 230,
    "helpUrl": "https://en.wikipedia.org/wiki/Arithmetic"
  }

math_arithmeticの生成コード


// +を選択
[出力端子] = [1つ目の入力端子] + [2つ目の入力端子];

// -を選択
[出力端子] = [1つ目の入力端子] - [2つ目の入力端子];

// ×を選択
[出力端子] = [1つ目の入力端子] × [2つ目の入力端子];

// ÷を選択
[出力端子] = [1つ目の入力端子] ÷ [2つ目の入力端子];

// ^を選択
[出力端子] = Math.pow([1つ目の入力端子],[2つ目の入力端子]);

math_singleについて

math_singleはMathオブジェクトのメソッドの中で引数が一つのものを設定するブロックです。
このブロックは1つの入力端子と1つのセレクトボックスがあります。
セレクトボックスにはルートを求めたり、絶対値を求めることができます。

math_singleの定義

  {
    "type": "math_single",
    "message0": "%1 %2",
    "args0": [
      {
        "type": "field_dropdown",
        "name": "OP",
        "options": [
          ["square root", "ROOT"],
          ["absolute", "ABS"],
          ["-", "NEG"],
          ["ln", "LN"],
          ["log10", "LOG10"],
          ["e^", "EXP"],
          ["10^", "POW10"]
        ]
      },
      {
        "type": "input_value",
        "name": "NUM",
        "check": "Number"
      }
    ],
    "output": "Number",
    "colour": 230,
    "helpUrl": "https://en.wikipedia.org/wiki/Square_root"
  }

math_singleの生成コード


// square rootを選択
[出力端子] = Math.sqrt([1つ目の入力端子]);

// absoluteを選択
[出力端子] = Math.abs([1つ目の入力端子]);

// -を選択
[出力端子] = -[1つ目の入力端子];

// lnを選択
[出力端子] = Math.log([1つ目の入力端子]);

// log10を選択
[出力端子] = Math.log([1つ目の入力端子]) / Math.log(10);

// e^を選択
[出力端子] = Math.exp([1つ目の入力端子]);

// 10^を選択
[出力端子] = Math.pow(10 , [1つ目の入力端子]);

math_trigについて

math_trigは三角関数を表すブロックです。
このブロックは1つの入力端子と1つのセレクトボックスがあります。
セレクトボックスにはsin・cosなどを選択することができます。

math_trigの定義

  {
    "type": "math_trig",
    "message0": "%1 %2",
    "args0": [
      {
        "type": "field_dropdown",
        "name": "OP",
        "options": [
          ["sin", "SIN"],
          ["cos", "COS"],
          ["tan", "TAN"],
          ["asin", "ASIN"],
          ["acos", "ACOS"],
          ["atan", "ATAN"]
        ]
      },
      {
        "type": "input_value",
        "name": "NUM",
        "check": "Number"
      }
    ],
    "output": "Number",
    "colour": 230,
    "helpUrl": "https://en.wikipedia.org/wiki/Trigonometric_functions"
  }

math_trigの生成コード


// sinを選択
[出力端子] = Math.sin([1つ目の入力端子] / 180 * Math.PI);

// cosを選択
[出力端子] = Math.cos([1つ目の入力端子] / 180 * Math.PI);

// tanを選択
[出力端子] = Math.tan([1つ目の入力端子] / 180 * Math.PI);

// asinを選択
[出力端子] = Math.asin([1つ目の入力端子]) / Math.PI *180;

// acosを選択
[出力端子] = Math.acos([1つ目の入力端子]) / Math.PI *180;

// atanを選択
[出力端子] = Math.atan([1つ目の入力端子]) / Math.PI *180;

math_constantについて

math_constantは数学定数を表すブロックです。

math_constantの定義

  {
    "type": "math_constant",
    "message0": "%1",
    "args0": [
      {
        "type": "field_dropdown",
        "name": "CONSTANT",
        "options": [
          ["\u03c0", "PI"],
          ["e", "E"],
          [
            "\u03c6", "GOLDEN_RATIO"],
          [
            "sqrt(2)", "SQRT2"],
          [
            "sqrt(\u00bd)", "SQRT1_2"],
          [
            "\u221e", "INFINITY"]
        ]
      }
    ],
    "output": "Number",
    "colour": 230,
    "tooltip": "Return one of the common constants: π (3.141…), e (2.718…), φ (1.618…), sqrt(2) (1.414…), sqrt(½) (0.707…), or ∞ (infinity).",
    "helpUrl": "https://en.wikipedia.org/wiki/Mathematical_constant"
  }

math_constantの生成コード


// πを選択
[出力端子] = Math.PI;

// eを選択
[出力端子] = Math.E;

// ψを選択
[出力端子] = (1 + Math.sqrt(5)) / 2;

// sort(2)を選択
[出力端子] = Math.SQRT2;

// sort(1/2)を選択
[出力端子] = Math.SQRT1_2;

// ∞を選択
[出力端子] = Infinity;

math_number_propertyについて

math_number_propertyは数字がどのような性質を持つかを判定するブロックです。
このブロックは1つの入力端子と1つのセレクトボックスがあります。
入力端子で指定した数値に対して、セレクトボックスの内容で判断し真偽値を返却します。
セレクトボックスには偶数奇数などを選択することができます。
セレクトボックスで「divisible by」を選ぶと入力端子が2つになりますが、バグがあるようでうまくコード生成できず0除算が発生します。

math_number_propertyの定義

  {
    "type": "math_number_property",
    "message0": "%1 is %2",
    "args0": [
      {
        "type": "input_value",
        "name": "NUMBER_TO_CHECK",
        "check": "Number"
      },
      {
        "type": "field_dropdown",
        "name": "PROPERTY",
        "options": [
          ["even", "EVEN"],
          ["odd", "ODD"],
          ["prime", "PRIME"],
          ["whole", "WHOLE"],
          ["positive", "POSITIVE"],
          ["negative", "NEGATIVE"],
          ["divisible by", "DIVISIBLE_BY"]
        ]
      }
    ],
    "inputsInline": true,
    "colour": 230,
    "output": "Boolean",
    "tooltip": "Check if a number is an even, odd, prime, whole, positive, or negative. Returns true or false.",
    "TODO(#376)": "https://github.com/google/blockly-android/issues/376",
    "mutator": "math_is_divisibleby_mutator"
  }

math_number_propertyの生成コード


// evenを選択、偶数判定を行います。
[出力端子] = [1つ目の入力端子] % 2 == 0;

// oddを選択、奇数判定を行います。
[出力端子] = [1つ目の入力端子] % 2 == 1;

// primeを選択、素数判定を行います。
function mathIsPrime(n) {
    // https://en.wikipedia.org/wiki/Primality_test#Naive_methods
    if (n == 2 || n == 3) {
        return true;
    }
    // False if n is NaN, negative, is 1, or not whole.
    // And false if n is divisible by 2 or 3.
    if (isNaN(n) || n <= 1 || n % 1 != 0 || n % 2 == 0 || n % 3 == 0) {
        return false;
    }
    // Check all the numbers of form 6k +/- 1, up to sqrt(n).
    for (var x = 6; x <= Math.sqrt(n) + 1; x += 6) {
        if (n % (x - 1) == 0 || n % (x + 1) == 0) {
            return false;
        }
    }
    return true;
}
                                                          
[出力端子] = mathIsPrime([1つ目の入力端子]);

// wholeを選択、整数判定を行います。
[出力端子] = [1つ目の入力端子] % 1 == 0;

// positiveを選択、正の数判定を行います。
[出力端子] = [1つ目の入力端子] > 0;

// negativeを選択、正の数判定を行います。
[出力端子] = [1つ目の入力端子] < 0;

// divisible byを選択、1つ目の入力端子が2つ目の入力端子で割り切れるかを判定します。
// ただしコード生成に不具合があるらしく2つ目の入力端子に何を入れても0になってしまい、0除算が発生する。
[出力端子] = [1つ目の入力端子] % [2つ目の入力端子] == 0;

math_changeについて

math_changeは変数の値を数値に変化させ、数値を加算するブロックです。
このブロックは1つの入力端子と1つのセレクトボックスがあります。
セレクトボックスはデフォルトで「item」という変数名が設定されています。
変数名はセレクトボックスで「定義済みの変数名」か「新しい変数名」のどちらかを設定することができます。
「定義済みの変数名」の場合はを数値に変化させ入力端子の値を加算し、「新しい変数名」の場合は変数の定義と入力端子の値の代入が行われます。

math_changeの定義

  {
    "type": "math_change",
    "message0": "change %1 by %2",
    "args0": [
      {
        "type": "field_variable",
        "name": "VAR",
        "variable": "item"
      },
      {
        "type": "input_value",
        "name": "DELTA",
        "check": "Number"
      }
    ],
    "previousStatement": null,
    "nextStatement": null,
    "colour": 330,
    "helpUrl": "https://en.wikipedia.org/wiki/Programming_idiom#Incrementing_a_counter"
  }

math_changeの生成コード


var [セレクトボックスで選んだ変数];
[セレクトボックスで選んだ変数] = (typeof [セレクトボックスで選んだ変数] == 'number' ? [セレクトボックスで選んだ変数] : 0) + [1つ目の入力端子];

math_roundについて

math_roundは四捨五入・切り上げ・切り捨てを行うブロックです。
このブロックは1つの入力端子と1つのセレクトボックスがあります。

math_roundの定義

  {
    "type": "math_round",
    "message0": "%1 %2",
    "args0": [
      {
        "type": "field_dropdown",
        "name": "OP",
        "options": [
          ["round", "ROUND"],
          ["round up", "ROUNDUP"],
          ["round down", "ROUNDDOWN"]
        ]
      },
      {
        "type": "input_value",
        "name": "NUM",
        "check": "Number"
      }
    ],
    "output": "Number",
    "colour": 230,
    "tooltip": "Round a number up or down.",
    "helpUrl": "https://en.wikipedia.org/wiki/Rounding"
  }

math_roundの生成コード


// roundを選択し四捨五入を行う。
[出力端子] = Math.round([1つ目の入力端子]);

// round upを選択し切り上げを行う。
[出力端子] = Math.ceil([1つ目の入力端子]);

// round downを選択し切り捨てを行う。
[出力端子] = Math.floor([1つ目の入力端子]);

math_moduloについて

math_moduloは剰余算を表すブロックです。
このブロックは2つの入力端子があります。
1つ目の入力端子の数値を2つ目の入力端子の値で割ったあまりが出力されます。

math_moduloの定義

  {
    "type": "math_modulo",
    "message0": "remainder of %1 ÷ %2",
    "args0": [
      {
        "type": "input_value",
        "name": "DIVIDEND",
        "check": "Number"
      },
      {
        "type": "input_value",
        "name": "DIVISOR",
        "check": "Number"
      }
    ],
    "inputsInline": true,
    "output": "Number",
    "colour": 230,
    "tooltip": "Return the remainder from dividing the two numbers.",
    "helpUrl": "https://en.wikipedia.org/wiki/Modulo_operation"
  }

math_moduloの生成コード


[出力端子] = [1つ目の入力端子] % [2つ目の入力端子];

math_constrainについて

math_constrainは数値の大小を比較するブロックです。
このブロックは3つの入力端子があります。
まず、1つ目の入力端子の数値を2つ目の入力端子の値に対してMath.maxを使用します、その後、Math.maxの結果と3つ目の入力端子に対してMath.minを行います。

math_constrainの定義

  {
    "type": "math_constrain",
    "message0": "constrain %1 low %2 high %3",
    "args0": [
      {
        "type": "input_value",
        "name": "VALUE",
        "check": "Number"
      },
      {
        "type": "input_value",
        "name": "LOW",
        "check": "Number"
      },
      {
        "type": "input_value",
        "name": "HIGH",
        "check": "Number"
      }
    ],
    "inputsInline": true,
    "output": "Number",
    "colour": 230,
    "tooltip": "Constrain a number to be between the specified limits (inclusive).",
    "helpUrl": "https://en.wikipedia.org/wiki/Clamping_%28graphics%29"
  }

math_constrainの生成コード


[出力端子] = Math.min( Math.max( [1つ目の入力端子 or 0] , [2つ目の入力端子 or 0] ) , [3つ目の入力端子 or Infinity] );

math_random_intについて

math_random_intは整数のランダム値を取得するブロックです。
このブロックは2つの入力端子があります。
1つ目の入力端子の数値と2つ目の入力端子の値の範囲でランダムな整数が出力されます。

math_random_intの定義

  {
    "type": "math_random_int",
    "message0": "random integer from %1 to %2",
    "args0": [
      {
        "type": "input_value",
        "name": "FROM",
        "check": "Number"
      },
      {
        "type": "input_value",
        "name": "TO",
        "check": "Number"
      }
    ],
    "inputsInline": true,
    "output": "Number",
    "colour": 230,
    "tooltip": "Return a random integer between the two specified limits, inclusive.",
    "helpUrl": "https://en.wikipedia.org/wiki/Random_number_generation"
  }

math_random_intの生成コード

function mathRandomInt(a, b) {
    if (a > b) {
        // Swap a and b to ensure a is smaller.
        var c = a;
        a = b;
        b = c;
    }
    return Math.floor(Math.random() * (b - a + 1) + a);
}

[出力端子] = mathRandomInt( [1つ目の入力端子] , [2つ目の入力端子] );

math_random_floatについて

math_random_floatはランダム値を取得するブロックです。
0以上1未満のランダムな値が出力されます。

math_random_floatの定義

  {
    "type": "math_random_float",
    "message0": "random fraction",
    "output": "Number",
    "colour": 230,
    "tooltip": "Return a random fraction between 0.0 (inclusive) and 1.0 (exclusive).",
    "helpUrl": "https://en.wikipedia.org/wiki/Random_number_generation"
  }

math_random_floatの生成コード


[出力端子] = Math.random();

math_number_hundredthsについて

コード生成に不具合があるらしく、コードが生成されないため詳細がわからない。

math_number_hundredthsの定義

  {
    "type": "math_number_hundredths",
    "message0": "%1",
    "args0": [
      {
        "type": "field_number",
        "name": "NUM",
        "precision": 0.01,
        "text": "3.14"
      }
    ],
    "colour": 230,
    "output": "Number",
    "tooltip": "A number.",
    "helpUrl": "https://en.wikipedia.org/wiki/Number"
  }

math_number_hundredthsの生成コード