Sign in to follow this  

Two names for the same variable?

This topic is 3460 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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++?

Share this post


Link to post
Share on other sites

This topic is 3460 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this