Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * libcaca ASCII-Art library
  3. * Copyright (c) 2002, 2003 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 GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  19. * 02111-1307 USA
  20. */
  21. /** \file triangle.c
  22. * \version \$Id$
  23. * \author Sam Hocevar <sam@zoy.org>
  24. * \brief Triangle drawing
  25. *
  26. * This file contains triangle drawing functions, both filled and outline.
  27. */
  28. #include "config.h"
  29. #include <stdlib.h>
  30. #include "caca.h"
  31. #include "caca_internals.h"
  32. /**
  33. * \brief Draw a triangle on the screen using the given character.
  34. *
  35. * \param x1 X coordinate of the first point.
  36. * \param y1 Y coordinate of the first point.
  37. * \param x2 X coordinate of the second point.
  38. * \param y2 Y coordinate of the second point.
  39. * \param x3 X coordinate of the third point.
  40. * \param y3 Y coordinate of the third point.
  41. * \param c Character to draw the triangle outline with.
  42. * \return void
  43. */
  44. void caca_draw_triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c)
  45. {
  46. caca_draw_line(x1, y1, x2, y2, c);
  47. caca_draw_line(x2, y2, x3, y3, c);
  48. caca_draw_line(x3, y3, x1, y1, c);
  49. }
  50. /**
  51. * \brief Draw a thin triangle on the screen.
  52. *
  53. * \param x1 X coordinate of the first point.
  54. * \param y1 Y coordinate of the first point.
  55. * \param x2 X coordinate of the second point.
  56. * \param y2 Y coordinate of the second point.
  57. * \param x3 X coordinate of the third point.
  58. * \param y3 Y coordinate of the third point.
  59. * \return void
  60. */
  61. void caca_draw_thin_triangle(int x1, int y1, int x2, int y2, int x3, int y3)
  62. {
  63. caca_draw_thin_line(x1, y1, x2, y2);
  64. caca_draw_thin_line(x2, y2, x3, y3);
  65. caca_draw_thin_line(x3, y3, x1, y1);
  66. }
  67. /**
  68. * \brief Fill a triangle on the screen using the given character.
  69. *
  70. * \param x1 X coordinate of the first point.
  71. * \param y1 Y coordinate of the first point.
  72. * \param x2 X coordinate of the second point.
  73. * \param y2 Y coordinate of the second point.
  74. * \param x3 X coordinate of the third point.
  75. * \param y3 Y coordinate of the third point.
  76. * \param c Character to fill the triangle with.
  77. * \return void
  78. */
  79. void caca_fill_triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c)
  80. {
  81. int x, y, xa, xb, xmax, ymax;
  82. /* Bubble-sort y1 <= y2 <= y3 */
  83. if(y1 > y2)
  84. {
  85. caca_fill_triangle(x2, y2, x1, y1, x3, y3, c);
  86. return;
  87. }
  88. if(y2 > y3)
  89. {
  90. caca_fill_triangle(x1, y1, x3, y3, x2, y2, c);
  91. return;
  92. }
  93. /* Promote precision */
  94. x1 *= 4;
  95. x2 *= 4;
  96. x3 *= 4;
  97. xmax = _caca_width - 1;
  98. ymax = _caca_height - 1;
  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. caca_putchar(x, y, c);
  126. }
  127. }