Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

163 Zeilen
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; 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 str UTF-8 string representing the character that should be used
  35. * 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, char const *str)
  40. {
  41. cucul_draw_line(cv, x1, y1, x2, y2, str);
  42. cucul_draw_line(cv, x2, y2, x3, y3, str);
  43. cucul_draw_line(cv, x3, y3, x1, y1, str);
  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 str UTF-8 string representing the character that should be used
  79. * to fill the triangle.
  80. * \return This function always returns 0.
  81. */
  82. int cucul_fill_triangle(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
  83. int x3, int y3, char const *str)
  84. {
  85. int x, y, xmin, xmax, ymin, ymax;
  86. long int xx1, xx2, xa, xb, sl21, sl31, sl32;
  87. uint32_t ch;
  88. /* Bubble-sort y1 <= y2 <= y3 */
  89. if(y1 > y2)
  90. return cucul_fill_triangle(cv, x2, y2, x1, y1, x3, y3, str);
  91. if(y2 > y3)
  92. return cucul_fill_triangle(cv, x1, y1, x3, y3, x2, y2, str);
  93. ch = cucul_utf8_to_utf32(str, NULL);
  94. /* Compute slopes and promote precision */
  95. sl21 = (y2 == y1) ? 0 : (x2 - x1) * 0x10000 / (y2 - y1);
  96. sl31 = (y3 == y1) ? 0 : (x3 - x1) * 0x10000 / (y3 - y1);
  97. sl32 = (y3 == y2) ? 0 : (x3 - x2) * 0x10000 / (y3 - y2);
  98. x1 *= 0x10000;
  99. x2 *= 0x10000;
  100. x3 *= 0x10000;
  101. ymin = y1 < 0 ? 0 : y1;
  102. ymax = y3 + 1 < (int)cv->height ? y3 + 1 : (int)cv->height;
  103. if(ymin < y2)
  104. {
  105. xa = x1 + sl21 * (ymin - y1);
  106. xb = x1 + sl31 * (ymin - y1);
  107. }
  108. else if(ymin == y2)
  109. {
  110. xa = x2;
  111. xb = (y1 == y3) ? x3 : x1 + sl31 * (ymin - y1);
  112. }
  113. else /* (ymin > y2) */
  114. {
  115. xa = x3 + sl32 * (ymin - y3);
  116. xb = x3 + sl31 * (ymin - y3);
  117. }
  118. /* Rasterize our triangle */
  119. for(y = ymin; y < ymax; y++)
  120. {
  121. /* Rescale xa and xb, recentering the division */
  122. if(xa < xb)
  123. {
  124. xx1 = (xa + 0x800) / 0x10000;
  125. xx2 = (xb + 0x801) / 0x10000;
  126. }
  127. else
  128. {
  129. xx1 = (xb + 0x800) / 0x10000;
  130. xx2 = (xa + 0x801) / 0x10000;
  131. }
  132. xmin = xx1 < 0 ? 0 : xx1;
  133. xmax = xx2 + 1 < (int)cv->width ? xx2 + 1 : (int)cv->width;
  134. for(x = xmin; x < xmax; x++)
  135. cucul_putchar(cv, x, y, ch);
  136. xa += y < y2 ? sl21 : sl32;
  137. xb += sl31;
  138. }
  139. return 0;
  140. }