Fix bug with expanding text.
[geda-pcb/pcjc2.git] / src / clip.c
blob8699e1c21669a6ddf58d9c2c5210780ef27854e0
1 /*!
2 * \file src/clip.c
4 * \brief Functions for inserting points into objects.
6 * <hr>
8 * <h1><b>Copyright.</b></h1>\n
10 * PCB, interactive printed circuit board design
12 * Copyright (C) 1994,1995,1996 Thomas Nau
14 * Copyright (C) 2004 harry eaton
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 * Contact addresses for paper mail and Email:
31 * Thomas Nau, Schlehenweg 15, 88471 Baustetten, Germany
32 * Thomas.Nau@rz.uni-ulm.de
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
40 #include <assert.h>
41 #include <math.h>
42 #include <stdlib.h>
44 #include "global.h"
45 #include "data.h"
46 #include "draw.h"
47 #include "mymem.h"
49 #ifdef HAVE_LIBDMALLOC
50 #include <dmalloc.h>
51 #endif
53 /*!
54 * \brief Clip the line to the clipBox.
56 * Clip X,Y to the given bounding box, plus a margin.
58 * \return true if something to be drawn, false if the whole thing is
59 * clipped.
61 bool
62 ClipLine (double minx, double miny, double maxx, double maxy,
63 double *x1, double *y1,
64 double *x2, double *y2,
65 double margin)
67 double d, r;
69 minx -= margin;
70 miny -= margin;
71 maxx += margin;
72 maxy += margin;
74 /* clip first point on left side */
75 if (*x1 < minx)
77 if (*x2 < minx)
78 return false;
79 d = *x2 - *x1;
80 r = (minx - *x1) / d;
81 *x1 = minx;
82 *y1 += r * (*y2 - *y1);
84 /* clip second point on left side */
85 if (*x2 < minx)
87 d = *x1 - *x2;
88 r = (minx - *x2) / d;
89 *x2 = minx;
90 *y2 += r * (*y1 - *y2);
92 /* clip first point on right side */
93 if (*x1 > maxx)
95 if (*x2 > maxx)
96 return false;
97 d = *x2 - *x1;
98 r = (maxx - *x1) / d;
99 *x1 = maxx;
100 *y1 += r * (*y2 - *y1);
102 /* clip second point on right side */
103 if (*x2 > maxx)
105 d = *x1 - *x2;
106 r = (maxx - *x2) / d;
107 *x2 = maxx;
108 *y2 += r * (*y1 - *y2);
111 /* clip first point on top */
112 if (*y1 < miny)
114 if (*y2 < miny)
115 return false;
116 d = *y2 - *y1;
117 r = (miny - *y1) / d;
118 *y1 = miny;
119 *x1 += r * (*x2 - *x1);
121 /* clip second point on top */
122 if (*y2 < miny)
124 d = *y1 - *y2;
125 r = (miny - *y2) / d;
126 *y2 = miny;
127 *x2 += r * (*x1 - *x2);
129 /* clip first point on bottom */
130 if (*y1 > maxy)
132 if (*y2 > maxy)
133 return false;
134 d = *y2 - *y1;
135 r = (maxy - *y1) / d;
136 *y1 = maxy;
137 *x1 += r * (*x2 - *x1);
139 /* clip second point on top */
140 if (*y2 > maxy)
142 d = *y1 - *y2;
143 r = (maxy - *y2) / d;
144 *y2 = maxy;
145 *x2 += r * (*x1 - *x2);
147 return true;