Friday, May 17, 2013

Pythagorean theorem in Cue Editor

Last month I presented Cue language for scripting events in QetriX. I mentioned I created an online editor, but I didn't show it here. So here is a screenshot: (it's shrinked together as I wanted to save pixels here)

Pythagorean theorem in Cue Editor
On the very left there is a block schema, where you can see a flow of all commands, because Cue utilizes a lot of "goto" commands and it may be easy to get lost in the code.

In the gray box, titled "Events", you can see the source code on the left an and debug output and trace on the right. Output is like a console, but here is nothing to print out. In Trace you can see what commands in what order (and with what result) has been executed.

"RESULT" is the value, what is returned into code after processing the Cue code.

In the code I defined two constants, but you can get those input values from QetriX, or e.g. GET, POST or COOKIE variables. So first two lines contains numbers 3 and 4. Pythagorean theorem for calculation the hypotenuse (variable "c") is: "c = sqrt(a² + b²)", so first of all we have to calculate square power for "a" and "b" (lines 3 and 4, showing two different ways how to achieve that, using "mul" and "pow" commands). Then we add those two results together and finally we calculate a square root of the sum.

There are no gotos or decisions, so the code is nicely straight - from Start ("S" circle) to End ("E" circle). Final result of the code is the same, as the result of the last line.

No comments:

Post a Comment