You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

185 lines
3.8 KiB

  1. /*
  2. * libee ASCII-Art library
  3. * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  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.
  12. *
  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.
  17. *
  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.
  21. */
  22. #include "config.h"
  23. #include <inttypes.h>
  24. #include <stdlib.h>
  25. #include "ee.h"
  26. struct line
  27. {
  28. int x1, y1;
  29. int x2, y2;
  30. char c;
  31. void (*draw) (struct line*);
  32. };
  33. static void clip_line(struct line*);
  34. static uint8_t clip_bits(int, int);
  35. static void draw_solid_line(struct line*);
  36. void ee_draw_line(int x1, int y1, int x2, int y2, char c)
  37. {
  38. struct line s;
  39. s.x1 = x1;
  40. s.y1 = y1;
  41. s.x2 = x2;
  42. s.y2 = y2;
  43. s.c = c;
  44. s.draw = draw_solid_line;
  45. clip_line(&s);
  46. }
  47. static void clip_line(struct line* s)
  48. {
  49. uint8_t bits1, bits2;
  50. bits1 = clip_bits(s->x1, s->y1);
  51. bits2 = clip_bits(s->x2, s->y2);
  52. if(bits1 & bits2)
  53. return;
  54. if(bits1 == 0)
  55. {
  56. if(bits2 == 0)
  57. s->draw(s);
  58. else
  59. {
  60. int tmp;
  61. tmp = s->x1; s->x1 = s->x2; s->x2 = tmp;
  62. tmp = s->y1; s->y1 = s->y2; s->y2 = tmp;
  63. clip_line(s);
  64. }
  65. return;
  66. }
  67. if(bits1 & (1<<0))
  68. {
  69. s->y1 = s->y2 - (s->x2 - 0) * (s->y2 - s->y1) / (s->x2 - s->x1);
  70. s->x1 = 0;
  71. }
  72. else if( bits1 & (1<<1) )
  73. {
  74. int xmax = ee_get_width() - 1;
  75. s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1);
  76. s->x1 = xmax;
  77. }
  78. else if( bits1 & (1<<2) )
  79. {
  80. s->x1 = s->x2 - (s->y2 - 0) * (s->x2 - s->x1) / (s->y2 - s->y1);
  81. s->y1 = 0;
  82. }
  83. else if( bits1 & (1<<3) )
  84. {
  85. int ymax = ee_get_height() - 1;
  86. s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1);
  87. s->y1 = ymax;
  88. }
  89. clip_line(s);
  90. }
  91. static uint8_t clip_bits(int x, int y)
  92. {
  93. uint8_t b = 0;
  94. if(x < 0)
  95. b |= (1<<0);
  96. else if(x >= ee_get_width())
  97. b |= (1<<1);
  98. if(y < 0)
  99. b |= (1<<2);
  100. else if(y >= ee_get_height())
  101. b |= (1<<3);
  102. return b;
  103. }
  104. static void draw_solid_line(struct line* s)
  105. {
  106. int x1 = s->x1;
  107. int y1 = s->y1;
  108. int x2 = s->x2;
  109. int y2 = s->y2;
  110. int dx = abs(x2-x1);
  111. int dy = abs(y2-y1);
  112. int xinc, yinc;
  113. xinc = (x1 > x2) ? -1 : 1;
  114. yinc = (y1 > y2) ? -1 : 1;
  115. if(dx >= dy)
  116. {
  117. int dpr = dy << 1;
  118. int dpru = dpr - (dx << 1);
  119. int delta = dpr - dx;
  120. for(; dx>=0; dx--)
  121. {
  122. ee_goto(x1, y1);
  123. ee_putchar(s->c);
  124. if(delta > 0)
  125. {
  126. x1 += xinc;
  127. y1 += yinc;
  128. delta += dpru;
  129. }
  130. else
  131. {
  132. x1 += xinc;
  133. delta += dpr;
  134. }
  135. }
  136. }
  137. else
  138. {
  139. int dpr = dx << 1;
  140. int dpru = dpr - (dy << 1);
  141. int delta = dpr - dy;
  142. for(; dy >= 0; dy--)
  143. {
  144. ee_goto(x1, y1);
  145. ee_putchar(s->c);
  146. if(delta > 0)
  147. {
  148. x1 += xinc;
  149. y1 += yinc;
  150. delta += dpru;
  151. }
  152. else
  153. {
  154. y1 += yinc;
  155. delta += dpr;
  156. }
  157. }
  158. }
  159. }