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

Linux Cross Reference
Tina5/tina-libs/tina/geometry/geomLine_inter.c

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

  1 /**********
  2  * 
  3  * This file is part of the TINA Open Source Image Analysis Environment
  4  * henceforth known as TINA
  5  *
  6  * TINA is free software; you can redistribute it and/or modify
  7  * it under the terms of the GNU Lesser General Public License as 
  8  * published by the Free Software Foundation.
  9  *
 10  * TINA is distributed in the hope that it will be useful,
 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  * GNU Lesser General Public License for more details.
 14  *
 15  * You should have received a copy of the GNU Lesser General Public License
 16  * along with TINA; if not, write to the Free Software Foundation, Inc., 
 17  * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  *
 19  **********
 20  * 
 21  * Program :    TINA
 22  * File    :  $Source: /home/tina/cvs/tina-libs/tina/geometry/geomLine_inter.c,v $
 23  * Date    :  $Date: 2005/01/09 17:49:25 $
 24  * Version :  $Revision: 1.2 $
 25  * CVS Id  :  $Id: geomLine_inter.c,v 1.2 2005/01/09 17:49:25 paul Exp $
 26  *
 27  * Notes :
 28  *
 29  *********
 30 */
 31 
 32 
 33 #include "geomLine_inter.h"
 34 
 35 #if HAVE_CONFIG_H
 36   #include <config.h>
 37 #endif
 38 
 39 #include <stdio.h>
 40 #include <math.h>
 41 #include <stdlib.h>
 42 #include <tina/sys/sysDef.h>
 43 #include <tina/sys/sysPro.h>
 44 #include <tina/math/mathDef.h>
 45 #include <tina/math/mathPro.h>
 46 #include <tina/geometry/geomDef.h>
 47 #include <tina/geometry/geom_LineDef.h>
 48 #include <tina/geometry/geom_LinePro.h>
 49 
 50 Line2 *line2_circ_inter(Line2 *l, Vec2 p, double r)
 51 {
 52         double A, B, C, D;
 53         double m, c;
 54         double dx, dy;
 55         double t0, t1, t;       /* Parametric line coefficients */
 56         Vec2 s0, s1, s;
 57 
 58         double start[2], end[2], circ[2], i0[2], i1[2];
 59         int x, y;
 60 
 61         x=0;
 62         y=1;
 63 
 64         start[x] = vec2_x(l->p1);
 65         start[y] = vec2_y(l->p1);
 66         end[x] = vec2_x(l->p2);
 67         end[y] = vec2_y(l->p2);
 68 
 69         circ[x] = vec2_x(p);
 70         circ[y] = vec2_y(p);
 71 
 72         dx = end[x]-start[x];
 73         dy = end[y]-start[y];
 74 
 75         if (abs(dy)>abs(dx))         /* All x's -> y's and vice versa */
 76         {
 77                 x=1;
 78                 y=0;    
 79                 dx = end[x]-start[x]; /* Recalc. */
 80                 dy = end[y]-start[y];
 81         }
 82 
 83         m = dy/dx;
 84         c = start[y]-m*start[x];
 85 
 86         A = 1.0 + m*m;
 87         B = -2.0*circ[x] + 2.0*m*c - 2.0*m*circ[y];
 88         C = circ[x]*circ[x] + c*c - 2.0*c*circ[y] + circ[y]*circ[y] - r*r;
 89 
 90         D = B*B - 4.0*A*C;
 91 
 92         if (D<=0.0) /* No intersection */
 93         {
 94                 return NULL;
 95         }
 96 
 97         i0[x] = (-B+sqrt(D))/(2.0*A);
 98         i0[y] = m*i0[x]+c;
 99         i1[x] = (-B-sqrt(D))/(2.0*A);
100         i1[y] = m*i1[x]+c;
101 
102         t0 = (i0[x]-start[x])/(end[x]-start[x]);
103         t1 = (i1[x]-start[x])/(end[x]-start[x]);
104 
105         if (((t0>=1.0) && (t1>=1.0)) || 
106                 ((t0<=0.0) && (t1<=0.0))    )   /* Line extent does not intersect */
107         {
108                 return NULL;
109         }
110 
111         if (((t0>=1.0) && (t1<=0.0)) || 
112                 ((t0<=0.0) && (t1>=1.0))    )   /* Whole line in circle */
113         {
114                 return line2_copy(l);
115         }
116 
117         /* Need to split the line */
118 
119         x = 0;
120         y = 1;
121 
122         if (((t0>=0.0) && (t0<=1.0)) && 
123                 ((t1>=0.0) && (t1<=1.0))    )   /* The line is split twice */
124         {
125                 vec2_x(s0) = start[x]+t0*(end[x]-start[x]);
126                 vec2_y(s0) = start[y]+t0*(end[y]-start[y]);
127                 vec2_x(s1) = start[x]+t1*(end[x]-start[x]);
128                 vec2_y(s1) = start[y]+t1*(end[y]-start[y]);
129 
130                 return line2_make(s0, s1, LINE2);
131         }
132 
133         if ((t0>=0.0) && (t0<=1.0))
134                 t = t0;
135         else
136                 t = t1;
137 
138         vec2_x(s) = start[x]+t*(end[x]-start[x]);
139         vec2_y(s) = start[y]+t*(end[y]-start[y]);
140 
141         if (vec2_dist(p, l->p1)<r) /* Use p1 */
142         {
143                 return line2_make(l->p1, s, LINE2);
144         }
145         else    /* Use p2 */
146         {
147                 return line2_make(l->p2, s, LINE2);
148         }       
149 }
150 

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