~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/math/geom/vec2.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**@(#)Vector handling (allocation, additon etc)
  2 **/
  3 
  4 #include <stdio.h>
  5 #include <math.h>
  6 #include <tina/sys.h>
  7 #include <tina/sysfuncs.h>
  8 #include <tina/math.h>
  9 #include <tina/mathfuncs.h>
 10 
 11 static Vec2 vec2_0 = {Vec2_id};
 12 
 13 
 14 /** for use with list utilities **/
 15 Vec2   *vec2_alloc(void)
 16 {
 17     Vec2   *v = ts_ralloc(Vec2);
 18 
 19     v->el[0] = 0.0;
 20     v->el[1] = 0.0;
 21     return (Vec2 *) ((void *) v);
 22 }
 23 
 24 /* Deep copy a vec2 (takes & returns POINTERS) */
 25 Vec2   *vec2_copy(Vec2 * vec2)
 26 {
 27     Vec2   *vec2_new = ts_ralloc(Vec2);
 28 
 29     *vec2_new = *vec2;
 30     return vec2_new;
 31 }
 32 
 33 Vec2   *vec2_make(Vec2 u)
 34 {
 35     Vec2   *v = ts_ralloc(Vec2);
 36 
 37     *v = u;
 38     return (Vec2 *) ((void *) v);
 39 }
 40 
 41 void    vec2_free(void *v)
 42 {
 43     rfree((void *) v);
 44 }
 45 
 46 Vec2    vec2(double x, double y)
 47 {
 48     Vec2    v = {Vec2_id};
 49 
 50     v.el[0] = x;
 51     v.el[1] = y;
 52     return (v);
 53 }
 54 
 55 Vec2    vec2_zero(void)
 56 {
 57     return (vec2_0);
 58 }
 59 
 60 Vec2    vec2_ex(void)
 61 {
 62     return (vec2(1.0, 0.0));
 63 }
 64 
 65 Vec2    vec2_ey(void)
 66 {
 67     return (vec2(0.0, 1.0));
 68 }
 69 
 70 void    vec2_comps(Vec2 v, float *x, float *y)
 71 {
 72     *x = v.el[0];
 73     *y = v.el[1];
 74 }
 75 
 76 double  vec2_get_x(Vec2 * v)
 77 {
 78     return (v->el[0]);
 79 }
 80 
 81 double  vec2_get_y(Vec2 * v)
 82 {
 83     return (v->el[1]);
 84 }
 85 
 86 Vec2    vec2_sum(Vec2 v, Vec2 w)
 87 {
 88     Vec2    sum = {Vec2_id};
 89 
 90     sum.el[0] = v.el[0] + w.el[0];
 91     sum.el[1] = v.el[1] + w.el[1];
 92     return (sum);
 93 }
 94 
 95 Vec2    vec2_sum3(Vec2 u, Vec2 v, Vec2 w)
 96 {
 97     Vec2    sum = {Vec2_id};
 98 
 99     sum.el[0] = u.el[0] + v.el[0] + w.el[0];
100     sum.el[1] = u.el[1] + v.el[1] + w.el[1];
101     return (sum);
102 }
103 
104 Vec2    vec2_sum4(Vec2 u, Vec2 v, Vec2 w, Vec2 x)
105 {
106     Vec2    sum = {Vec2_id};
107 
108     sum.el[0] = u.el[0] + v.el[0] + w.el[0] + x.el[0];
109     sum.el[1] = u.el[1] + v.el[1] + w.el[1] + x.el[1];
110     return (sum);
111 }
112 
113 Vec2    vec2_minus(Vec2 v)
114 {
115     Vec2    minus = {Vec2_id};
116 
117     minus.el[0] = -v.el[0];
118     minus.el[1] = -v.el[1];
119     return (minus);
120 }
121 
122 Vec2    vec2_diff(Vec2 v, Vec2 w)
123 {
124     Vec2    diff = {Vec2_id};
125 
126     diff.el[0] = v.el[0] - w.el[0];
127     diff.el[1] = v.el[1] - w.el[1];
128     return (diff);
129 }
130 
131 Vec2    vec2_times(double k, Vec2 v)
132 {
133     Vec2    prod = {Vec2_id};
134 
135     prod.el[0] = k * v.el[0];
136     prod.el[1] = k * v.el[1];
137     return (prod);
138 }
139 
140 Vec2    vec2_interp(double k, Vec2 v1, Vec2 v2)
141 {
142     double  k1 = 1.0 - k;
143     Vec2    interp = {Vec2_id};
144 
145     interp.el[0] = k1 * v1.el[0] + k * v2.el[0];
146     interp.el[1] = k1 * v1.el[1] + k * v2.el[1];
147     return (interp);
148 }
149 
150 double  vec2_dot(Vec2 v, Vec2 w)
151 {
152     return (v.el[0] * w.el[0] + v.el[1] * w.el[1]);
153 }
154 
155 double  vec2_cross(Vec2 v, Vec2 w)
156 {
157     return (v.el[0] * w.el[1] - v.el[1] * w.el[0]);
158 }
159 
160 double  vec2_mod(Vec2 v)
161 {
162     double  vx = v.el[0], vy = v.el[1];
163 
164     return (sqrt(vx * vx + vy * vy));
165 }
166 
167 double  vec2_sqrmod(Vec2 v)
168 {
169     double  vx = v.el[0], vy = v.el[1];
170 
171     return (vx * vx + vy * vy);
172 }
173 
174 double  vec2_modunit(Vec2 v, Vec2 * e)
175 {
176     double  vx = v.el[0], vy = v.el[1];
177     double  k, k1;
178 
179     k = sqrt(vx * vx + vy * vy);
180     if (k == 0.0)
181     {
182         *e = vec2_ex();
183         return (0.0);
184     }
185     k1 = 1.0 / k;
186     *e = vec2(vx * k1, vy * k1);
187     return (k);
188 }
189 
190 Vec2    vec2_unit(Vec2 v)
191 {
192     double  vx = v.el[0], vy = v.el[1];
193     double  k, k1;
194 
195     k = sqrt(vx * vx + vy * vy);
196     if (k == 0.0)
197         return (vec2_ex());
198     k1 = 1.0 / k;
199     return (vec2(vx * k1, vy * k1));
200 }
201 
202 void    vec2_to_polar(Vec2 v, double *r, double *theta)
203 {
204     double  x = vec2_x(v);
205     double  y = vec2_y(v);
206 
207     *r = sqrt(x * x + y * y);
208     *theta = atan2(y, x);
209 }
210 
211 Vec2    vec2_of_polar(double r, double theta)
212 {
213     return (vec2(r * cos(theta), r * sin(theta)));
214 }
215 
216 Vec2    vec2_rand_circle(Vec2 centre, double radius)
217 {
218     double  x, y, r;
219 
220     do
221     {
222         x = rand_unif(-1.0, 1.0);
223         y = rand_unif(-1.0, 1.0);
224     } while ((r = x * x + y * y) > 1.0);
225     r = radius / sqrt(r);
226     return (vec2_sum(centre, vec2(x / r, y / r)));
227 }
228 
229 double  vec2_dist(Vec2 v, Vec2 w)
230 {
231     double  dx = v.el[0] - w.el[0];
232     double  dy = v.el[1] - w.el[1];
233 
234     return (sqrt(dx * dx + dy * dy));
235 }
236 
237 double  vec2_sqrdist(Vec2 v, Vec2 w)
238 {
239     double  dx = v.el[0] - w.el[0];
240     double  dy = v.el[1] - w.el[1];
241 
242     return (dx * dx + dy * dy);
243 }
244 
245 double  vec2_angle(Vec2 v, Vec2 w)
246 {
247     return (atan2(vec2_cross(w, v), vec2_dot(w, v)));
248 }
249 
250 Vec2    vec2_perp(Vec2 v)
251 {
252     float   vx, vy;
253 
254     vec2_comps(v, &vx, &vy);
255     return (vec2_unit(vec2(vy, -vx)));
256 }
257 
258 double  vec2_perp_dist(Vec2 p, Vec2 v, Vec2 d)  /* length of
259                                                  * perpendicular from d
260                                                  * to (p,v) */
261 
262 {
263     Vec2    diff = {Vec2_id};
264 
265     diff = vec2_diff(d, p);
266     return (vec2_mod(vec2_diff(diff, vec2_times(vec2_dot(diff, v), v))));
267 }
268 
269 void    vec2_basis(Vec2 up, Vec2 * ex, Vec2 * ey)
270 {
271     if (vec2_mod(up) == 0.0)
272     {
273         *ex = vec2_ex();
274         *ey = vec2_ey();
275         return;
276     }
277     *ey = vec2_unit(up);
278     *ex = vec2_perp(*ey);
279 }
280 
281 int     vec2_parallel(Vec2 v1, Vec2 v2, double dotthres)
282 {
283     return (fabs(vec2_dot(v1, v2)) > dotthres);
284 }
285 
286 Vec2    vec2_read(FILE * fp)
287 {
288     Vec2    v = {Vec2_id};
289 
290     (void) fscanf(fp, "%f %f", &v.el[0], &v.el[1]);
291     return (v);
292 }
293 
294 void    vec2_print(FILE * fp, Vec2 v)
295 {
296     (void) fprintf(fp, "%f %f ", v.el[0], v.el[1]);
297 }
298 
299 void    vec2_pprint(FILE * fp, char *msg, Vec2 v)
300 {
301     (void) fprintf(fp, "%s(%15.6f%15.6f)\n", msg, v.el[0], v.el[1]);
302 }
303 
304 void    vec2_format(Vec2 v)
305 {
306     format("%f %f\n", v.el[0], v.el[1]);
307 }
308 
309 /* Functions useful for setting vec2 ranges for use with
310  * tv_camera2_rect. SMC */
311 
312 /* Return (as a Vec2) the LOWER LEFT corner of a box given by 2 vec2's */
313 Vec2    vec2_less(Vec2 v1, Vec2 v2)
314 {
315     return (vec2(MIN(v1.el[0], v2.el[0]), (MIN(v1.el[1], v2.el[1]))));
316 }
317 
318 
319 /* Return (as a Vec2) the UPPER RIGHT corner of a box given by 2 vec2's */
320 Vec2    vec2_greater(Vec2 v1, Vec2 v2)
321 {
322     return (vec2(MAX(v1.el[0], v2.el[0]), (MAX(v1.el[1], v2.el[1]))));
323 }
324 
325 /* Ensure box (defined by v1 & v2)  includes vec (expanding box if
326  * required) (v1 is the UPPER LEFT) (v2 is LOWER RIGHT). */
327 void    vec2_ranges(Vec2 * v1, Vec2 * v2, Vec2 vec)
328 {
329     *v1 = vec2_less(*v1, vec);
330     *v2 = vec2_greater(*v2, vec);
331 }
332 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.