Problem
I am trying to convert the following piece of code: https://github.com/mdn/webgl-examples/blob/gh-pages/tutorial/glUtils.js#L13-L15
The JavaScript code is:
Matrix.Translation = function (v)
{
// ignore length 2 case for simplicty
if (v.elements.length == 3) {
var r = Matrix.I(4);
r.elements[0][3] = v.elements[0];
r.elements[1][3] = v.elements[1];
r.elements[2][3] = v.elements[2];
return r;
}
throw "Invalid length for Translation";
}
Now, I can rewrite it cljs as follows:
(defn translation [x y z]
(let [r (. js/Matrix I 4)]
r[0][3] = x ;; how do I write this?
r[1][3] = y ;; how do I write this?
r[2][3] = z ;; how do I write this?
))
Question
However, how do I write r[0][3] in cljs?
1 Answer 1
You can use aget and aset to work with Javascript arrays:
(def arr (array (array "a1" "a2") (array "b1" "b2") (array "c1" "c2")))
It creates following nested array:
#js [#js ["a1" "a2"] #js ["b1" "b2"] #js ["c1" "c2"]]
You can access nested elements with aget:
(aget arr 1 0)
;; => "b1"
And update with aset:
(aset arr 1 0 "newb1")
updates arr to:
#js [#js ["a1" "a2"] #js ["newb1" "b2"] #js ["c1" "c2"]]
You might want to take a look at other functions related to Javascript arrays: alength, array, make-array, array?.
answered Oct 29, 2016 at 8:27
Piotrek Bzdyl
13.2k1 gold badge36 silver badges51 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-clj