0

I found a code from internet and try to understand it. I don't know what is the programming language of the code and try to find javascript equivalencies or workarounds.

A) Which language is in question?


*a_offsetrot = PI / 4.0f;

B) What is the meaning of '*' in above and what is the possible Javascript equivalence?

C) What is the meaning of 'f' and what is the possible Javascript equivalence?


if (K<0) K = 0; else K = (float)sqrt (K);

D) What is the meaning of '(float)' and what is the possible Javascript equivalence?


*a_offsetrot = 0.5f * (float)atan2(B,ac);

E) What is the meaning of "atan2" and what is the possible Javascript equivalence?


void Ellipse_Transform (float * a_rh, float * a_rv, float *a_offsetrot, Vector2 * endpoint, matrix * a_mat, int *a_ReverseWinding)

F) What is the meaning of 'Vector2 * endpoint' and is there possible Javascript equivalence? Is Vector2 a thing that has two (x,y)-points or only one (x,y)-point?


m[0] = a_mat->m[0] * +rh * c + a_mat->m[3] * rh * s;

G) What is the meaning of 'a_mat->m[0]' and what is the possible Javascript equivalence?

EDIT: Updated title to be more meaningful.

EDIT2: Thanks to constructive answer of @rid I got the C language code translated to Javascript and it is HERE and full working functional example is HERE.

Community
  • 1
  • 1
Timo Kähkönen
  • 11,962
  • 9
  • 71
  • 112

1 Answers1

4

A) The language is C.

B) * is the dereferencing operator. There is no equivalent in JavaScript, since JavaScript does not use pointers.

C) The f in 4.0f indicates that the value is a float value. There is no equivalent in JavaScript, since in JavaScript all numeric values are represented as numbers.

D) (float) is a type cast. Something similar in JavaScript is parseFloat() which will parse the argument and return a floating point number, but this is different from type casting, and there is no equivalent to type casting in JavaScript.

E) atan2() is a math function for calculating the arctangent. You can find it in the standard JavaScript Math package.

F) A Vector2 is most likely a structure that has two properties, an x property and a y property.

G) a_mat->m[0] means the first element from the array m that is a property of a_mat. If a_mat was an object in JavaScript, the equivalent would be a_mat.m[0].

rid
  • 61,078
  • 31
  • 152
  • 193
  • Is Vector2 "native" C type or does it come from some additional library or package? – Timo Kähkönen Oct 30 '12 at 09:59
  • @Timo, it's not native, it's most likely defined as a `struct` by the person who wrote that code, and used as a convenient way to store 2 values together. You can think of it as an object in JavaScript: `{x: ..., y: ...}`. – rid Oct 30 '12 at 09:59
  • Why atan2? Why not atan? – Timo Kähkönen Oct 30 '12 at 10:10
  • @Timo, there's also an `atan()` function that takes a single argument. `atan2()` returns the arctangent of the quotient of its arguments. `atan()` returns the arctangent of a number. The JavaScript `Math` package [contains both of these functions](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math#Methods). – rid Oct 30 '12 at 10:12
  • Fine. Thanks. I understand now the code more perfectly! – Timo Kähkönen Oct 30 '12 at 10:15
  • Is `atan2` more suitable for handling bitmap graphics where y-axis increases downwards? – Timo Kähkönen Oct 30 '12 at 10:24
  • @Timo, you should ask that as a separate question. – rid Oct 30 '12 at 14:52
  • The * operator can be so multiply and dereferencing operator. When converting the above code to Javascript, can I just remove all dereferencing operators? – Timo Kähkönen Oct 30 '12 at 17:14
  • @Timo, sure, since JavaScript doesn't have pointers, there's nothing to dereference. `*` has many meanings (it can be a pointer type, the dereferencing operator or the multiplication sign), but in that particular case, it's the dereferencing operator. – rid Oct 30 '12 at 17:25