選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

291 行
5.9 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. static void draw_thin_line(struct line*);
  37. void ee_draw_line(int x1, int y1, int x2, int y2, char c)
  38. {
  39. struct line s;
  40. s.x1 = x1;
  41. s.y1 = y1;
  42. s.x2 = x2;
  43. s.y2 = y2;
  44. s.c = c;
  45. s.draw = draw_solid_line;
  46. clip_line(&s);
  47. }
  48. void ee_draw_thin_line(int x1, int y1, int x2, int y2)
  49. {
  50. struct line s;
  51. s.x1 = x1;
  52. s.y1 = y1;
  53. s.x2 = x2;
  54. s.y2 = y2;
  55. s.draw = draw_thin_line;
  56. clip_line(&s);
  57. }
  58. static void clip_line(struct line* s)
  59. {
  60. uint8_t bits1, bits2;
  61. bits1 = clip_bits(s->x1, s->y1);
  62. bits2 = clip_bits(s->x2, s->y2);
  63. if(bits1 & bits2)
  64. return;
  65. if(bits1 == 0)
  66. {
  67. if(bits2 == 0)
  68. s->draw(s);
  69. else
  70. {
  71. int tmp;
  72. tmp = s->x1; s->x1 = s->x2; s->x2 = tmp;
  73. tmp = s->y1; s->y1 = s->y2; s->y2 = tmp;
  74. clip_line(s);
  75. }
  76. return;
  77. }
  78. if(bits1 & (1<<0))
  79. {
  80. s->y1 = s->y2 - (s->x2 - 0) * (s->y2 - s->y1) / (s->x2 - s->x1);
  81. s->x1 = 0;
  82. }
  83. else if( bits1 & (1<<1) )
  84. {
  85. int xmax = ee_get_width() - 1;
  86. s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1);
  87. s->x1 = xmax;
  88. }
  89. else if( bits1 & (1<<2) )
  90. {
  91. s->x1 = s->x2 - (s->y2 - 0) * (s->x2 - s->x1) / (s->y2 - s->y1);
  92. s->y1 = 0;
  93. }
  94. else if( bits1 & (1<<3) )
  95. {
  96. int ymax = ee_get_height() - 1;
  97. s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1);
  98. s->y1 = ymax;
  99. }
  100. clip_line(s);
  101. }
  102. static uint8_t clip_bits(int x, int y)
  103. {
  104. uint8_t b = 0;
  105. if(x < 0)
  106. b |= (1<<0);
  107. else if(x >= ee_get_width())
  108. b |= (1<<1);
  109. if(y < 0)
  110. b |= (1<<2);
  111. else if(y >= ee_get_height())
  112. b |= (1<<3);
  113. return b;
  114. }
  115. static void draw_solid_line(struct line* s)
  116. {
  117. int x1, y1, x2, y2;
  118. int dx, dy;
  119. int xinc, yinc;
  120. x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
  121. dx = abs(x2 - x1);
  122. dy = abs(y2 - y1);
  123. xinc = (x1 > x2) ? -1 : 1;
  124. yinc = (y1 > y2) ? -1 : 1;
  125. if(dx >= dy)
  126. {
  127. int dpr = dy << 1;
  128. int dpru = dpr - (dx << 1);
  129. int delta = dpr - dx;
  130. for(; dx>=0; dx--)
  131. {
  132. ee_goto(x1, y1);
  133. ee_putchar(s->c);
  134. if(delta > 0)
  135. {
  136. x1 += xinc;
  137. y1 += yinc;
  138. delta += dpru;
  139. }
  140. else
  141. {
  142. x1 += xinc;
  143. delta += dpr;
  144. }
  145. }
  146. }
  147. else
  148. {
  149. int dpr = dx << 1;
  150. int dpru = dpr - (dy << 1);
  151. int delta = dpr - dy;
  152. for(; dy >= 0; dy--)
  153. {
  154. ee_goto(x1, y1);
  155. ee_putchar(s->c);
  156. if(delta > 0)
  157. {
  158. x1 += xinc;
  159. y1 += yinc;
  160. delta += dpru;
  161. }
  162. else
  163. {
  164. y1 += yinc;
  165. delta += dpr;
  166. }
  167. }
  168. }
  169. }
  170. static void draw_thin_line(struct line* s)
  171. {
  172. char *charmapx, *charmapy;
  173. int x1, y1, x2, y2;
  174. int dx, dy;
  175. int yinc;
  176. if(s->x2 >= s->x1)
  177. {
  178. if(s->y1 > s->y2)
  179. charmapx = ",'";
  180. else
  181. charmapx = "`.";
  182. x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
  183. }
  184. else
  185. {
  186. if(s->y1 > s->y2)
  187. charmapx = "`.";
  188. else
  189. charmapx = ",'";
  190. x2 = s->x1; y2 = s->y1; x1 = s->x2; y1 = s->y2;
  191. }
  192. dx = abs(x2 - x1);
  193. dy = abs(y2 - y1);
  194. if(y1 > y2)
  195. {
  196. charmapy = ",'";
  197. yinc = -1;
  198. }
  199. else
  200. {
  201. yinc = 1;
  202. charmapy = "`.";
  203. }
  204. if(dx >= dy)
  205. {
  206. int dpr = dy << 1;
  207. int dpru = dpr - (dx << 1);
  208. int delta = dpr - dx;
  209. int prev = 0;
  210. for(; dx>=0; dx--)
  211. {
  212. ee_goto(x1, y1);
  213. if(delta > 0)
  214. {
  215. ee_putchar(charmapy[1]);
  216. x1++;
  217. y1 += yinc;
  218. delta += dpru;
  219. prev = 1;
  220. }
  221. else
  222. {
  223. if(prev)
  224. ee_putchar(charmapy[0]);
  225. else
  226. ee_putchar('-');
  227. x1++;
  228. delta += dpr;
  229. prev = 0;
  230. }
  231. }
  232. }
  233. else
  234. {
  235. int dpr = dx << 1;
  236. int dpru = dpr - (dy << 1);
  237. int delta = dpr - dy;
  238. for(; dy >= 0; dy--)
  239. {
  240. ee_goto(x1, y1);
  241. if(delta > 0)
  242. {
  243. ee_putchar(charmapx[0]);
  244. ee_putchar(charmapx[1]);
  245. x1++;
  246. y1 += yinc;
  247. delta += dpru;
  248. }
  249. else
  250. {
  251. ee_putchar('|');
  252. y1 += yinc;
  253. delta += dpr;
  254. }
  255. }
  256. }
  257. }