nxglib_circlepts.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /****************************************************************************
  2. * graphics/nxglib/nxglib_circlepts.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. /* Named indices into the 16 circle points generated by nxgl_circlepts */
  54. #define POINT_0p0 0
  55. #define POINT_22p5 1
  56. #define POINT_45p0 2
  57. #define POINT_67p5 3
  58. #define POINT_90p0 4
  59. #define POINT_112p5 5
  60. #define POINT_135p0 6
  61. #define POINT_157p5 7
  62. #define POINT_180p0 8
  63. #define POINT_202p5 9
  64. #define POINT_225p0 10
  65. #define POINT_247p5 11
  66. #define POINT_270p0 12
  67. #define POINT_292p5 13
  68. #define POINT_315p0 14
  69. #define POINT_337p5 15
  70. #define NCIRCLE_POINTS 16
  71. /****************************************************************************
  72. * Private Types
  73. ****************************************************************************/
  74. /****************************************************************************
  75. * Private Data
  76. ****************************************************************************/
  77. /****************************************************************************
  78. * Public Data
  79. ****************************************************************************/
  80. /****************************************************************************
  81. * Private Functions
  82. ****************************************************************************/
  83. /****************************************************************************
  84. * Public Functions
  85. ****************************************************************************/
  86. /****************************************************************************
  87. * Name: nxgl_circlepts
  88. *
  89. * Description:
  90. * Given a description of a circle, return a set of 16 points on the
  91. * circumference of the circle. These points may then be used by
  92. * nx_drawcircle() or related APIs to draw a circle outline.
  93. *
  94. * Input parameters:
  95. * center - A pointer to the point that is the center of the circle
  96. * radius - The radius of the circle in pixels.
  97. * circle - A pointer the first entry in an array of 16 points where the
  98. * circle points will be returned.
  99. *
  100. * Returned value:
  101. * None
  102. *
  103. ****************************************************************************/
  104. void nxgl_circlepts(FAR const struct nxgl_point_s *center, nxgl_coord_t radius,
  105. FAR struct nxgl_point_s *circle)
  106. {
  107. nxgl_coord_t xoffs;
  108. nxgl_coord_t yoffs;
  109. /* 0, 90, 180, and 270 degrees are the easiest */
  110. circle[POINT_0p0].x = center->x + radius;
  111. circle[POINT_0p0].y = center->y;
  112. circle[POINT_90p0].x = center->x;
  113. circle[POINT_90p0].y = center->y + radius;
  114. circle[POINT_180p0].x = center->x - radius;
  115. circle[POINT_180p0].y = center->y;
  116. circle[POINT_270p0].x = center->x;
  117. circle[POINT_270p0].y = center->y - radius;
  118. /* Now 22.5, 67.5, 112.5, 157.5, 202.5, 247.5, 292.5, and 337.5 */
  119. xoffs = b16toi(b16muli(COS_22p5, radius) + b16HALF);
  120. yoffs = b16toi(b16muli(SIN_22p5, radius) + b16HALF);
  121. circle[POINT_22p5].x = center->x + xoffs;
  122. circle[POINT_22p5].y = center->y + yoffs;
  123. circle[POINT_157p5].x = center->x - xoffs;
  124. circle[POINT_157p5].y = center->y + yoffs;
  125. circle[POINT_202p5].x = center->x - xoffs;
  126. circle[POINT_202p5].y = center->y - yoffs;
  127. circle[POINT_337p5].x = center->x + xoffs;
  128. circle[POINT_337p5].y = center->y - yoffs;
  129. circle[POINT_67p5].x = center->x + yoffs;
  130. circle[POINT_67p5].y = center->y + xoffs;
  131. circle[POINT_112p5].x = center->x - yoffs;
  132. circle[POINT_112p5].y = center->y + xoffs;
  133. circle[POINT_247p5].x = center->x - yoffs;
  134. circle[POINT_247p5].y = center->y - xoffs;
  135. circle[POINT_292p5].x = center->x + yoffs;
  136. circle[POINT_292p5].y = center->y - xoffs;
  137. /* Finally, 45.0, 135.0, 225.0, 315.0 */
  138. xoffs = b16toi(b16muli(COS_45p0, radius) + b16HALF);
  139. circle[POINT_45p0].x = center->x + xoffs;
  140. circle[POINT_45p0].y = center->y + xoffs;
  141. circle[POINT_135p0].x = center->x - xoffs;
  142. circle[POINT_135p0].y = center->y + xoffs;
  143. circle[POINT_225p0].x = center->x - xoffs;
  144. circle[POINT_225p0].y = center->y - xoffs;
  145. circle[POINT_315p0].x = center->x + xoffs;
  146. circle[POINT_315p0].y = center->y - xoffs;
  147. }