nxglib_circletraps.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /****************************************************************************
  2. * graphics/nxglib/nxglib_circletraps.c
  3. *
  4. * Copyright (C) 2011 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <string.h>
  40. #include <errno.h>
  41. #include <fixedmath.h>
  42. #include <nuttx/nx/nxglib.h>
  43. /****************************************************************************
  44. * Pre-Processor Definitions
  45. ****************************************************************************/
  46. /* Trigonometry */
  47. #define SIN_0p0 0 /* sin(0) = 0 */
  48. #define COS_0p0 1 /* cos(0) = 1 */
  49. #define SIN_22p5 25080 /* sin(22.5) = 25080 / 65536 */
  50. #define COS_22p5 60547 /* cos(22.5) = 60547 / 65536 */
  51. #define SIN_45p0 46341 /* sin(45) = 46341 / 65536 */
  52. #define COS_45p0 SIN_45p0 /* cos(45) = sin(45) */
  53. /****************************************************************************
  54. * Private Types
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Private Data
  58. ****************************************************************************/
  59. /****************************************************************************
  60. * Public Data
  61. ****************************************************************************/
  62. /****************************************************************************
  63. * Private Functions
  64. ****************************************************************************/
  65. /****************************************************************************
  66. * Public Functions
  67. ****************************************************************************/
  68. /****************************************************************************
  69. * Name: nxgl_circletraps
  70. *
  71. * Description:
  72. * Given a description of a a circle, return 8 trapezoids that can be
  73. * used to fill the circle by nx_fillcircle() and other interfaces.
  74. *
  75. * Input parameters:
  76. * center - A pointer to the point that is the center of the circle
  77. * radius - The radius of the circle in pixels.
  78. * circle - A pointer the first entry in an array of 8 trapezoids where
  79. * the circle description will be returned.
  80. *
  81. * Returned value:
  82. * None
  83. *
  84. ****************************************************************************/
  85. void nxgl_circletraps(FAR const struct nxgl_point_s *center, nxgl_coord_t radius,
  86. FAR struct nxgl_trapezoid_s *circle)
  87. {
  88. nxgl_coord_t xoffs;
  89. nxgl_coord_t yoffs;
  90. circle[0].top.x1 = itob16(center->x);
  91. circle[0].top.x2 = circle[0].top.x1;
  92. circle[0].top.y = center->y - radius;
  93. circle[7].bot.x1 = circle[0].top.x1;
  94. circle[7].bot.x2 = circle[0].top.x1;
  95. circle[7].bot.y = center->y + radius;
  96. circle[3].bot.x1 = itob16(center->x - radius);
  97. circle[3].bot.x2 = itob16(center->x + radius);
  98. circle[3].bot.y = center->y;
  99. circle[4].top.x1 = circle[3].bot.x1;
  100. circle[4].top.x2 = circle[3].bot.x2;
  101. circle[4].top.y = circle[3].bot.y;
  102. /* Now 22.5, 67.5, 112.5, 157.5, 202.5, 247.5, 292.5, and 337.5 */
  103. xoffs = b16toi(b16muli(COS_22p5, radius) + b16HALF);
  104. yoffs = b16toi(b16muli(SIN_22p5, radius) + b16HALF);
  105. circle[2].bot.x1 = itob16(center->x - xoffs);
  106. circle[2].bot.x2 = itob16(center->x + xoffs);
  107. circle[2].bot.y = center->y - yoffs;
  108. circle[3].top.x1 = circle[2].bot.x1;
  109. circle[3].top.x2 = circle[2].bot.x2;
  110. circle[3].top.y = circle[2].bot.y;
  111. circle[4].bot.x1 = circle[2].bot.x1;
  112. circle[4].bot.x2 = circle[2].bot.x2;
  113. circle[4].bot.y = center->y + yoffs;
  114. circle[5].top.x1 = circle[4].bot.x1;
  115. circle[5].top.x2 = circle[4].bot.x2;
  116. circle[5].top.y = circle[4].bot.y;
  117. circle[0].bot.x1 = itob16(center->x - yoffs);
  118. circle[0].bot.x2 = itob16(center->x + yoffs);
  119. circle[0].bot.y = center->y - xoffs;
  120. circle[1].top.x1 = circle[0].bot.x1;
  121. circle[1].top.x2 = circle[0].bot.x2;
  122. circle[1].top.y = circle[0].bot.y;
  123. circle[6].bot.x1 = circle[1].top.x1;
  124. circle[6].bot.x2 = circle[1].top.x2;
  125. circle[6].bot.y = center->y + xoffs;
  126. circle[7].top.x1 = circle[6].bot.x1;
  127. circle[7].top.x2 = circle[6].bot.x2;
  128. circle[7].top.y = circle[6].bot.y;
  129. /* Finally, 45.0, 135.0, 225.0, 315.0 */
  130. xoffs = b16toi(b16muli(COS_45p0, radius) + b16HALF);
  131. circle[1].bot.x1 = itob16(center->x - xoffs);
  132. circle[1].bot.x2 = itob16(center->x + xoffs);
  133. circle[1].bot.y = center->y - xoffs;
  134. circle[2].top.x1 = circle[1].bot.x1;
  135. circle[2].top.x2 = circle[1].bot.x2;
  136. circle[2].top.y = circle[1].bot.y;
  137. circle[5].bot.x1 = circle[1].bot.x1;
  138. circle[5].bot.x2 = circle[1].bot.x2;
  139. circle[5].bot.y = center->y + xoffs;
  140. circle[6].top.x1 = circle[5].bot.x1;
  141. circle[6].top.x2 = circle[5].bot.x2;
  142. circle[6].top.y = circle[5].bot.y;
  143. }