(no commit message)
[geda-pcb/pcjc2.git] / src / clip.c
blobe4fef34cec0e7bc6e73e5ee30e0e0edb2a8f59cc
1 /*
2 * COPYRIGHT
4 * PCB, interactive printed circuit board design
5 * Copyright (C) 1994,1995,1996 Thomas Nau
6 * Copyright (C) 2004 harry eaton
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Contact addresses for paper mail and Email:
23 * Thomas Nau, Schlehenweg 15, 88471 Baustetten, Germany
24 * Thomas.Nau@rz.uni-ulm.de
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
33 #include <assert.h>
34 #include <math.h>
35 #include <stdlib.h>
37 #include "global.h"
38 #include "data.h"
39 #include "draw.h"
40 #include "mymem.h"
42 #ifdef HAVE_LIBDMALLOC
43 #include <dmalloc.h>
44 #endif
46 /* Clip the line to the clipBox
47 * return true if something to be drawn
48 * false if the whole thing is clipped
50 bool
51 ClipLine (double minx, double miny, double maxx, double maxy,
52 double *x1, double *y1,
53 double *x2, double *y2,
54 double margin)
56 double d, r;
58 minx -= margin;
59 miny -= margin;
60 maxx += margin;
61 maxy += margin;
63 /* clip first point on left side */
64 if (*x1 < minx)
66 if (*x2 < minx)
67 return false;
68 d = *x2 - *x1;
69 r = (minx - *x1) / d;
70 *x1 = minx;
71 *y1 += r * (*y2 - *y1);
73 /* clip second point on left side */
74 if (*x2 < minx)
76 d = *x1 - *x2;
77 r = (minx - *x2) / d;
78 *x2 = minx;
79 *y2 += r * (*y1 - *y2);
81 /* clip first point on right side */
82 if (*x1 > maxx)
84 if (*x2 > maxx)
85 return false;
86 d = *x2 - *x1;
87 r = (maxx - *x1) / d;
88 *x1 = maxx;
89 *y1 += r * (*y2 - *y1);
91 /* clip second point on right side */
92 if (*x2 > maxx)
94 d = *x1 - *x2;
95 r = (maxx - *x2) / d;
96 *x2 = maxx;
97 *y2 += r * (*y1 - *y2);
100 /* clip first point on top */
101 if (*y1 < miny)
103 if (*y2 < miny)
104 return false;
105 d = *y2 - *y1;
106 r = (miny - *y1) / d;
107 *y1 = miny;
108 *x1 += r * (*x2 - *x1);
110 /* clip second point on top */
111 if (*y2 < miny)
113 d = *y1 - *y2;
114 r = (miny - *y2) / d;
115 *y2 = miny;
116 *x2 += r * (*x1 - *x2);
118 /* clip first point on bottom */
119 if (*y1 > maxy)
121 if (*y2 > maxy)
122 return false;
123 d = *y2 - *y1;
124 r = (maxy - *y1) / d;
125 *y1 = maxy;
126 *x1 += r * (*x2 - *x1);
128 /* clip second point on top */
129 if (*y2 > maxy)
131 d = *y1 - *y2;
132 r = (maxy - *y2) / d;
133 *y2 = maxy;
134 *x2 += r * (*x1 - *x2);
136 return true;