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

159 行
4.6 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  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. #include "common.h"
  19. #if !defined(__KERNEL__)
  20. # include <stdlib.h>
  21. #endif
  22. #include "cucul.h"
  23. #include "cucul_internals.h"
  24. /** \brief Draw a triangle on the canvas using the given character.
  25. *
  26. * This function never fails.
  27. *
  28. * \param cv The handle to the libcucul canvas.
  29. * \param x1 X coordinate of the first point.
  30. * \param y1 Y coordinate of the first point.
  31. * \param x2 X coordinate of the second point.
  32. * \param y2 Y coordinate of the second point.
  33. * \param x3 X coordinate of the third point.
  34. * \param y3 Y coordinate of the third point.
  35. * \param ch UTF-32 character to be used to draw the triangle outline.
  36. * \return This function always returns 0.
  37. */
  38. int cucul_draw_triangle(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
  39. int x3, int y3, unsigned long int ch)
  40. {
  41. cucul_draw_line(cv, x1, y1, x2, y2, ch);
  42. cucul_draw_line(cv, x2, y2, x3, y3, ch);
  43. cucul_draw_line(cv, x3, y3, x1, y1, ch);
  44. return 0;
  45. }
  46. /** \brief Draw a thin triangle on the canvas.
  47. *
  48. * This function never fails.
  49. *
  50. * \param cv The handle to the libcucul canvas.
  51. * \param x1 X coordinate of the first point.
  52. * \param y1 Y coordinate of the first point.
  53. * \param x2 X coordinate of the second point.
  54. * \param y2 Y coordinate of the second point.
  55. * \param x3 X coordinate of the third point.
  56. * \param y3 Y coordinate of the third point.
  57. * \return This function always returns 0.
  58. */
  59. int cucul_draw_thin_triangle(cucul_canvas_t *cv, int x1, int y1,
  60. int x2, int y2, int x3, int y3)
  61. {
  62. cucul_draw_thin_line(cv, x1, y1, x2, y2);
  63. cucul_draw_thin_line(cv, x2, y2, x3, y3);
  64. cucul_draw_thin_line(cv, x3, y3, x1, y1);
  65. return 0;
  66. }
  67. /** \brief Fill a triangle on the canvas using the given character.
  68. *
  69. * This function never fails.
  70. *
  71. * \param cv The handle to the libcucul canvas.
  72. * \param x1 X coordinate of the first point.
  73. * \param y1 Y coordinate of the first point.
  74. * \param x2 X coordinate of the second point.
  75. * \param y2 Y coordinate of the second point.
  76. * \param x3 X coordinate of the third point.
  77. * \param y3 Y coordinate of the third point.
  78. * \param ch UTF-32 character to be used to fill the triangle.
  79. * \return This function always returns 0.
  80. */
  81. int cucul_fill_triangle(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
  82. int x3, int y3, unsigned long int ch)
  83. {
  84. int x, y, xmin, xmax, ymin, ymax;
  85. long int xx1, xx2, xa, xb, sl21, sl31, sl32;
  86. /* Bubble-sort y1 <= y2 <= y3 */
  87. if(y1 > y2)
  88. return cucul_fill_triangle(cv, x2, y2, x1, y1, x3, y3, ch);
  89. if(y2 > y3)
  90. return cucul_fill_triangle(cv, x1, y1, x3, y3, x2, y2, ch);
  91. /* Compute slopes and promote precision */
  92. sl21 = (y2 == y1) ? 0 : (x2 - x1) * 0x10000 / (y2 - y1);
  93. sl31 = (y3 == y1) ? 0 : (x3 - x1) * 0x10000 / (y3 - y1);
  94. sl32 = (y3 == y2) ? 0 : (x3 - x2) * 0x10000 / (y3 - y2);
  95. x1 *= 0x10000;
  96. x2 *= 0x10000;
  97. x3 *= 0x10000;
  98. ymin = y1 < 0 ? 0 : y1;
  99. ymax = y3 + 1 < (int)cv->height ? y3 + 1 : (int)cv->height;
  100. if(ymin < y2)
  101. {
  102. xa = x1 + sl21 * (ymin - y1);
  103. xb = x1 + sl31 * (ymin - y1);
  104. }
  105. else if(ymin == y2)
  106. {
  107. xa = x2;
  108. xb = (y1 == y3) ? x3 : x1 + sl31 * (ymin - y1);
  109. }
  110. else /* (ymin > y2) */
  111. {
  112. xa = x3 + sl32 * (ymin - y3);
  113. xb = x3 + sl31 * (ymin - y3);
  114. }
  115. /* Rasterize our triangle */
  116. for(y = ymin; y < ymax; y++)
  117. {
  118. /* Rescale xa and xb, recentering the division */
  119. if(xa < xb)
  120. {
  121. xx1 = (xa + 0x800) / 0x10000;
  122. xx2 = (xb + 0x801) / 0x10000;
  123. }
  124. else
  125. {
  126. xx1 = (xb + 0x800) / 0x10000;
  127. xx2 = (xa + 0x801) / 0x10000;
  128. }
  129. xmin = xx1 < 0 ? 0 : xx1;
  130. xmax = xx2 + 1 < (int)cv->width ? xx2 + 1 : (int)cv->width;
  131. for(x = xmin; x < xmax; x++)
  132. cucul_put_char(cv, x, y, ch);
  133. xa += y < y2 ? sl21 : sl32;
  134. xb += sl31;
  135. }
  136. return 0;
  137. }