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.
 
 
 
 
 
 

144 lines
4.1 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. * \param cv The handle to the libcucul canvas.
  26. * \param x1 X coordinate of the first point.
  27. * \param y1 Y coordinate of the first point.
  28. * \param x2 X coordinate of the second point.
  29. * \param y2 Y coordinate of the second point.
  30. * \param x3 X coordinate of the third point.
  31. * \param y3 Y coordinate of the third point.
  32. * \param str UTF-8 string representing the character that should be used
  33. * to draw the triangle outline.
  34. * \return void
  35. */
  36. void cucul_draw_triangle(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
  37. int x3, int y3, char const *str)
  38. {
  39. cucul_draw_line(cv, x1, y1, x2, y2, str);
  40. cucul_draw_line(cv, x2, y2, x3, y3, str);
  41. cucul_draw_line(cv, x3, y3, x1, y1, str);
  42. }
  43. /** \brief Draw a thin triangle on the canvas.
  44. *
  45. * \param cv The handle to the libcucul canvas.
  46. * \param x1 X coordinate of the first point.
  47. * \param y1 Y coordinate of the first point.
  48. * \param x2 X coordinate of the second point.
  49. * \param y2 Y coordinate of the second point.
  50. * \param x3 X coordinate of the third point.
  51. * \param y3 Y coordinate of the third point.
  52. * \return void
  53. */
  54. void cucul_draw_thin_triangle(cucul_canvas_t *cv, int x1, int y1,
  55. int x2, int y2, int x3, int y3)
  56. {
  57. cucul_draw_thin_line(cv, x1, y1, x2, y2);
  58. cucul_draw_thin_line(cv, x2, y2, x3, y3);
  59. cucul_draw_thin_line(cv, x3, y3, x1, y1);
  60. }
  61. /** \brief Fill a triangle on the canvas using the given character.
  62. *
  63. * \param cv The handle to the libcucul canvas.
  64. * \param x1 X coordinate of the first point.
  65. * \param y1 Y coordinate of the first point.
  66. * \param x2 X coordinate of the second point.
  67. * \param y2 Y coordinate of the second point.
  68. * \param x3 X coordinate of the third point.
  69. * \param y3 Y coordinate of the third point.
  70. * \param str UTF-8 string representing the character that should be used
  71. * to fill the triangle.
  72. * \return void
  73. */
  74. void cucul_fill_triangle(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
  75. int x3, int y3, char const *str)
  76. {
  77. int x, y, xa, xb, xmax, ymax;
  78. uint32_t ch;
  79. /* Bubble-sort y1 <= y2 <= y3 */
  80. if(y1 > y2)
  81. {
  82. cucul_fill_triangle(cv, x2, y2, x1, y1, x3, y3, str);
  83. return;
  84. }
  85. if(y2 > y3)
  86. {
  87. cucul_fill_triangle(cv, x1, y1, x3, y3, x2, y2, str);
  88. return;
  89. }
  90. /* Promote precision */
  91. x1 *= 4;
  92. x2 *= 4;
  93. x3 *= 4;
  94. xmax = cv->width - 1;
  95. ymax = cv->height - 1;
  96. ch = _cucul_utf8_to_utf32(str);
  97. /* Rasterize our triangle */
  98. for(y = y1 < 0 ? 0 : y1; y <= y3 && y <= ymax; y++)
  99. {
  100. if(y <= y2)
  101. {
  102. xa = (y1 == y2) ? x2 : x1 + (x2 - x1) * (y - y1) / (y2 - y1);
  103. xb = (y1 == y3) ? x3 : x1 + (x3 - x1) * (y - y1) / (y3 - y1);
  104. }
  105. else
  106. {
  107. xa = (y3 == y2) ? x2 : x3 + (x2 - x3) * (y - y3) / (y2 - y3);
  108. xb = (y3 == y1) ? x1 : x3 + (x1 - x3) * (y - y3) / (y1 - y3);
  109. }
  110. if(xb < xa)
  111. {
  112. int tmp = xb;
  113. xb = xa; xa = tmp;
  114. }
  115. /* Rescale xa and xb, slightly cropping */
  116. xa = (xa + 2) / 4;
  117. xb = (xb - 2) / 4;
  118. if(xb < 0) continue;
  119. if(xa > xmax) continue;
  120. if(xa < 0) xa = 0;
  121. if(xb > xmax) xb = xmax;
  122. for(x = xa; x <= xb; x++)
  123. _cucul_putchar32(cv, x, y, ch);
  124. }
  125. }