1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.3
3 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
5 // Permission to copy, use, modify, sell and distribute this software
6 // is granted provided this copyright notice appears in all copies.
7 // This software is provided "as is" without express or implied
8 // warranty, and with no claim as to its suitability for any purpose.
10 //----------------------------------------------------------------------------
11 // Contact: mcseem@antigrain.com
12 // mcseemagg@yahoo.com
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
16 // Liang-Barsky clipping
18 //----------------------------------------------------------------------------
19 #ifndef AGG_CLIP_LIANG_BARSKY_INCLUDED
20 #define AGG_CLIP_LIANG_BARSKY_INCLUDED
22 #include "agg_basics.h"
27 //----------------------------------------------------------clipping_flags
28 // Determine the clipping code of the vertex according to the
29 // Cyrus-Beck line clipping algorithm
34 // -------+--------+-------- clip_box.y2
38 // -------+--------+-------- clip_box.y1
42 // clip_box.x1 clip_box.x2
46 inline unsigned clipping_flags(T x
, T y
, const rect_base
<T
>& clip_box
)
48 return (x
> clip_box
.x2
) |
49 ((y
> clip_box
.y2
) << 1) |
50 ((x
< clip_box
.x1
) << 2) |
51 ((y
< clip_box
.y1
) << 3);
56 //-------------------------------------------------------clip_liang_barsky
58 /*inline*/ unsigned clip_liang_barsky(T x1
, T y1
, T x2
, T y2
,
59 const rect_base
<T
>& clip_box
,
62 const double nearzero
= 1e-30;
64 double deltax
= x2
- x1
;
65 double deltay
= y2
- y1
;
81 // bump off of the vertical
82 deltax
= (x1
> clip_box
.x1
) ? -nearzero
: nearzero
;
87 // bump off of the horizontal
88 deltay
= (y1
> clip_box
.y1
) ? -nearzero
: nearzero
;
115 tinx
= (xin
- x1
) / deltax
;
116 tiny
= (yin
- y1
) / deltay
;
142 toutx
= (xout
- x1
) / deltax
;
143 touty
= (yout
- y1
) / deltay
;
145 tout1
= (toutx
< touty
) ? toutx
: touty
;
147 if(tin2
> 0.0 || tout1
> 0.0)
156 *y
++ = (T
)(y1
+ tinx
* deltay
);
160 *x
++ = (T
)(x1
+ tiny
* deltax
);
171 *y
++ = (T
)(y1
+ toutx
* deltay
);
175 *x
++ = (T
)(x1
+ touty
* deltax
);