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

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

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

  1 /**@(#)2D geometry
  2 **/
  3 
  4 #include <stdio.h>
  5 #include <math.h>
  6 #include <tina/sys.h>
  7 #include <tina/math.h>
  8 #include <tina/mathfuncs.h>
  9 
 10 /***
 11 Notation:
 12 q - point position
 13 l - point on line,  v - unit direction of line
 14 p - point on plane, n - unit normal to plane
 15 ***/
 16 
 17 Vec2    vec2_midpoint(Vec2 q1, Vec2 q2)
 18 {
 19     Vec2    midpoint = {Vec2_id};
 20 
 21     vec2_x(midpoint) = (float)0.5 * (vec2_x(q1) + vec2_x(q2));
 22     vec2_y(midpoint) = (float)0.5 * (vec2_y(q1) + vec2_y(q2));
 23     return (midpoint);
 24 }
 25 
 26 Vec2    vec2_projperp(Vec2 u, Vec2 v)   /**part of  u  perpendicular to unit  v**/
 27 
 28 {
 29     return (vec2_diff(u, vec2_times(vec2_dot(u, v), v)));
 30 }
 31 
 32 Vec2    vec2_projpar(Vec2 u, Vec2 v)    /**part of  u  parallel to unit  v**/
 33 
 34 {
 35     return (vec2_times(vec2_dot(u, v), v));
 36 }
 37 
 38 Vec2    vec2_proj_on_line(Vec2 q, Vec2 l, Vec2 v)
 39 {
 40     Vec2    lq = {Vec2_id};
 41 
 42     lq = vec2_diff(q, l);
 43     return (vec2_sum(l, vec2_times(vec2_dot(lq, v), v)));
 44 }
 45 
 46 Vec2    vec2_inter_lines(Vec2 l1, Vec2 v1, Vec2 l2, Vec2 v2)
 47 {
 48     double  c = vec2_cross(v1, v2), t1;
 49 
 50     if (c == 0.0)
 51         return (vec2_midpoint(l1, l2));
 52     t1 = vec2_cross(vec2_diff(l2, l1), v2) / c;
 53     return (vec2_sum(l1, vec2_times(t1, v1)));
 54 }
 55 
 56 void    vec2_join_2_points(Vec2 q1, Vec2 q2, Vec2 * l, Vec2 * v)
 57 {
 58     *l = vec2_times(0.5, vec2_sum(q1, q2));
 59     *v = vec2_unit(vec2_diff(q2, q1));
 60 }
 61 
 62 double  vec2_dist_point_line(Vec2 q, Vec2 l, Vec2 v)
 63 {
 64     Vec2    dq = {Vec2_id};
 65 
 66     dq = vec2_projperp(vec2_diff(q, l), v);
 67     return (vec2_mod(dq));
 68 }
 69 
 70 void    vec2_circ_3_points(Vec2 p1, Vec2 p2, Vec2 p3, Vec2 * centre, double *radius)
 71 {
 72     Vec2    a = {Vec2_id};
 73     Vec2    b = {Vec2_id};
 74     double  a2, b2, ab, u, v, w;
 75 
 76     a = vec2_diff(p2, p1);
 77     b = vec2_diff(p3, p1);
 78 
 79     a2 = vec2_sqrmod(a);
 80     b2 = vec2_sqrmod(b);
 81     ab = vec2_dot(a, b);
 82 
 83     u = b2 * (a2 - ab);
 84     v = a2 * (b2 - ab);
 85     w = 2.0 * (a2 * b2 - ab * ab);
 86 
 87     a = vec2_times(u, a);
 88     b = vec2_times(v, b);
 89 
 90     *centre = vec2_sum(p1, vec2_times(1 / w, vec2_sum(a, b)));
 91     *radius = sqrt((a2 * b2 * (a2 + b2 - 2.0 * ab)) / (2.0 * w));
 92 }
 93 
 94 Vec2   *vec2_inter_par_test(Vec2 p, Vec2 v1, Vec2 q, Vec2 v2, double parallel)
 95 {
 96     Vec2    cp = {Vec2_id};
 97     Vec2    b = {Vec2_id};
 98     Vec2   *isct;
 99     double  a;
100 
101     cp = vec2_inter_lines(p, v1, q, v2);
102     if (!vec2_parallel(v1, v2, parallel))
103     {
104         cp.el[0] = -v2.el[1];
105         cp.el[1] = v2.el[0];
106 
107         a = vec2_dot(cp, vec2_diff(p, q)) / vec2_dot(cp, v1);
108         b = vec2_times(a, v1);
109 
110         isct = vec2_alloc();
111         isct->el[0] = p.el[0] - b.el[0];
112         isct->el[1] = p.el[1] - b.el[1];
113         return (isct);
114     } else
115         return (NULL);
116 }
117 

~ [ 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.