0

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

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?.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.