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.
 
 
 
 
 
 

137 lines
3.7 KiB

  1. /*
  2. * libcucul Unicode canvas library
  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, int x3, int y3, char c)
  37. {
  38. cucul_draw_line(qq, x1, y1, x2, y2, c);
  39. cucul_draw_line(qq, x2, y2, x3, y3, c);
  40. cucul_draw_line(qq, x3, y3, x1, y1, c);
  41. }
  42. /**
  43. * \brief Draw a thin triangle on the screen.
  44. *
  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_t *qq, int x1, int y1, int x2, int y2, 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, int x3, int y3, char c)
  72. {
  73. int x, y, xa, xb, xmax, ymax;
  74. /* Bubble-sort y1 <= y2 <= y3 */
  75. if(y1 > y2)
  76. {
  77. cucul_fill_triangle(qq, x2, y2, x1, y1, x3, y3, c);
  78. return;
  79. }
  80. if(y2 > y3)
  81. {
  82. cucul_fill_triangle(qq, x1, y1, x3, y3, x2, y2, c);
  83. return;
  84. }
  85. /* Promote precision */
  86. x1 *= 4;
  87. x2 *= 4;
  88. x3 *= 4;
  89. xmax = qq->width - 1;
  90. ymax = qq->height - 1;
  91. /* Rasterize our triangle */
  92. for(y = y1 < 0 ? 0 : y1; y <= y3 && y <= ymax; y++)
  93. {
  94. if(y <= y2)
  95. {
  96. xa = (y1 == y2) ? x2 : x1 + (x2 - x1) * (y - y1) / (y2 - y1);
  97. xb = (y1 == y3) ? x3 : x1 + (x3 - x1) * (y - y1) / (y3 - y1);
  98. }
  99. else
  100. {
  101. xa = (y3 == y2) ? x2 : x3 + (x2 - x3) * (y - y3) / (y2 - y3);
  102. xb = (y3 == y1) ? x1 : x3 + (x1 - x3) * (y - y3) / (y1 - y3);
  103. }
  104. if(xb < xa)
  105. {
  106. int tmp = xb;
  107. xb = xa; xa = tmp;
  108. }
  109. /* Rescale xa and xb, slightly cropping */
  110. xa = (xa + 2) / 4;
  111. xb = (xb - 2) / 4;
  112. if(xb < 0) continue;
  113. if(xa > xmax) continue;
  114. if(xa < 0) xa = 0;
  115. if(xb > xmax) xb = xmax;
  116. for(x = xa; x <= xb; x++)
  117. cucul_putchar(qq, x, y, c);
  118. }
  119. }