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.
 
 
 
 
 
 

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