Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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