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.
 
 
 
 
 
 

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