How to create a vector2 in C?

3

I would like to know if you have how to create a class, to store 2 variables, for example, a Vector2, where I would instantiate and use like this:

Vector2 tile;
int posX, posY;
posX = tile.x;
posY = tile.Y;

or something like that.

    
asked by anonymous 29.03.2014 / 07:17

2 answers

5

In C, with struct (C does not have class ), do so

/* define struct Vector2 com dois elementos */
struct Vector2 {
    int x;
    int y;
};

/* define tile como uma variavel pertencente ao tipo struct Vector2 */
struct Vector2 tile;

/* atribui valores aos membros de tile */
tile.x = 42;
tile.y = -1;
    
29.03.2014 / 10:28
0

In C, Classes are not used, as is done in C ++. When we want to store data of different types, we use struct :

struct estrutura {
    int nBase;
    int nAltura;
    double fArea; };

and then we can create structures of type struct estrutura :

 estrutura triangulo1;
 estrutura retangulo1;

and access its independent elements from each other:

triangulo1.nBase = 10;
triangulo1.nAltura = 5;
triangulo1.fArea = triangulo1.nBase * triangulo1.nAltura / 2.0;
// triangulo1.fArea terá valor de 7.5

retangulo1.nBase = 7;
retangulo1.nAltura = 3;
retangulo1.fArea = nBase * nAltura;
// retangulo1.fArea terá valor de 21.0

I hope I have helped!

    
13.05.2014 / 04:43