Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

169 řádky
4.9 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains triangle drawing functions, both filled and outline.
  16. */
  17. #include "config.h"
  18. #if !defined(__KERNEL__)
  19. # include <stdlib.h>
  20. #endif
  21. #include "caca.h"
  22. #include "caca_internals.h"
  23. /** \brief Draw a triangle on the canvas using the given character.
  24. *
  25. * This function never fails.
  26. *
  27. * \param cv The handle to the libcaca canvas.
  28. * \param x1 X coordinate of the first point.
  29. * \param y1 Y coordinate of the first point.
  30. * \param x2 X coordinate of the second point.
  31. * \param y2 Y coordinate of the second point.
  32. * \param x3 X coordinate of the third point.
  33. * \param y3 Y coordinate of the third point.
  34. * \param ch UTF-32 character to be used to draw the triangle outline.
  35. * \return This function always returns 0.
  36. */
  37. int caca_draw_triangle(caca_canvas_t *cv, int x1, int y1, int x2, int y2,
  38. int x3, int y3, uint32_t ch)
  39. {
  40. caca_draw_line(cv, x1, y1, x2, y2, ch);
  41. caca_draw_line(cv, x2, y2, x3, y3, ch);
  42. caca_draw_line(cv, x3, y3, x1, y1, ch);
  43. return 0;
  44. }
  45. /** \brief Draw a thin triangle on the canvas.
  46. *
  47. * This function never fails.
  48. *
  49. * \param cv The handle to the libcaca canvas.
  50. * \param x1 X coordinate of the first point.
  51. * \param y1 Y coordinate of the first point.
  52. * \param x2 X coordinate of the second point.
  53. * \param y2 Y coordinate of the second point.
  54. * \param x3 X coordinate of the third point.
  55. * \param y3 Y coordinate of the third point.
  56. * \return This function always returns 0.
  57. */
  58. int caca_draw_thin_triangle(caca_canvas_t *cv, int x1, int y1,
  59. int x2, int y2, int x3, int y3)
  60. {
  61. caca_draw_thin_line(cv, x1, y1, x2, y2);
  62. caca_draw_thin_line(cv, x2, y2, x3, y3);
  63. caca_draw_thin_line(cv, x3, y3, x1, y1);
  64. return 0;
  65. }
  66. /** \brief Fill a triangle on the canvas using the given character.
  67. *
  68. * This function never fails.
  69. *
  70. * \param cv The handle to the libcaca canvas.
  71. * \param x1 X coordinate of the first point.
  72. * \param y1 Y coordinate of the first point.
  73. * \param x2 X coordinate of the second point.
  74. * \param y2 Y coordinate of the second point.
  75. * \param x3 X coordinate of the third point.
  76. * \param y3 Y coordinate of the third point.
  77. * \param ch UTF-32 character to be used to fill the triangle.
  78. * \return This function always returns 0.
  79. */
  80. int caca_fill_triangle(caca_canvas_t *cv, int x1, int y1, int x2, int y2,
  81. int x3, int y3, uint32_t ch)
  82. {
  83. int x, y, xmin, xmax, ymin, ymax;
  84. int xx1, xx2, xa, xb, sl21, sl31, sl32;
  85. /* Bubble-sort y1 <= y2 <= y3 */
  86. if(y1 > y2)
  87. return caca_fill_triangle(cv, x2, y2, x1, y1, x3, y3, ch);
  88. if(y2 > y3)
  89. return caca_fill_triangle(cv, x1, y1, x3, y3, x2, y2, ch);
  90. /* Compute slopes and promote precision */
  91. sl21 = (y2 == y1) ? 0 : (x2 - x1) * 0x10000 / (y2 - y1);
  92. sl31 = (y3 == y1) ? 0 : (x3 - x1) * 0x10000 / (y3 - y1);
  93. sl32 = (y3 == y2) ? 0 : (x3 - x2) * 0x10000 / (y3 - y2);
  94. x1 *= 0x10000;
  95. x2 *= 0x10000;
  96. x3 *= 0x10000;
  97. ymin = y1 < 0 ? 0 : y1;
  98. ymax = y3 + 1 < cv->height ? y3 + 1 : cv->height;
  99. if(ymin < y2)
  100. {
  101. xa = x1 + sl21 * (ymin - y1);
  102. xb = x1 + sl31 * (ymin - y1);
  103. }
  104. else if(ymin == y2)
  105. {
  106. xa = x2;
  107. xb = (y1 == y3) ? x3 : x1 + sl31 * (ymin - y1);
  108. }
  109. else /* (ymin > y2) */
  110. {
  111. xa = x3 + sl32 * (ymin - y3);
  112. xb = x3 + sl31 * (ymin - y3);
  113. }
  114. /* Rasterize our triangle */
  115. for(y = ymin; y < ymax; y++)
  116. {
  117. /* Rescale xa and xb, recentering the division */
  118. if(xa < xb)
  119. {
  120. xx1 = (xa + 0x800) / 0x10000;
  121. xx2 = (xb + 0x801) / 0x10000;
  122. }
  123. else
  124. {
  125. xx1 = (xb + 0x800) / 0x10000;
  126. xx2 = (xa + 0x801) / 0x10000;
  127. }
  128. xmin = xx1 < 0 ? 0 : xx1;
  129. xmax = xx2 + 1 < cv->width ? xx2 + 1 : cv->width;
  130. for(x = xmin; x < xmax; x++)
  131. caca_put_char(cv, x, y, ch);
  132. xa += y < y2 ? sl21 : sl32;
  133. xb += sl31;
  134. }
  135. return 0;
  136. }
  137. /*
  138. * XXX: The following functions are aliases.
  139. */
  140. int cucul_draw_triangle(cucul_canvas_t *, int, int, int, int, int,
  141. int, uint32_t) CACA_ALIAS(caca_draw_triangle);
  142. int cucul_draw_thin_triangle(cucul_canvas_t *, int, int, int, int,
  143. int, int) CACA_ALIAS(caca_draw_thin_triangle);
  144. int cucul_fill_triangle(cucul_canvas_t *, int, int, int, int, int,
  145. int, uint32_t) CACA_ALIAS(caca_fill_triangle);