Complexify!: The world of complex numbers. - #2113
Conversation
A better extension for complex numbers, fixing all the issues with the original Complexity!
|
What do you think, @Brackets-Coder and @yuri-kiss? |
Brackets-Coder
left a comment
There was a problem hiding this comment.
Generally speaking you should try to follow formatting best practices so your code doesn't look cluttered. Additionally, try to avoid opening new pull requests when it isn't necessary and you could just update the original :)
| 'use strict'; | ||
| //Just in case: | ||
| if (!Scratch.extensions.unsandboxed) { | ||
| alert("We don't like sand"); |
There was a problem hiding this comment.
Nice allusion to Anakin Skywalker's famous opinion, but generally you should avoid alerts and just throw the error
There was a problem hiding this comment.
ok, i'll avoid alerts. also who's Anakin Skywalker?
There was a problem hiding this comment.
ok, i'll avoid alerts. also who's Anakin Skywalker?
I'm just going to assume you've never watched the masterpiece of Star Wars
| } | ||
| } | ||
|
|
||
| function jsCode() { /**Again, thanks Rawify*/ |
There was a problem hiding this comment.
First, you're putting the library (which is already inside an Immediately Invoked Function Expression) inside a function that is just executed elsewhere, and you're also re-declaring "use-strict" again which is totally redundant. Why can't you just minify the library and put it inside the class constructor?
There was a problem hiding this comment.
you're saying i can just insert the complex code in the constructor?
There was a problem hiding this comment.
you're saying i can just insert the complex code in the constructor?
That's how JS works, if you're just executing the function once inside the try catch block than might as well not have the function and reduce line count
| { | ||
| filter: [Scratch.TargetType.SPRITE], //Just in case | ||
| blockType: Scratch.BlockType.LABEL, | ||
| text: Scratch.translate("Motion"), | ||
| }, |
There was a problem hiding this comment.
so this section seems to be restricted to sprites, but let's check to make sure you're also filtering the block code so it doesn't return an error if these blocks are dragged from a sprite into the backdrop
There was a problem hiding this comment.
I don't know how to do that, but i'll try
There was a problem hiding this comment.
I don't know how to do that, but i'll try
You're correctly filtering the block pallet in the stage, but the sprite-exclusive blocks can be dragged into the stage and may cause errors. You should check to see if the block's target is the stage, it works like this:
blockOpcode({ Arg1, Arg2 }, { target }) {
console.log(target);
}
just console log target and you'll see how to detect the stage
There was a problem hiding this comment.
ok i'll if (util.target.isStage) return; to each movement block
| filter: [Scratch.TargetType.SPRITE], | ||
| opcode: 'goToPolar', //A block no one asked for, and I fear no one needs | ||
| blockType: Scratch.BlockType.COMMAND, | ||
| text: 'Go polar [RADII] ∠ [ANGLY]', | ||
| arguments: { | ||
| RADII: { type: Scratch.ArgumentType.STRING, defaultValue: 50 }, | ||
| ANGLY: { type: Scratch.ArgumentType.STRING, defaultValue: '0.9272952180016123' } | ||
| }, | ||
| }, | ||
| { | ||
| filter: [Scratch.TargetType.SPRITE], | ||
| opcode: 'glideComplex', //My favourite block [I love it] | ||
| blockType: Scratch.BlockType.COMMAND, | ||
| text: 'Glide [SECS] secs to [COMPLEX]', | ||
| arguments: { | ||
| COMPLEX: { type: Scratch.ArgumentType.STRING, defaultValue: '30+40i' }, | ||
| SECS: { type: Scratch.ArgumentType.NUMBER, defaultValue: 1 } | ||
| }, | ||
| }, |
There was a problem hiding this comment.
Are the offhand comments here really necessary? I think they just distract from the code
There was a problem hiding this comment.
no, just some nice details. i'll delete them
| /** | ||
| We'll use toString() to return the Complex number with math notation. | ||
| If you wonder why, remember Complex is a class, and hence, returns objects. | ||
| So, we don't want "{re: -5, im: 1}", "[-5,1]" or "[object Object]". We want "-5+i" as is | ||
| Thus, no Scratch.Cast.toString() or anything like that, because toString() will always do. | ||
| */ |
There was a problem hiding this comment.
May I ask why this is the case? If everything is returned as objects then shouldn't you just parse them and return their properties instead of the whole object? The reason Scratch.Cast.toString() exists is because scratch has weird quirks that it has to account for which the normal javascript toString doesn't
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
There was a problem hiding this comment.
thanks for the tip, @yuri-kiss! also @Brackets-Coder, the true reason we used .toString() is because no other function will output the strings we want
There was a problem hiding this comment.
update: this.strBuild is the new string maker, it's equivalent to the old Complex.prototype.toString in (almost) every way.
| glideComplex (args, util) { //Do you recognize this? Answer at the end! | ||
| if (util.stackFrame.timer) { | ||
| const timeElapsed = util.stackFrame.timer.timeElapsed(); | ||
| if (timeElapsed < util.stackFrame.duration * 1000) { | ||
| // We've moving! And we'll move again. | ||
| const frac = timeElapsed / (util.stackFrame.duration * 1000); | ||
| const dx = frac * (util.stackFrame.endX - util.stackFrame.startX); | ||
| const dy = frac * (util.stackFrame.endY - util.stackFrame.startY); | ||
| util.target.setXY(util.stackFrame.startX + dx, util.stackFrame.startY + dy); | ||
| util.yield(); | ||
| } else { | ||
| // We're done! Now, lets end this | ||
| util.target.setXY(util.stackFrame.endX, util.stackFrame.endY); | ||
| } | ||
| } else { | ||
| // We're starting! So, new Timer! | ||
| util.stackFrame.timer = new Timer(); | ||
| util.stackFrame.timer.start(); | ||
| util.stackFrame.duration = args.SECS; | ||
| util.stackFrame.startX = util.target.x; | ||
| util.stackFrame.startY = util.target.y; | ||
| util.stackFrame.endX = Complex(args.COMPLEX).re; //A little edit | ||
| util.stackFrame.endY = Complex(args.COMPLEX).im; | ||
| if (util.stackFrame.duration <= 0) { | ||
| // We can't glide -1 seconds, can we? | ||
| util.target.setXY(util.stackFrame.endX, util.stackFrame.endY); | ||
| return; | ||
| } | ||
| util.yield(); | ||
| } | ||
| } |
There was a problem hiding this comment.
This seems really unoptimized
There was a problem hiding this comment.
yea i just copied the code from Scratch-vm because we didn't know how to glide it ourselves
There was a problem hiding this comment.
even asked ChatGPT, we couldn't find any optimization
| convertComplex({ ANGLE, TOSMTH }) { | ||
| try { | ||
| if (ANGLE == "") { | ||
| return 0; | ||
| } | ||
| const cInstance = Complex(ANGLE); | ||
| switch (TOSMTH) { | ||
| case 'degs to rads': if (cInstance.im == 0) return (cInstance.re * 0.017453292519943295) % twoPi; | ||
| return cInstance.mul(0.017453292519943295).toString(); break; | ||
| case '𝜋': return cInstance.mul(3.141592653589793).toString(); break; | ||
| case 'rads to degs': if (cInstance.im == 0) return (cInstance.re * 57.29577951308232) % 360; | ||
| return cInstance.mul(57.29577951308232).toString(); break; | ||
| default: return NaN | ||
| } | ||
| } catch (e) { | ||
| console.log(e); | ||
| return 0; | ||
| } | ||
| } |
There was a problem hiding this comment.
Oh my gosh the formatting is crazy I'll see if I can fix it
|
!format |
This comment was marked as abuse.
This comment was marked as abuse.
I'll add a better glideComplex, more Scratch.Cast and the minified code later. For now, small changes first
Still working on some thing though. I'll tell you when it's ready.
@Brackets-Coder, I'm new to JS and GitHub and that sort of stuff. I don't know a lot of things. Some things, but not all of them. I didn't knew that you could edit pull-requesting files until recently, after I created this new pull request.
And which are the horrible practices? Now that I know how to edit files, maybe I can correct Complexify!.js |
|
!format |
Absolutely not trying to be critical, we all were there once and it was only recently (in the past few months) that I really started with Github. It's an understandable situation, I'm just here to try to help you through it. |
|
!format (heard these fix smth idk) |
|
The formatting bot didn't find any formatting issues. It currently only checks the extensions folder. The author or a maintainer can run terminal command 'npm run format' manually to format all files. |
Yep. MORE THINGIES. Thanks to every error you've found, it's better than ever. Can't wait to see it at the gallery!
|
!format (for the update. two done, one to go) |
Basic changes :)
Checked again. what is define? idk
|
The formatting bot didn't find any formatting issues. It currently only checks the extensions folder. The author or a maintainer can run terminal command 'npm run format' manually to format all files. |
Testing some small changes
Let's C if it works
|
!format |
|
The formatting bot didn't find any formatting issues. It currently only checks the extensions folder. The author or a maintainer can run terminal command 'npm run format' manually to format all files. |
|
@Brackets-Coder never told me about my horrible practices :( |
|
!format |
Contrary to popular opinion, I'm not very good at math. I usually just stick with the code. |
|
@Brackets-Coder yeah but there must be any code-related horrible practices, right? (It passed a year but some could still be stuck with me lol) |
|
@penta-quark-neutro I'll probably be updating the version soon, stay alert |
|
"export [x] as [pentaquark]"? Wouldn't "compleX" be more accurate? I have two extensions that can handle complex numbers, and they don't accept the same inputs. |
|
@penta-quark-neutro Thanks, and I didn't knew you had two complex number extensions. Before I seal the blocks, you can propose changes to their current names. Which block names seem counter-intuitive or wronged? (i'm so used to their current ones it's hard for me to spot places for changes in favor of understandment or consistency) |
|
I think all the names are fine, but don't take my word for it—since I'm a mathematician, that's the sort of thing I understand. |
|
@penta-quark-neutro estoy buscando tu otra extensión de números complejos, para añadir otra forma de exportarlos. De paso (y ya que ere matemático), dáme ideas de funciones para poner en mi extensión :3 |
|
la otra extension es "vector directo", sus entradas son vectores [re,im], directamente el objeto, no un string. |
|
@penta-quark-neutro Me alegra que lo apruebes. Estuve revisando tus otras extensiones, y al ojo se nota que eres matemático (no entiendo nada de lo que escribes pero funciona xD); sé que este no es el mejor lugar para decir esto, pero tu extensión de lógica trivalente sería ligeramente más legible y rápida si utilizas |
|
si entiendo, pero logica trivalente no fue hecho con la misma intención que otros, fue un trabajo mas de aburrimiento. |
|
@penta-quark-neutro Con la nueva actualización, ¿tú que harías?
En estos no cambié nada, pero déjame presumirlos:
!format |
|
¿como que "que haría"? |
|
@penta-quark-neutro no sé, pues ni yo conozco la razón detrás de crear Complexity. Bueno, te veré en tu repo por un rato, pues encuentro varios lugares para optimizar ;3 |
|
@penta-quark-neutro Pues claro, ese bloque de Export lo hice compatible con la naturaleza de Scratch (quien sólo sabe Strings, Numbers y Booleans), por esa misma razón los complejos de mi extensión se extraen de strings (hecho por mi función
|
|
now if you excuse me, I'll work to add the Riemann's Zeta soon |
|
I don't know if you'll believe me, but I actually imagined you would do it at some point. |
|
I already have a weak approximation; I can now prove results. |
Nah, if so, you'd've told me when I asked for more functions. Además, dices tener una aproximación, ¿no, @penta-quark-neutro ? Sería útil compararlas con la mía. |
|
no sugerí eso porque es una función "rara", con aplicaciones especificas. |
|
@penta-quark-neutro I'm going to have a breakdown |
















A better extension for complex numbers, fixing all the issues with the original Complexity! See the first one at #2091
More motion, vectors, decimals and trig functions!