3 #ifndef __zqb_cliplinebyrect_hpp
4 #define __zqb_cliplinebyrect_hpp
6 #include "zqb_macros.h"
18 template<
class T>
bool
19 ZqbClipLineByRect (T x[2],
29 ZqbASCENT(rx[0], rx[1]);
30 ZqbASCENT(ry[0], ry[1]);
33 if((x[0] < rx[0] && x[1] < rx[0]) || (x[0] > rx[1] && x[1] > rx[1]) ||
34 (y[0] < ry[0] && y[1] < ry[0]) || (y[0] > ry[1] && y[1] > ry[1]))
38 if(rx[0] <= x[0] && x[0] <= rx[1] && rx[0] <= x[1] && x[1] <= rx[1] &&
39 ry[0] <= y[0] && y[0] <= ry[1] && ry[0] <= y[1] && y[1] <= ry[1])
43 if(x[0] != x[1] && y[0] == y[1])
48 xf[0] = ZqbMIN(rx[1], x[0]);
49 xf[1] = ZqbMAX(rx[0], x[1]);
53 xf[1] = ZqbMIN(rx[1], x[1]);
54 xf[0] = ZqbMAX(rx[0], x[0]);
58 else if(x[0] == x[1] && y[0] != y[1])
63 yf[0] = ZqbMIN(ry[1], y[0]);
64 yf[1] = ZqbMAX(ry[0], y[1]);
68 yf[1] = ZqbMIN(ry[1], y[1]);
69 yf[0] = ZqbMAX(ry[0], y[0]);
78 double k = (y[1] - y[0])/(x[1] - x[0]);
79 double b = y[0] - k * x[0];
83 double minx = ZqbMIN((ry[0] - b)/k, (ry[1] - b)/k);
84 double maxx = ZqbMAX((ry[0] - b)/k, (ry[1] - b)/k);
86 if(rx[1] < minx || rx[0] > maxx)
91 minx = ZqbMAX(rx[0], minx);
92 maxx = ZqbMIN(rx[1], maxx);
94 if(x[0] < minx && x[1] < minx ||
95 x[0] > maxx && x[1] > maxx)
101 xf[0] = ZqbMIN(x[0], maxx);
102 xf[1] = ZqbMAX(x[1], minx);
106 xf[1] = ZqbMIN(x[1], maxx);
107 xf[0] = ZqbMAX(x[0], minx);
109 yf[0] = k * xf[0] + b;
110 yf[1] = k * xf[1] + b;
114 x[0] = xf[0]; y[0] = yf[0];
115 x[1] = xf[1]; y[1] = yf[1];