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
3.9 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. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Do What The Fuck You Want To
  8. * Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. /** \file triangle.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \brief Triangle drawing
  15. *
  16. * This file contains triangle drawing functions, both filled and outline.
  17. */
  18. #include "config.h"
  19. #if !defined(__KERNEL__)
  20. # include <stdlib.h>
  21. #endif
  22. #include "cucul.h"
  23. #include "cucul_internals.h"
  24. /**
  25. * \brief Draw a triangle on the screen using the given character.
  26. *
  27. * \param x1 X coordinate of the first point.
  28. * \param y1 Y coordinate of the first point.
  29. * \param x2 X coordinate of the second point.
  30. * \param y2 Y coordinate of the second point.
  31. * \param x3 X coordinate of the third point.
  32. * \param y3 Y coordinate of the third point.
  33. * \param c Character to draw the triangle outline with.
  34. * \return void
  35. */
  36. void cucul_draw_triangle(cucul_t *qq, int x1, int y1, int x2, int y2,
  37. int x3, int y3, char const *str)
  38. {
  39. cucul_draw_line(qq, x1, y1, x2, y2, str);
  40. cucul_draw_line(qq, x2, y2, x3, y3, str);
  41. cucul_draw_line(qq, x3, y3, x1, y1, str);
  42. }
  43. /**
  44. * \brief Draw a thin triangle on the screen.
  45. *
  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_t *qq, int x1, int y1, int x2, int y2,
  55. int x3, int y3)
  56. {
  57. cucul_draw_thin_line(qq, x1, y1, x2, y2);
  58. cucul_draw_thin_line(qq, x2, y2, x3, y3);
  59. cucul_draw_thin_line(qq, x3, y3, x1, y1);
  60. }
  61. /**
  62. * \brief Fill a triangle on the screen using the given character.
  63. *
  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 c Character to fill the triangle with.
  71. * \return void
  72. */
  73. void cucul_fill_triangle(cucul_t *qq, 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 c;
  78. /* Bubble-sort y1 <= y2 <= y3 */
  79. if(y1 > y2)
  80. {
  81. cucul_fill_triangle(qq, x2, y2, x1, y1, x3, y3, str);
  82. return;
  83. }
  84. if(y2 > y3)
  85. {
  86. cucul_fill_triangle(qq, x1, y1, x3, y3, x2, y2, str);
  87. return;
  88. }
  89. /* Promote precision */
  90. x1 *= 4;
  91. x2 *= 4;
  92. x3 *= 4;
  93. xmax = qq->width - 1;
  94. ymax = qq->height - 1;
  95. c = _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(qq, x, y, c);
  123. }
  124. }