Two names for the same variable?

Started by
5 comments, last by K- 15 years, 9 months ago
Hi, I have a Vector3 class with variables x, y, z. Is there any way to access these variables through other names? e.g. consider a Vector2 class with variables x, y. If I wanted to create a texture object that uses the same struct but calls them u, v.
Advertisement
Yes, with anonymous unions and structs (the latter are technically not standard, but everyone supports them). It is common to see something like this:
struct Vector3{  union  {    struct    {      float x,y,z;    };    struct    {      float u,v,w;    };    struct    {      float r,g,b;    };  };};

Personally, I don't like this. u, v, and w are variable names used by convention for "the vector in the other space", and are not mandatory for clear math code. If you're naming your vectors properly, using xyz should be unambiguous. And for colors, if you want to enshrine them as a separate type (which does make a fair amount of sense if you're doing special color space stuff) just give them their own type.
Sure:

EDIT: Added v property...
EDIT: Forget about my class. I forgot about union...
EDIT: And why did I assume he's using C#? Man, I'm waaay off today!

Now that I've shown you that, is there any reason why you can't just use a Vector3 instead of creating whole new class just so you can call x and y by u and v?
the reason I was interested in different names was that sometimes you might want to refer to elements XYZ of a vector, but in other notations they are talked about as i j k (like quaternions for example).
Quote:Original post by Winegums
the reason I was interested in different names was that sometimes you might want to refer to elements XYZ of a vector, but in other notations they are talked about as i j k (like quaternions for example).
AFAIK, it's not typical to refer to elements of a quaternion as i, j, and k. Rather, i, j, and k refer to the imaginary numbers for which x, y, and z are the coefficients. (In other words, the element names are x, y, z, and w, just as in a 'normal' 4-d vector).

You also sometimes see 'a', 'b', 'c', and 'd', but this seems to be less common (at least in the programming world).
I tend to just use Vector2/Vector3/Vector4 for most things. Like texture coordinates, you probably want to be able to run them through matrices and such, so it makes sense for them to be vectors.

Quaternions probably should have their own type, since they have different operations than vectors, and you don't need to be able to run them through matrices or anything.

Colors could go either way. I'd probably just use Vector3, since they get treated like vectors for lighting calculations and such anyway.

I do use the anonymous struct union though, so I can access vectors as x/y/z or as an array of floats. Less ugly than just casting to a float pointer.
Quote:Original post by Winegums
Hi, I have a Vector3 class with variables x, y, z.

Is there any way to access these variables through other names?


Probably yes, are you using C++?

This topic is closed to new replies.

Advertisement