Skip to content

Commit

Permalink
true-fantom/math: Add clamp and scale blocks (#981)
Browse files Browse the repository at this point in the history
Resolves #888
  • Loading branch information
DNin01 authored Nov 6, 2023
1 parent 057d5aa commit 9adb4ac
Showing 1 changed file with 85 additions and 21 deletions.
106 changes: 85 additions & 21 deletions extensions/true-fantom/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@
arguments: {
A: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "\n",
defaultValue: "",
},
B: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "\n",
defaultValue: "",
},
},
},
Expand All @@ -130,11 +130,11 @@
arguments: {
A: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "\n",
defaultValue: "",
},
B: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "\n",
defaultValue: "",
},
},
},
Expand All @@ -145,7 +145,7 @@
arguments: {
A: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "\n",
defaultValue: "",
},
},
},
Expand All @@ -157,7 +157,7 @@
arguments: {
A: {
type: Scratch.ArgumentType.STRING,
defaultValue: "\n",
defaultValue: "",
},
B: {
type: Scratch.ArgumentType.STRING,
Expand All @@ -172,7 +172,7 @@
arguments: {
A: {
type: Scratch.ArgumentType.STRING,
defaultValue: "\n",
defaultValue: "",
},
B: {
type: Scratch.ArgumentType.STRING,
Expand All @@ -187,7 +187,7 @@
arguments: {
A: {
type: Scratch.ArgumentType.STRING,
defaultValue: "\n",
defaultValue: "",
},
B: {
type: Scratch.ArgumentType.STRING,
Expand All @@ -202,7 +202,7 @@
arguments: {
A: {
type: Scratch.ArgumentType.STRING,
defaultValue: "\n",
defaultValue: "",
},
B: {
type: Scratch.ArgumentType.STRING,
Expand All @@ -217,7 +217,7 @@
arguments: {
A: {
type: Scratch.ArgumentType.STRING,
defaultValue: "\n",
defaultValue: "",
},
B: {
type: Scratch.ArgumentType.STRING,
Expand All @@ -232,7 +232,7 @@
arguments: {
A: {
type: Scratch.ArgumentType.STRING,
defaultValue: "\n",
defaultValue: "",
},
B: {
type: Scratch.ArgumentType.STRING,
Expand All @@ -247,7 +247,7 @@
arguments: {
A: {
type: Scratch.ArgumentType.STRING,
defaultValue: "\n",
defaultValue: "",
},
B: {
type: Scratch.ArgumentType.STRING,
Expand Down Expand Up @@ -325,14 +325,61 @@
},
},
"---",
{
opcode: "clamp_block",
blockType: Scratch.BlockType.REPORTER,
text: "clamp [A] between [B] and [C]",
arguments: {
A: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "",
},
B: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "0",
},
C: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "100",
},
},
},
{
opcode: "scale_block",
blockType: Scratch.BlockType.REPORTER,
text: "map [A] from range [m1] - [M1] to range [m2] - [M2]",
arguments: {
A: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "",
},
m1: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "0",
},
M1: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "100",
},
m2: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "0",
},
M2: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "1",
},
},
},
"---",
{
opcode: "trunc2_block",
blockType: Scratch.BlockType.REPORTER,
text: "trunc of [A] with [B] digits after dot",
arguments: {
A: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "\n",
defaultValue: "",
},
B: {
type: Scratch.ArgumentType.NUMBER,
Expand All @@ -347,7 +394,7 @@
arguments: {
A: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "\n",
defaultValue: "",
},
},
},
Expand All @@ -359,11 +406,11 @@
arguments: {
A: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "\n",
defaultValue: "",
},
B: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "\n",
defaultValue: "",
},
},
},
Expand All @@ -375,7 +422,7 @@
arguments: {
A: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "\n",
defaultValue: "",
},
B: {
type: Scratch.ArgumentType.NUMBER,
Expand Down Expand Up @@ -407,7 +454,7 @@
arguments: {
A: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "\n",
defaultValue: "",
},
},
},
Expand All @@ -419,7 +466,7 @@
arguments: {
A: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "\n",
defaultValue: "",
},
},
},
Expand All @@ -430,7 +477,7 @@
arguments: {
A: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "\n",
defaultValue: "",
},
},
},
Expand All @@ -441,7 +488,7 @@
arguments: {
A: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "\n",
defaultValue: "",
},
},
},
Expand Down Expand Up @@ -496,6 +543,23 @@
exactly_cont_block({ A, B }) {
return cast.toString(A).includes(cast.toString(B));
}
clamp_block({ A, B, C }) {
if (cast.compare(A, B) < 0) {
return B;
} else if (cast.compare(A, C) > 0) {
return C;
} else {
return A;
}
}
scale_block({ A, m1, M1, m2, M2 }) {
return (
((cast.toNumber(A) - cast.toNumber(m1)) *
(cast.toNumber(M2) - cast.toNumber(m2))) /
(cast.toNumber(M1) - cast.toNumber(m1)) +
cast.toNumber(m2)
);
}
trunc2_block({ A, B }) {
let n = Math.floor(cast.toNumber(B));
if (n >= 1) {
Expand Down

0 comments on commit 9adb4ac

Please sign in to comment.