
Untitled
By: a guest on
May 9th, 2012 | syntax:
None | size: 1.18 KB | hits: 12 | expires: Never
Array of struct initialization
typedef struct { double x, y; } vec;
typedef struct { int n; vec* v; } polygon_t, *polygon;
#define BIN_V(op, xx, yy) vec v##op(vec a, vec b) {
vec c; c.x = xx; c.y = yy; return c; }
#define BIN_S(op, r) double v##op(vec a, vec b) { return r; }
BIN_V(sub, a.x - b.x, a.y - b.y);
BIN_V(add, a.x + b.x, a.y + b.y);
BIN_S(dot, a.x * b.x + a.y * b.y);
BIN_S(cross, a.x * b.y - a.y * b.x);
vec testPoints[] = {
{1, 1},
{3, 3},
{3, 5},
{5, 2},
{6, 3},
{7, 4}
};
vec v = { 2, 3 };
vec v;
v.x = 2;
v.y = 4;
int myints[3] = { 1, 2, 3 };
myints[0] = 1;
myints[1] = 2;
myints[2] = 3;
vec mystructs[2] = { { 1, 2}, { 2, 3} };
mystructs[0].x = 1;
mystructs[0].y = 2;
mystructs[1].x = 2;
mystructs[1].y = 3;
struct vec
{
vec(double a_x, double a_y) : x(a_x), y(a_y) {}
double x,y;
};
std::vector<vec> allPoints;
allPoints.push_back(vec(1,2));
std::vector<vec> v;
v.push_back(vec{ 1, 2 });
vec make_vec(double a, double b) {
vec v = { a, b };
return v;
}
...
v.push_back(make_vec(1, 2));