3 * Version 1.0 - Andrew Glassner
4 * from "Graphics Gems", Academic Press, 1990
10 /*********************/
11 /* 2d geometry types */
12 /*********************/
14 typedef struct Point2Struct { /* 2d point */
17 typedef Point2 Vector2;
19 typedef struct IntPoint2Struct { /* 2d integer point */
23 typedef struct Matrix3Struct { /* 3-by-3 matrix */
27 typedef struct Box2dStruct { /* 2d box */
32 /*********************/
33 /* 3d geometry types */
34 /*********************/
36 typedef struct Point3Struct { /* 3d point */
39 typedef Point3 Vector3;
41 typedef struct IntPoint3Struct { /* 3d integer point */
46 typedef struct Matrix4Struct { /* 4-by-4 matrix */
50 typedef struct Box3dStruct { /* 3d box */
56 /***********************/
57 /* one-argument macros */
58 /***********************/
60 /* absolute value of a */
61 #define ABS(a) (((a)<0) ? -(a) : (a))
63 /* round a to nearest int */
64 #define ROUND(a) ((a)>0 ? (int)((a)+0.5) : -(int)(0.5-(a)))
66 /* take sign of a, either -1, 0, or 1 */
67 #define ZSGN(a) (((a)<0) ? -1 : (a)>0 ? 1 : 0)
69 /* take binary sign of a, either -1, or 1 if >= 0 */
70 #define SGN(a) (((a)<0) ? -1 : 1)
72 /* shout if something that should be true isn't */
74 if (!(x)) fprintf(stderr," Assert failed: x\n");
77 #define SQR(a) ((a)*(a))
80 /***********************/
81 /* two-argument macros */
82 /***********************/
84 /* find minimum of a and b */
85 #define MIN(a,b) (((a)<(b))?(a):(b))
87 /* find maximum of a and b */
88 #define MAX(a,b) (((a)>(b))?(a):(b))
90 /* swap a and b (see Gem by Wyvill) */
91 #define SWAP(a,b) { a^=b; b^=a; a^=b; }
93 /* linear interpolation from l (when a=0) to h (when a=1)*/
94 /* (equal to (a*h)+((1-a)*l) */
95 #define LERP(a,l,h) ((l)+(((h)-(l))*(a)))
97 /* clamp the input to the specified range */
98 #define CLAMP(v,l,h) ((v)<(l) ? (l) : (v) > (h) ? (h) : v)
101 /****************************/
102 /* memory allocation macros */
103 /****************************/
105 /* create a new instance of a structure (see Gem by Hultquist) */
106 #define NEWSTRUCT(x) (struct x *)(malloc((unsigned)sizeof(struct x)))
108 /* create a new instance of a type */
109 #define NEWTYPE(x) (x *)(malloc((unsigned)sizeof(x)))
112 /********************/
113 /* useful constants */
114 /********************/
117 #define PI 3.141592 /* the venerable pi */
119 #define PITIMES2 6.283185 /* 2 * pi */
120 #define PIOVER2 1.570796 /* pi / 2 */
121 #define E 2.718282 /* the venerable e */
122 #define SQRT2 1.414214 /* sqrt(2) */
123 #define SQRT3 1.732051 /* sqrt(3) */
124 #define GOLDEN 1.618034 /* the golden ratio */
125 #define DTOR 0.017453 /* convert degrees to radians */
126 #define RTOD 57.29578 /* convert radians to degrees */
143 typedef int boolean; /* boolean data type */
144 typedef boolean flag; /* flag data type */
148 /*extern double V2SquaredLength(Vector2 *a), V2Length();
149 extern double V2Dot(Vector2 *a, Vector2 *b), V2DistanceBetween2Points();
150 extern Vector2 *V2Negate(), *V2Normalize(), *V2Scale(), *V2Add(), *V2Sub(Vector2 *a, Vector2 *b, Vector2 *c);
151 extern Vector2 *V2Lerp(), *V2Combine(), *V2Mul(), *V2MakePerpendicular();
152 extern Vector2 *V2New(), *V2Duplicate();
153 extern Point2 *V2MulPointByMatrix();
154 extern Matrix3 *V2MatMul();
156 extern double V3SquaredLength(), V3Length();
157 extern double V3Dot(), V3DistanceBetween2Points();
158 extern Vector3 *V3Normalize(), *V3Scale(), *V3Add(), *V3Sub();
159 extern Vector3 *V3Lerp(), *V3Combine(), *V3Mul(), *V3Cross();
160 extern Vector3 *V3New(), *V3Duplicate();
161 extern Point3 *V3MulPointByMatrix();
162 extern Matrix4 *V3MatMul();
164 extern double RegulaFalsi(), NewtonRaphson(), findroot();*/