1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.4
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 // classes dda_line_interpolator, dda2_line_interpolator
18 //----------------------------------------------------------------------------
20 #ifndef AGG_DDA_LINE_INCLUDED
21 #define AGG_DDA_LINE_INCLUDED
24 #include "agg_basics.h"
29 //===================================================dda_line_interpolator
30 template<int FractionShift
, int YShift
=0> class dda_line_interpolator
33 //--------------------------------------------------------------------
34 dda_line_interpolator() {}
36 //--------------------------------------------------------------------
37 dda_line_interpolator(int y1
, int y2
, unsigned count
) :
39 m_inc(((y2
- y1
) << FractionShift
) / int(count
)),
44 //--------------------------------------------------------------------
50 //--------------------------------------------------------------------
56 //--------------------------------------------------------------------
57 void operator += (unsigned n
)
62 //--------------------------------------------------------------------
63 void operator -= (unsigned n
)
69 //--------------------------------------------------------------------
70 int y() const { return m_y
+ (m_dy
>> (FractionShift
-YShift
)); }
71 int dy() const { return m_dy
; }
84 //=================================================dda2_line_interpolator
85 class dda2_line_interpolator
88 typedef int save_data_type
;
89 enum save_size_e
{ save_size
= 2 };
91 //--------------------------------------------------------------------
92 dda2_line_interpolator() {}
94 //-------------------------------------------- Forward-adjusted line
95 dda2_line_interpolator(int y1
, int y2
, int count
) :
96 m_cnt(count
<= 0 ? 1 : count
),
97 m_lft((y2
- y1
) / m_cnt
),
98 m_rem((y2
- y1
) % m_cnt
),
111 //-------------------------------------------- Backward-adjusted line
112 dda2_line_interpolator(int y1
, int y2
, int count
, int) :
113 m_cnt(count
<= 0 ? 1 : count
),
114 m_lft((y2
- y1
) / m_cnt
),
115 m_rem((y2
- y1
) % m_cnt
),
127 //-------------------------------------------- Backward-adjusted line
128 dda2_line_interpolator(int y
, int count
) :
129 m_cnt(count
<= 0 ? 1 : count
),
144 //--------------------------------------------------------------------
145 void save(save_data_type
* data
) const
151 //--------------------------------------------------------------------
152 void load(const save_data_type
* data
)
158 //--------------------------------------------------------------------
170 //--------------------------------------------------------------------
182 //--------------------------------------------------------------------
183 void adjust_forward()
188 //--------------------------------------------------------------------
189 void adjust_backward()
194 //--------------------------------------------------------------------
195 int mod() const { return m_mod
; }
196 int rem() const { return m_rem
; }
197 int lft() const { return m_lft
; }
199 //--------------------------------------------------------------------
200 int y() const { return m_y
; }
216 //---------------------------------------------line_bresenham_interpolator
217 class line_bresenham_interpolator
220 enum subpixel_scale_e
223 subpixel_scale
= 1 << subpixel_shift
,
224 subpixel_mask
= subpixel_scale
- 1
227 //--------------------------------------------------------------------
228 static int line_lr(int v
) { return v
>> subpixel_shift
; }
230 //--------------------------------------------------------------------
231 line_bresenham_interpolator(int x1
, int y1
, int x2
, int y2
) :
232 m_x1_lr(line_lr(x1
)),
233 m_y1_lr(line_lr(y1
)),
234 m_x2_lr(line_lr(x2
)),
235 m_y2_lr(line_lr(y2
)),
236 m_ver(abs(m_x2_lr
- m_x1_lr
) < abs(m_y2_lr
- m_y1_lr
)),
237 m_len(m_ver
? abs(m_y2_lr
- m_y1_lr
) :
238 abs(m_x2_lr
- m_x1_lr
)),
239 m_inc(m_ver
? ((y2
> y1
) ? 1 : -1) : ((x2
> x1
) ? 1 : -1)),
240 m_interpolator(m_ver
? x1
: y1
,
246 //--------------------------------------------------------------------
247 bool is_ver() const { return m_ver
; }
248 unsigned len() const { return m_len
; }
249 int inc() const { return m_inc
; }
251 //--------------------------------------------------------------------
258 //--------------------------------------------------------------------
265 //--------------------------------------------------------------------
266 int x1() const { return m_x1_lr
; }
267 int y1() const { return m_y1_lr
; }
268 int x2() const { return line_lr(m_interpolator
.y()); }
269 int y2() const { return line_lr(m_interpolator
.y()); }
270 int x2_hr() const { return m_interpolator
.y(); }
271 int y2_hr() const { return m_interpolator
.y(); }
281 dda2_line_interpolator m_interpolator
;