Move internationalization macros to one header
[geda-pcb/gde.git] / src / clip.c
blobf24f4429b45e5f47babb36bbe67603237d4d19b4
1 /* $Id$ */
3 /*
4 * COPYRIGHT
6 * PCB, interactive printed circuit board design
7 * Copyright (C) 1994,1995,1996 Thomas Nau
8 * Copyright (C) 2004 harry eaton
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * Contact addresses for paper mail and Email:
25 * Thomas Nau, Schlehenweg 15, 88471 Baustetten, Germany
26 * Thomas.Nau@rz.uni-ulm.de
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
35 #include <assert.h>
36 #include <math.h>
37 #include <stdlib.h>
39 #include "global.h"
40 #include "data.h"
41 #include "draw.h"
42 #include "mymem.h"
44 #ifdef HAVE_LIBDMALLOC
45 #include <dmalloc.h>
46 #endif
48 RCSID ("$Id$");
50 /* Clip the line to the clipBox
51 * return True if something to be drawn
52 * false if the whole thing is clipped
54 Boolean
55 ClipLine (double minx, double miny, double maxx, double maxy,
56 double *x1, double *y1,
57 double *x2, double *y2,
58 double margin)
60 double d, r;
62 minx -= margin;
63 miny -= margin;
64 maxx += margin;
65 maxy += margin;
67 /* clip first point on left side */
68 if (*x1 < minx)
70 if (*x2 < minx)
71 return False;
72 d = *x2 - *x1;
73 r = (minx - *x1) / d;
74 *x1 = minx;
75 *y1 += r * (*y2 - *y1);
77 /* clip second point on left side */
78 if (*x2 < minx)
80 d = *x1 - *x2;
81 r = (minx - *x2) / d;
82 *x2 = minx;
83 *y2 += r * (*y1 - *y2);
85 /* clip first point on right side */
86 if (*x1 > maxx)
88 if (*x2 > maxx)
89 return False;
90 d = *x2 - *x1;
91 r = (maxx - *x1) / d;
92 *x1 = maxx;
93 *y1 += r * (*y2 - *y1);
95 /* clip second point on right side */
96 if (*x2 > maxx)
98 d = *x1 - *x2;
99 r = (maxx - *x2) / d;
100 *x2 = maxx;
101 *y2 += r * (*y1 - *y2);
104 /* clip first point on top */
105 if (*y1 < miny)
107 if (*y2 < miny)
108 return False;
109 d = *y2 - *y1;
110 r = (miny - *y1) / d;
111 *y1 = miny;
112 *x1 += r * (*x2 - *x1);
114 /* clip second point on top */
115 if (*y2 < miny)
117 d = *y1 - *y2;
118 r = (miny - *y2) / d;
119 *y2 = miny;
120 *x2 += r * (*x1 - *x2);
122 /* clip first point on bottom */
123 if (*y1 > maxy)
125 if (*y2 > maxy)
126 return False;
127 d = *y2 - *y1;
128 r = (maxy - *y1) / d;
129 *y1 = maxy;
130 *x1 += r * (*x2 - *x1);
132 /* clip second point on top */
133 if (*y2 > maxy)
135 d = *y1 - *y2;
136 r = (maxy - *y2) / d;
137 *y2 = maxy;
138 *x2 += r * (*x1 - *x2);
140 return True;