pcb 4.1.1
An interactive printed circuit board layout editor.

clip.c

Go to the documentation of this file.
00001 
00036 #ifdef HAVE_CONFIG_H
00037 #include "config.h"
00038 #endif
00039 
00040 #include <assert.h>
00041 #include <math.h>
00042 #include <stdlib.h>
00043 
00044 #include "global.h"
00045 #include "data.h"
00046 #include "draw.h"
00047 #include "mymem.h"
00048 
00049 #ifdef HAVE_LIBDMALLOC
00050 #include <dmalloc.h>
00051 #endif
00052 
00061 bool
00062 ClipLine (double minx, double miny, double maxx, double maxy,
00063           double *x1, double *y1,
00064           double *x2, double *y2,
00065           double margin)
00066 {
00067   double d, r;
00068 
00069   minx -= margin;
00070   miny -= margin;
00071   maxx += margin;
00072   maxy += margin;
00073 
00074   /* clip first point on left side */
00075   if (*x1 < minx)
00076     {
00077       if (*x2 < minx)
00078         return false;
00079       d = *x2 - *x1;
00080       r = (minx - *x1) / d;
00081       *x1 = minx;
00082       *y1 += r * (*y2 - *y1);
00083     }
00084   /* clip second point on left side */
00085   if (*x2 < minx)
00086     {
00087       d = *x1 - *x2;
00088       r = (minx - *x2) / d;
00089       *x2 = minx;
00090       *y2 += r * (*y1 - *y2);
00091     }
00092   /* clip first point on right side */
00093   if (*x1 > maxx)
00094     {
00095       if (*x2 > maxx)
00096         return false;
00097       d = *x2 - *x1;
00098       r = (maxx - *x1) / d;
00099       *x1 = maxx;
00100       *y1 += r * (*y2 - *y1);
00101     }
00102   /* clip second point on right side */
00103   if (*x2 > maxx)
00104     {
00105       d = *x1 - *x2;
00106       r = (maxx - *x2) / d;
00107       *x2 = maxx;
00108       *y2 += r * (*y1 - *y2);
00109     }
00110 
00111   /* clip first point on top */
00112   if (*y1 < miny)
00113     {
00114       if (*y2 < miny)
00115         return false;
00116       d = *y2 - *y1;
00117       r = (miny - *y1) / d;
00118       *y1 = miny;
00119       *x1 += r * (*x2 - *x1);
00120     }
00121   /* clip second point on top */
00122   if (*y2 < miny)
00123     {
00124       d = *y1 - *y2;
00125       r = (miny - *y2) / d;
00126       *y2 = miny;
00127       *x2 += r * (*x1 - *x2);
00128     }
00129   /* clip first point on bottom */
00130   if (*y1 > maxy)
00131     {
00132       if (*y2 > maxy)
00133         return false;
00134       d = *y2 - *y1;
00135       r = (maxy - *y1) / d;
00136       *y1 = maxy;
00137       *x1 += r * (*x2 - *x1);
00138     }
00139   /* clip second point on top */
00140   if (*y2 > maxy)
00141     {
00142       d = *y1 - *y2;
00143       r = (maxy - *y2) / d;
00144       *y2 = maxy;
00145       *x2 += r * (*x1 - *x2);
00146     }
00147   return true;
00148 }