agg_dda_line.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //----------------------------------------------------------------------------
  2. // Anti-Grain Geometry - Version 2.4
  3. // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
  4. //
  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.
  9. //
  10. //----------------------------------------------------------------------------
  11. // Contact: mcseem@antigrain.com
  12. // mcseemagg@yahoo.com
  13. // http://www.antigrain.com
  14. //----------------------------------------------------------------------------
  15. //
  16. // classes dda_line_interpolator, dda2_line_interpolator
  17. //
  18. //----------------------------------------------------------------------------
  19. #ifndef AGG_DDA_LINE_INCLUDED
  20. #define AGG_DDA_LINE_INCLUDED
  21. #include <stdlib.h>
  22. #include "agg_basics.h"
  23. namespace agg
  24. {
  25. //===================================================dda_line_interpolator
  26. template<int FractionShift, int YShift=0> class dda_line_interpolator
  27. {
  28. public:
  29. //--------------------------------------------------------------------
  30. dda_line_interpolator() {}
  31. //--------------------------------------------------------------------
  32. dda_line_interpolator(int y1, int y2, unsigned count) :
  33. m_y(y1),
  34. m_inc(((y2 - y1) << FractionShift) / int(count)),
  35. m_dy(0)
  36. {
  37. }
  38. //--------------------------------------------------------------------
  39. void operator ++ ()
  40. {
  41. m_dy += m_inc;
  42. }
  43. //--------------------------------------------------------------------
  44. void operator -- ()
  45. {
  46. m_dy -= m_inc;
  47. }
  48. //--------------------------------------------------------------------
  49. void operator += (unsigned n)
  50. {
  51. m_dy += m_inc * n;
  52. }
  53. //--------------------------------------------------------------------
  54. void operator -= (unsigned n)
  55. {
  56. m_dy -= m_inc * n;
  57. }
  58. //--------------------------------------------------------------------
  59. int y() const { return m_y + (m_dy >> (FractionShift-YShift)); }
  60. int dy() const { return m_dy; }
  61. private:
  62. int m_y;
  63. int m_inc;
  64. int m_dy;
  65. };
  66. //=================================================dda2_line_interpolator
  67. class dda2_line_interpolator
  68. {
  69. public:
  70. typedef int save_data_type;
  71. enum save_size_e { save_size = 2 };
  72. //--------------------------------------------------------------------
  73. dda2_line_interpolator() {}
  74. //-------------------------------------------- Forward-adjusted line
  75. dda2_line_interpolator(int y1, int y2, int count) :
  76. m_cnt(count <= 0 ? 1 : count),
  77. m_lft((y2 - y1) / m_cnt),
  78. m_rem((y2 - y1) % m_cnt),
  79. m_mod(m_rem),
  80. m_y(y1)
  81. {
  82. if(m_mod <= 0)
  83. {
  84. m_mod += count;
  85. m_rem += count;
  86. m_lft--;
  87. }
  88. m_mod -= count;
  89. }
  90. //-------------------------------------------- Backward-adjusted line
  91. dda2_line_interpolator(int y1, int y2, int count, int) :
  92. m_cnt(count <= 0 ? 1 : count),
  93. m_lft((y2 - y1) / m_cnt),
  94. m_rem((y2 - y1) % m_cnt),
  95. m_mod(m_rem),
  96. m_y(y1)
  97. {
  98. if(m_mod <= 0)
  99. {
  100. m_mod += count;
  101. m_rem += count;
  102. m_lft--;
  103. }
  104. }
  105. //-------------------------------------------- Backward-adjusted line
  106. dda2_line_interpolator(int y, int count) :
  107. m_cnt(count <= 0 ? 1 : count),
  108. m_lft(y / m_cnt),
  109. m_rem(y % m_cnt),
  110. m_mod(m_rem),
  111. m_y(0)
  112. {
  113. if(m_mod <= 0)
  114. {
  115. m_mod += count;
  116. m_rem += count;
  117. m_lft--;
  118. }
  119. }
  120. //--------------------------------------------------------------------
  121. void save(save_data_type* data) const
  122. {
  123. data[0] = m_mod;
  124. data[1] = m_y;
  125. }
  126. //--------------------------------------------------------------------
  127. void load(const save_data_type* data)
  128. {
  129. m_mod = data[0];
  130. m_y = data[1];
  131. }
  132. //--------------------------------------------------------------------
  133. void operator++()
  134. {
  135. m_mod += m_rem;
  136. m_y += m_lft;
  137. if(m_mod > 0)
  138. {
  139. m_mod -= m_cnt;
  140. m_y++;
  141. }
  142. }
  143. //--------------------------------------------------------------------
  144. void operator--()
  145. {
  146. if(m_mod <= m_rem)
  147. {
  148. m_mod += m_cnt;
  149. m_y--;
  150. }
  151. m_mod -= m_rem;
  152. m_y -= m_lft;
  153. }
  154. //--------------------------------------------------------------------
  155. void adjust_forward()
  156. {
  157. m_mod -= m_cnt;
  158. }
  159. //--------------------------------------------------------------------
  160. void adjust_backward()
  161. {
  162. m_mod += m_cnt;
  163. }
  164. //--------------------------------------------------------------------
  165. int mod() const { return m_mod; }
  166. int rem() const { return m_rem; }
  167. int lft() const { return m_lft; }
  168. //--------------------------------------------------------------------
  169. int y() const { return m_y; }
  170. private:
  171. int m_cnt;
  172. int m_lft;
  173. int m_rem;
  174. int m_mod;
  175. int m_y;
  176. };
  177. //---------------------------------------------line_bresenham_interpolator
  178. class line_bresenham_interpolator
  179. {
  180. public:
  181. enum subpixel_scale_e
  182. {
  183. subpixel_shift = 8,
  184. subpixel_scale = 1 << subpixel_shift,
  185. subpixel_mask = subpixel_scale - 1
  186. };
  187. //--------------------------------------------------------------------
  188. static int line_lr(int v) { return v >> subpixel_shift; }
  189. //--------------------------------------------------------------------
  190. line_bresenham_interpolator(int x1, int y1, int x2, int y2) :
  191. m_x1_lr(line_lr(x1)),
  192. m_y1_lr(line_lr(y1)),
  193. m_x2_lr(line_lr(x2)),
  194. m_y2_lr(line_lr(y2)),
  195. m_ver(abs(m_x2_lr - m_x1_lr) < abs(m_y2_lr - m_y1_lr)),
  196. m_len(m_ver ? abs(m_y2_lr - m_y1_lr) :
  197. abs(m_x2_lr - m_x1_lr)),
  198. m_inc(m_ver ? ((y2 > y1) ? 1 : -1) : ((x2 > x1) ? 1 : -1)),
  199. m_interpolator(m_ver ? x1 : y1,
  200. m_ver ? x2 : y2,
  201. m_len)
  202. {
  203. }
  204. //--------------------------------------------------------------------
  205. bool is_ver() const { return m_ver; }
  206. unsigned len() const { return m_len; }
  207. int inc() const { return m_inc; }
  208. //--------------------------------------------------------------------
  209. void hstep()
  210. {
  211. ++m_interpolator;
  212. m_x1_lr += m_inc;
  213. }
  214. //--------------------------------------------------------------------
  215. void vstep()
  216. {
  217. ++m_interpolator;
  218. m_y1_lr += m_inc;
  219. }
  220. //--------------------------------------------------------------------
  221. int x1() const { return m_x1_lr; }
  222. int y1() const { return m_y1_lr; }
  223. int x2() const { return line_lr(m_interpolator.y()); }
  224. int y2() const { return line_lr(m_interpolator.y()); }
  225. int x2_hr() const { return m_interpolator.y(); }
  226. int y2_hr() const { return m_interpolator.y(); }
  227. private:
  228. int m_x1_lr;
  229. int m_y1_lr;
  230. int m_x2_lr;
  231. int m_y2_lr;
  232. bool m_ver;
  233. unsigned m_len;
  234. int m_inc;
  235. dda2_line_interpolator m_interpolator;
  236. };
  237. }
  238. #endif