Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains triangle drawing functions, both filled and outline.
  15. */
  16. #include "config.h"
  17. #include "common.h"
  18. #if !defined(__KERNEL__)
  19. # include <stdlib.h>
  20. #endif
  21. #include "cucul.h"
  22. #include "cucul_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 libcucul 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 cucul_draw_triangle(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
  38. int x3, int y3, unsigned long int ch)
  39. {
  40. cucul_draw_line(cv, x1, y1, x2, y2, ch);
  41. cucul_draw_line(cv, x2, y2, x3, y3, ch);
  42. cucul_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 libcucul 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 cucul_draw_thin_triangle(cucul_canvas_t *cv, int x1, int y1,
  59. int x2, int y2, int x3, int y3)
  60. {
  61. cucul_draw_thin_line(cv, x1, y1, x2, y2);
  62. cucul_draw_thin_line(cv, x2, y2, x3, y3);
  63. cucul_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 libcucul 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 cucul_fill_triangle(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
  81. int x3, int y3, unsigned long int ch)
  82. {
  83. int x, y, xmin, xmax, ymin, ymax;
  84. long int xx1, xx2, xa, xb, sl21, sl31, sl32;
  85. /* Bubble-sort y1 <= y2 <= y3 */
  86. if(y1 > y2)
  87. return cucul_fill_triangle(cv, x2, y2, x1, y1, x3, y3, ch);
  88. if(y2 > y3)
  89. return cucul_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 < (int)cv->height ? y3 + 1 : (int)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 < (int)cv->width ? xx2 + 1 : (int)cv->width;
  130. for(x = xmin; x < xmax; x++)
  131. cucul_put_char(cv, x, y, ch);
  132. xa += y < y2 ? sl21 : sl32;
  133. xb += sl31;
  134. }
  135. return 0;
  136. }