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.
 
 
 
 
 

154 lines
3.3 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. static uint8_t clip_bits(int, int);
  27. static void draw_solid_line(int, int, int, int, char);
  28. void ee_draw_line(int x1, int y1, int x2, int y2, char c)
  29. {
  30. uint8_t bits1, bits2;
  31. bits1 = clip_bits(x1, y1);
  32. bits2 = clip_bits(x2, y2);
  33. if(bits1 & bits2)
  34. return;
  35. if(bits1 == 0)
  36. {
  37. if(bits2 == 0)
  38. draw_solid_line(x1, y1, x2, y2, c);
  39. else
  40. ee_draw_line(x2, y2, x1, y1, c);
  41. return;
  42. }
  43. if(bits1 & (1<<0))
  44. {
  45. y1 = y2 - (x2-0) * (y2-y1) / (x2-x1);
  46. x1 = 0;
  47. }
  48. else if( bits1 & (1<<1) )
  49. {
  50. int xmax = ee_get_width() - 1;
  51. y1 = y2 - (x2-xmax) * (y2-y1) / (x2-x1);
  52. x1 = xmax;
  53. }
  54. else if( bits1 & (1<<2) )
  55. {
  56. x1 = x2 - (y2-0) * (x2-x1) / (y2-y1);
  57. y1 = 0;
  58. }
  59. else if( bits1 & (1<<3) )
  60. {
  61. int ymax = ee_get_height() - 1;
  62. x1 = x2 - (y2-ymax) * (x2-x1) / (y2-y1);
  63. y1 = ymax;
  64. }
  65. ee_draw_line(x1, y1, x2, y2, c);
  66. }
  67. static void draw_solid_line(int x1, int y1, int x2, int y2, char c)
  68. {
  69. int dx = abs(x2-x1);
  70. int dy = abs(y2-y1);
  71. int xinc, yinc;
  72. xinc = (x1 > x2) ? -1 : 1;
  73. yinc = (y1 > y2) ? -1 : 1;
  74. if(dx >= dy)
  75. {
  76. int dpr = dy << 1;
  77. int dpru = dpr - (dx << 1);
  78. int delta = dpr - dx;
  79. for(; dx>=0; dx--)
  80. {
  81. ee_goto(x1, y1);
  82. ee_putchar(c);
  83. if(delta > 0)
  84. {
  85. x1 += xinc;
  86. y1 += yinc;
  87. delta += dpru;
  88. }
  89. else
  90. {
  91. x1 += xinc;
  92. delta += dpr;
  93. }
  94. }
  95. }
  96. else
  97. {
  98. int dpr = dx << 1;
  99. int dpru = dpr - (dy << 1);
  100. int delta = dpr - dy;
  101. for(; dy >= 0; dy--)
  102. {
  103. ee_goto(x1, y1);
  104. ee_putchar(c);
  105. if(delta > 0)
  106. {
  107. x1 += xinc;
  108. y1 += yinc;
  109. delta += dpru;
  110. }
  111. else
  112. {
  113. y1 += yinc;
  114. delta += dpr;
  115. }
  116. }
  117. }
  118. }
  119. static uint8_t clip_bits(int x, int y)
  120. {
  121. uint8_t b = 0;
  122. if(x < 0)
  123. b |= (1<<0);
  124. else if(x >= ee_get_width())
  125. b |= (1<<1);
  126. if(y < 0)
  127. b |= (1<<2);
  128. else if(y >= ee_get_height())
  129. b |= (1<<3);
  130. return b;
  131. }