# Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 1.18 KB  |  hits: 12  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. Array of struct initialization
2. typedef struct { double x, y; } vec;
3. typedef struct { int n; vec* v; } polygon_t, *polygon;
4.
5. #define BIN_V(op, xx, yy) vec v##op(vec a, vec b) {
6.     vec c; c.x = xx; c.y = yy; return c; }
7.
8. #define BIN_S(op, r) double v##op(vec a, vec b) { return r; }
9.
10. BIN_V(sub, a.x - b.x, a.y - b.y);
11. BIN_V(add, a.x + b.x, a.y + b.y);
12. BIN_S(dot, a.x * b.x + a.y * b.y);
13. BIN_S(cross, a.x * b.y - a.y * b.x);
14.
15. vec testPoints[] = {
16.                     {1, 1},
17.                     {3, 3},
18.                     {3, 5},
19.                     {5, 2},
20.                     {6, 3},
21.                     {7, 4}
22.                    };
23.
24. vec v = { 2, 3 };
25.
26. vec v;
27. v.x = 2;
28. v.y = 4;
29.
30. int myints[3] = { 1, 2, 3 };
31.
32. myints[0] = 1;
33. myints[1] = 2;
34. myints[2] = 3;
35.
36. vec mystructs[2] = { { 1, 2}, { 2, 3} };
37.
38. mystructs[0].x = 1;
39. mystructs[0].y = 2;
40. mystructs[1].x = 2;
41. mystructs[1].y = 3;
42.
43. struct vec
44. {
45.     vec(double a_x, double a_y) : x(a_x), y(a_y) {}
46.     double x,y;
47. };
48.
49. std::vector<vec> allPoints;
50.
51. allPoints.push_back(vec(1,2));
52.
53. std::vector<vec> v;
54. v.push_back(vec{ 1, 2 });
55.
56. vec make_vec(double a, double b) {
57.     vec v = { a, b };
58.     return v;
59. }
60. ...
61. v.push_back(make_vec(1, 2));