davidedc / livecodelab

a web based livecoding environment
http://livecodelab.net/
MIT License
327 stars 63 forks source link

operator overloading (for vectors to start with, probably for colors too) #248

Open davidedc opened 9 years ago

davidedc commented 9 years ago

Translations should be able to take a vector as an argument. rotations too should take a vector (and an angle).

The current notation is the Processing one, which knows nothing about vectors, as we do for example

move 1,2,3

which in a way seems a little limited. We should rather be able to do use arrays for vectors like so

point = [1,2,3]
move (point + [4,5,6]) * pulse

So we should be able to overload '+' and '*' (and others) to make sense of vectors that way.

This overloading thing is a classic topic in Javascript (as in: everybody wants it but it's not really a language feature), BUT it can be done and the best way I've seen it done is in paper.js, where it's handled like so: http://scratchdisk.com/posts/operator-overloading

The coffeescript library that we already use does allow us to get the AST, traverse it to do the required changes, then emit the compiled code. With a bit of luck the parsing/compiling stages of coffeescript should amount to the same work that it does now (only we do it in one pass now), and the visiting of a few lines of code should amount to a visit of a few nodes and I'd hope it to be really be quick.

(Also consider that the current regex transformation stage can be wildly streamlined, I didn't even seriously try).

Once this works, this would open up operations on Colors for example, and potentially some of the current regex transformations could be migrated to be AST operations.

I'd expect this to be OK in @rumblesan 's branch as he works on solid ASTs to start with, so whatever we do on the main branch should be even more "legit" in his branch.

xinaesthete commented 9 years ago

I was thinking about related stuff today actually; I'm toying with the idea of receiving skeletal data from kinect via OSC for use in LCL, and it would be annoying to not be able to at least use coordinates as vectors, even without operator overloading. But operator overloading would definitely be good and your reasoning about how to implement seems fairly sound.

rumblesan commented 9 years ago

so my initial reaction to overloading was somewhat hesitant, but actually I think that it would be very possible to have it work in a sane fashion.

reading the paper.js stuff, it seems like the system they've got is reminiscent of haskells type classes. it's a bit more awkward in a language without strong typing, but I think it's the sanest way to do this kind of stuff I've seen.

In short, yeah I'm all for this. I think there's a fair bit of stuff we need to do around defining the language and how things should work, but I think it's all good stuff.

davidedc commented 9 years ago

yes indeed there is a potential rabbit hole here. If we really did this properly, we wouldn't just support [1,2,3] as a vector, that's somewhat reductive, we would really have to support generic matrixes (I'm assuming using arrays of arrays). Cause the vector could be "horizontal" as in the example but it could be vertical, so we should support stuff like transposition, multiplication between matrixes (which includes the case of vectors), multiplication between matrixes and scalars, component-wise multiplication and throw errors when the dimensions of the vector/matrixes don't add up...

xinaesthete commented 9 years ago

Just to map out other parts of the potential rabbit hole, quaternions could also be useful.

davidedc commented 9 years ago

Matlab could be a good example of some notations we may be shooting for: http://uk.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html

As for quaternions, in theory if we support matrixes and complex numbers then we can support calculations on quaternions just fine, but declaring quaternions in matrix form doesn't look right at all. So for other notations:

Are there other sw environments that have other notable notations for quaternions?