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

Linux Cross Reference
Tina4/src/vision/line2/inter.c

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

  1 #include <string.h>
  2 #include <math.h>
  3 #include <stdio.h>
  4 #include <values.h>
  5 #include <sys/param.h>
  6 #include <tina/sys.h>
  7 #include <tina/sysfuncs.h>
  8 #include <tina/math.h>
  9 #include <tina/mathfuncs.h>
 10 #include <tina/vision.h>
 11 #include <tina/visionfuncs.h>
 12 #include <tina/tv.h>
 13 #include <tina/tvfuncs.h>
 14 #include <tina/draw.h>
 15 #include <tina/drawfuncs.h>
 16 #include <tina/toolsfuncs.h>
 17 
 18 Line2 *line2_circ_inter(Line2 *l, Vec2 p, double r)
 19 {
 20         double A, B, C, D;
 21         double m, c;
 22         double dx, dy;
 23         double t0, t1, t;       /* Parametric line coefficients */
 24         Vec2 s0, s1, s;
 25 
 26         double start[2], end[2], circ[2], i0[2], i1[2];
 27         int x, y;
 28 
 29         x=0;
 30         y=1;
 31 
 32         start[x] = vec2_x(l->p1);
 33         start[y] = vec2_y(l->p1);
 34         end[x] = vec2_x(l->p2);
 35         end[y] = vec2_y(l->p2);
 36 
 37         circ[x] = vec2_x(p);
 38         circ[y] = vec2_y(p);
 39 
 40         dx = end[x]-start[x];
 41         dy = end[y]-start[y];
 42 
 43         if (abs(dy)>abs(dx))         /* All x's -> y's and vice versa */
 44         {
 45                 x=1;
 46                 y=0;    
 47                 dx = end[x]-start[x]; /* Recalc. */
 48                 dy = end[y]-start[y];
 49         }
 50 
 51         m = dy/dx;
 52         c = start[y]-m*start[x];
 53 
 54         A = 1.0 + m*m;
 55         B = -2.0*circ[x] + 2.0*m*c - 2.0*m*circ[y];
 56         C = circ[x]*circ[x] + c*c - 2.0*c*circ[y] + circ[y]*circ[y] - r*r;
 57 
 58         D = B*B - 4.0*A*C;
 59 
 60         if (D<=0.0) /* No intersection */
 61         {
 62                 return NULL;
 63         }
 64 
 65         i0[x] = (-B+sqrt(D))/(2.0*A);
 66         i0[y] = m*i0[x]+c;
 67         i1[x] = (-B-sqrt(D))/(2.0*A);
 68         i1[y] = m*i1[x]+c;
 69 
 70         t0 = (i0[x]-start[x])/(end[x]-start[x]);
 71         t1 = (i1[x]-start[x])/(end[x]-start[x]);
 72 
 73         if (((t0>=1.0) && (t1>=1.0)) || 
 74                 ((t0<=0.0) && (t1<=0.0))    )   /* Line extent does not intersect */
 75         {
 76                 return NULL;
 77         }
 78 
 79         if (((t0>=1.0) && (t1<=0.0)) || 
 80                 ((t0<=0.0) && (t1>=1.0))    )   /* Whole line in circle */
 81         {
 82                 return line2_copy(l);
 83         }
 84 
 85         /* Need to split the line */
 86 
 87         x = 0;
 88         y = 1;
 89 
 90         if (((t0>=0.0) && (t0<=1.0)) && 
 91                 ((t1>=0.0) && (t1<=1.0))    )   /* The line is split twice */
 92         {
 93                 vec2_x(s0) = start[x]+t0*(end[x]-start[x]);
 94                 vec2_y(s0) = start[y]+t0*(end[y]-start[y]);
 95                 vec2_x(s1) = start[x]+t1*(end[x]-start[x]);
 96                 vec2_y(s1) = start[y]+t1*(end[y]-start[y]);
 97 
 98                 return line2_make(s0, s1, LINE2);
 99         }
100 
101         if ((t0>=0.0) && (t0<=1.0))
102                 t = t0;
103         else
104                 t = t1;
105 
106         vec2_x(s) = start[x]+t*(end[x]-start[x]);
107         vec2_y(s) = start[y]+t*(end[y]-start[y]);
108 
109         if (vec2_dist(p, l->p1)<r) /* Use p1 */
110         {
111                 return line2_make(l->p1, s, LINE2);
112         }
113         else    /* Use p2 */
114         {
115                 return line2_make(l->p2, s, LINE2);
116         }       
117 }
118 

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