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.
 
 
 
 
 
 

150 lines
4.3 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, xa, xb, xmax, ymax;
  86. uint32_t ch;
  87. /* Bubble-sort y1 <= y2 <= y3 */
  88. if(y1 > y2)
  89. return cucul_fill_triangle(cv, x2, y2, x1, y1, x3, y3, str);
  90. if(y2 > y3)
  91. return cucul_fill_triangle(cv, x1, y1, x3, y3, x2, y2, str);
  92. /* Promote precision */
  93. x1 *= 4;
  94. x2 *= 4;
  95. x3 *= 4;
  96. xmax = cv->width - 1;
  97. ymax = cv->height - 1;
  98. ch = cucul_utf8_to_utf32(str, NULL);
  99. /* Rasterize our triangle */
  100. for(y = y1 < 0 ? 0 : y1; y <= y3 && y <= ymax; y++)
  101. {
  102. if(y <= y2)
  103. {
  104. xa = (y1 == y2) ? x2 : x1 + (x2 - x1) * (y - y1) / (y2 - y1);
  105. xb = (y1 == y3) ? x3 : x1 + (x3 - x1) * (y - y1) / (y3 - y1);
  106. }
  107. else
  108. {
  109. xa = (y3 == y2) ? x2 : x3 + (x2 - x3) * (y - y3) / (y2 - y3);
  110. xb = (y3 == y1) ? x1 : x3 + (x1 - x3) * (y - y3) / (y1 - y3);
  111. }
  112. if(xb < xa)
  113. {
  114. int tmp = xb;
  115. xb = xa; xa = tmp;
  116. }
  117. /* Rescale xa and xb, slightly cropping */
  118. xa = (xa + 2) / 4;
  119. xb = (xb - 2) / 4;
  120. if(xb < 0) continue;
  121. if(xa > xmax) continue;
  122. if(xa < 0) xa = 0;
  123. if(xb > xmax) xb = xmax;
  124. for(x = xa; x <= xb; x++)
  125. cucul_putchar(cv, x, y, ch);
  126. }
  127. return 0;
  128. }