Friday, May 17, 2013

Pythagorean theorem in Q Editor

Last month I wrote about Q 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 closer together as I wanted to save some pixels)

On the very left there is a block schema, where you can see a flow of all commands, because Q 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 and a 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 back into the code after processing the Q script.

In the script 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.