rectangle.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //*****************************************************************************
  2. //
  3. // rectangle.c - Routines for drawing and filling rectangles.
  4. //
  5. // Copyright (c) 2007-2010 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Texas Instruments (TI) is supplying this software for use solely and
  9. // exclusively on TI's microcontroller products. The software is owned by
  10. // TI and/or its suppliers, and is protected under applicable copyright
  11. // laws. You may not combine this software with "viral" open-source
  12. // software in order to form a larger program.
  13. //
  14. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  15. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  16. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  18. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  19. // DAMAGES, FOR ANY REASON WHATSOEVER.
  20. //
  21. // This is part of revision 6288 of the Stellaris Graphics Library.
  22. //
  23. //*****************************************************************************
  24. #include "debug.h"
  25. #include "grlib.h"
  26. //*****************************************************************************
  27. //
  28. //! \addtogroup primitives_api
  29. //! @{
  30. //
  31. //*****************************************************************************
  32. //*****************************************************************************
  33. //
  34. // Make sure min and max are defined.
  35. //
  36. //*****************************************************************************
  37. #ifndef min
  38. #define min(a, b) (((a) < (b)) ? (a) : (b))
  39. #endif
  40. #ifndef max
  41. #define max(a, b) (((a) < (b)) ? (b) : (a))
  42. #endif
  43. //*****************************************************************************
  44. //
  45. //! Draws a rectangle.
  46. //!
  47. //! \param pContext is a pointer to the drawing context to use.
  48. //! \param pRect is a pointer to the structure containing the extents of the
  49. //! rectangle.
  50. //!
  51. //! This function draws a rectangle. The rectangle will extend from \e lXMin
  52. //! to \e lXMax and \e lYMin to \e lYMax, inclusive.
  53. //!
  54. //! \return None.
  55. //
  56. //*****************************************************************************
  57. void
  58. GrRectDraw(const tContext *pContext, const tRectangle *pRect)
  59. {
  60. //
  61. // Check the arguments.
  62. //
  63. ASSERT(pContext);
  64. ASSERT(pRect);
  65. //
  66. // Draw a line across the top of the rectangle.
  67. //
  68. GrLineDrawH(pContext, pRect->sXMin, pRect->sXMax, pRect->sYMin);
  69. //
  70. // Return if the rectangle is one pixel tall.
  71. //
  72. if(pRect->sYMin == pRect->sYMax)
  73. {
  74. return;
  75. }
  76. //
  77. // Draw a line down the right side of the rectangle.
  78. //
  79. GrLineDrawV(pContext, pRect->sXMax, pRect->sYMin + 1, pRect->sYMax);
  80. //
  81. // Return if the rectangle is one pixel wide.
  82. //
  83. if(pRect->sXMin == pRect->sXMax)
  84. {
  85. return;
  86. }
  87. //
  88. // Draw a line across the bottom of the rectangle.
  89. //
  90. GrLineDrawH(pContext, pRect->sXMax - 1, pRect->sXMin, pRect->sYMax);
  91. //
  92. // Return if the rectangle is two pixels tall.
  93. //
  94. if((pRect->sYMin + 1) == pRect->sYMax)
  95. {
  96. return;
  97. }
  98. //
  99. // Draw a line up the left side of the rectangle.
  100. //
  101. GrLineDrawV(pContext, pRect->sXMin, pRect->sYMax - 1, pRect->sYMin + 1);
  102. }
  103. //*****************************************************************************
  104. //
  105. //! Draws a filled rectangle.
  106. //!
  107. //! \param pContext is a pointer to the drawing context to use.
  108. //! \param pRect is a pointer to the structure containing the extents of the
  109. //! rectangle.
  110. //!
  111. //! This function draws a filled rectangle. The rectangle will extend from
  112. //! \e lXMin to \e lXMax and \e lYMin to \e lYMax, inclusive. The clipping of
  113. //! the rectangle to the clipping rectangle is performed within this routine;
  114. //! the display driver's rectangle fill routine is used to perform the actual
  115. //! rectangle fill.
  116. //!
  117. //! \return None.
  118. //
  119. //*****************************************************************************
  120. void
  121. GrRectFill(const tContext *pContext, const tRectangle *pRect)
  122. {
  123. tRectangle sTemp;
  124. //
  125. // Check the arguments.
  126. //
  127. ASSERT(pContext);
  128. ASSERT(pRect);
  129. //
  130. // Swap the X coordinates if sXMin is greater than sXMax.
  131. //
  132. if(pRect->sXMin > pRect->sXMax)
  133. {
  134. sTemp.sXMin = pRect->sXMax;
  135. sTemp.sXMax = pRect->sXMin;
  136. }
  137. else
  138. {
  139. sTemp.sXMin = pRect->sXMin;
  140. sTemp.sXMax = pRect->sXMax;
  141. }
  142. //
  143. // Swap the Y coordinates if sYMin is greater than sYMax.
  144. //
  145. if(pRect->sYMin > pRect->sYMax)
  146. {
  147. sTemp.sYMin = pRect->sYMax;
  148. sTemp.sYMax = pRect->sYMin;
  149. }
  150. else
  151. {
  152. sTemp.sYMin = pRect->sYMin;
  153. sTemp.sYMax = pRect->sYMax;
  154. }
  155. //
  156. // Now that the coordinates are ordered, return without drawing anything if
  157. // the entire rectangle is out of the clipping region.
  158. //
  159. if((sTemp.sXMin > pContext->sClipRegion.sXMax) ||
  160. (sTemp.sXMax < pContext->sClipRegion.sXMin) ||
  161. (sTemp.sYMin > pContext->sClipRegion.sYMax) ||
  162. (sTemp.sYMax < pContext->sClipRegion.sYMin))
  163. {
  164. return;
  165. }
  166. //
  167. // Clip the X coordinates to the edges of the clipping region if necessary.
  168. //
  169. if(sTemp.sXMin < pContext->sClipRegion.sXMin)
  170. {
  171. sTemp.sXMin = pContext->sClipRegion.sXMin;
  172. }
  173. if(sTemp.sXMax > pContext->sClipRegion.sXMax)
  174. {
  175. sTemp.sXMax = pContext->sClipRegion.sXMax;
  176. }
  177. //
  178. // Clip the Y coordinates to the edges of the clipping region if necessary.
  179. //
  180. if(sTemp.sYMin < pContext->sClipRegion.sYMin)
  181. {
  182. sTemp.sYMin = pContext->sClipRegion.sYMin;
  183. }
  184. if(sTemp.sYMax > pContext->sClipRegion.sYMax)
  185. {
  186. sTemp.sYMax = pContext->sClipRegion.sYMax;
  187. }
  188. //
  189. // Call the low level rectangle fill routine.
  190. //
  191. DpyRectFill(pContext->pDisplay, &sTemp, pContext->ulForeground);
  192. }
  193. //*****************************************************************************
  194. //
  195. //! Determines if two rectangles overlap.
  196. //!
  197. //! \param psRect1 is a pointer to the first rectangle.
  198. //! \param psRect2 is a pointer to the second rectangle.
  199. //!
  200. //! This function determines whether two rectangles overlap. It assumes that
  201. //! rectangles \e psRect1 and \e psRect2 are valid with \e sXMin < \e sXMax and
  202. //! \e sYMin < \e sYMax.
  203. //!
  204. //! \return Returns 1 if there is an overlap or 0 if not.
  205. //
  206. //*****************************************************************************
  207. int
  208. GrRectOverlapCheck(tRectangle *psRect1, tRectangle *psRect2)
  209. {
  210. if((psRect1->sXMax < psRect2->sXMin) ||
  211. (psRect2->sXMax < psRect1->sXMin) ||
  212. (psRect1->sYMax < psRect2->sYMin) ||
  213. (psRect2->sYMax < psRect1->sYMin))
  214. {
  215. return(0);
  216. }
  217. else
  218. {
  219. return(1);
  220. }
  221. }
  222. //*****************************************************************************
  223. //
  224. //! Determines the intersection of two rectangles.
  225. //!
  226. //! \param psRect1 is a pointer to the first rectangle.
  227. //! \param psRect2 is a pointer to the second rectangle.
  228. //! \param psIntersect is a pointer to a rectangle which will be written with
  229. //! the intersection of \e psRect1 and \e psRect2.
  230. //!
  231. //! This function determines if two rectangles overlap and, if they do,
  232. //! calculates the rectangle representing their intersection. If the rectangles
  233. //! do not overlap, 0 is returned and \e psIntersect is not written.
  234. //!
  235. //! \return Returns 1 if there is an overlap or 0 if not.
  236. //
  237. //*****************************************************************************
  238. int
  239. GrRectIntersectGet(tRectangle *psRect1, tRectangle *psRect2,
  240. tRectangle *psIntersect)
  241. {
  242. //
  243. // Make sure we were passed valid rectangles.
  244. //
  245. if((psRect1->sXMax <= psRect1->sXMin) ||
  246. (psRect1->sYMax <= psRect1->sYMin) ||
  247. (psRect2->sXMax <= psRect2->sXMin) ||
  248. (psRect2->sYMax <= psRect2->sYMin))
  249. {
  250. return(0);
  251. }
  252. //
  253. // Make sure that there is an intersection between the two rectangles.
  254. //
  255. if(!GrRectOverlapCheck(psRect1, psRect2))
  256. {
  257. return(0);
  258. }
  259. //
  260. // The rectangles do intersect so determine the rectangle of the
  261. // intersection.
  262. //
  263. psIntersect->sXMin = max(psRect1->sXMin, psRect2->sXMin);
  264. psIntersect->sXMax = min(psRect1->sXMax, psRect2->sXMax);
  265. psIntersect->sYMin = max(psRect1->sYMin, psRect2->sYMin);
  266. psIntersect->sYMax = min(psRect1->sYMax, psRect2->sYMax);
  267. return(1);
  268. }
  269. //*****************************************************************************
  270. //
  271. // Close the Doxygen group.
  272. //! @}
  273. //
  274. //*****************************************************************************