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.

309 lines
9.4 KiB

5 years ago
  1. /* --COPYRIGHT--,BSD
  2. * Copyright (c) 2013, Texas Instruments Incorporated
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of Texas Instruments Incorporated nor the names of
  17. * its contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. * --/COPYRIGHT--*/
  32. #include "grlib.h"
  33. //*****************************************************************************
  34. //
  35. //! \addtogroup rectangle_api
  36. //! @{
  37. //
  38. //*****************************************************************************
  39. //*****************************************************************************
  40. //
  41. // Make sure min and max are defined.
  42. //
  43. //*****************************************************************************
  44. #ifndef min
  45. #define min(a, b) (((a) < (b)) ? (a) : (b))
  46. #endif
  47. #ifndef max
  48. #define max(a, b) (((a) < (b)) ? (b) : (a))
  49. #endif
  50. //*****************************************************************************
  51. //
  52. //! Draws a rectangle.
  53. //!
  54. //! \param pContext is a pointer to the drawing context to use.
  55. //! \param pRect is a pointer to the structure containing the extents of the
  56. //! rectangle.
  57. //!
  58. //! This function draws a rectangle. The rectangle will extend from \e lXMin
  59. //! to \e lXMax and \e lYMin to \e lYMax, inclusive.
  60. //!
  61. //! \return None.
  62. //
  63. //*****************************************************************************
  64. void
  65. GrRectDraw(const tContext *pContext, const tRectangle *pRect)
  66. {
  67. //
  68. // Check the arguments.
  69. //
  70. assert(pContext);
  71. assert(pRect);
  72. //
  73. // Draw a line across the top of the rectangle.
  74. //
  75. GrLineDrawH(pContext, pRect->sXMin, pRect->sXMax, pRect->sYMin);
  76. //
  77. // Return if the rectangle is one pixel tall.
  78. //
  79. if(pRect->sYMin == pRect->sYMax)
  80. {
  81. return;
  82. }
  83. //
  84. // Draw a line down the right side of the rectangle.
  85. //
  86. GrLineDrawV(pContext, pRect->sXMax, pRect->sYMin + 1, pRect->sYMax);
  87. //
  88. // Return if the rectangle is one pixel wide.
  89. //
  90. if(pRect->sXMin == pRect->sXMax)
  91. {
  92. return;
  93. }
  94. //
  95. // Draw a line across the bottom of the rectangle.
  96. //
  97. GrLineDrawH(pContext, pRect->sXMax - 1, pRect->sXMin, pRect->sYMax);
  98. //
  99. // Return if the rectangle is two pixels tall.
  100. //
  101. if((pRect->sYMin + 1) == pRect->sYMax)
  102. {
  103. return;
  104. }
  105. //
  106. // Draw a line up the left side of the rectangle.
  107. //
  108. GrLineDrawV(pContext, pRect->sXMin, pRect->sYMax - 1, pRect->sYMin + 1);
  109. }
  110. //*****************************************************************************
  111. //
  112. //! Draws a filled rectangle.
  113. //!
  114. //! \param pContext is a pointer to the drawing context to use.
  115. //! \param pRect is a pointer to the structure containing the extents of the
  116. //! rectangle.
  117. //!
  118. //! This function draws a filled rectangle. The rectangle will extend from
  119. //! \e lXMin to \e lXMax and \e lYMin to \e lYMax, inclusive. The clipping of
  120. //! the rectangle to the clipping rectangle is performed within this routine;
  121. //! the display driver's rectangle fill routine is used to perform the actual
  122. //! rectangle fill.
  123. //!
  124. //! \return None.
  125. //
  126. //*****************************************************************************
  127. void
  128. GrRectFill(const tContext *pContext, const tRectangle *pRect)
  129. {
  130. tRectangle sTemp;
  131. //
  132. // Check the arguments.
  133. //
  134. assert(pContext);
  135. assert(pRect);
  136. //
  137. // Swap the X coordinates if sXMin is greater than sXMax.
  138. //
  139. if(pRect->sXMin > pRect->sXMax)
  140. {
  141. sTemp.sXMin = pRect->sXMax;
  142. sTemp.sXMax = pRect->sXMin;
  143. }
  144. else
  145. {
  146. sTemp.sXMin = pRect->sXMin;
  147. sTemp.sXMax = pRect->sXMax;
  148. }
  149. //
  150. // Swap the Y coordinates if sYMin is greater than sYMax.
  151. //
  152. if(pRect->sYMin > pRect->sYMax)
  153. {
  154. sTemp.sYMin = pRect->sYMax;
  155. sTemp.sYMax = pRect->sYMin;
  156. }
  157. else
  158. {
  159. sTemp.sYMin = pRect->sYMin;
  160. sTemp.sYMax = pRect->sYMax;
  161. }
  162. //
  163. // Now that the coordinates are ordered, return without drawing anything if
  164. // the entire rectangle is out of the clipping region.
  165. //
  166. if((sTemp.sXMin > pContext->sClipRegion.sXMax) ||
  167. (sTemp.sXMax < pContext->sClipRegion.sXMin) ||
  168. (sTemp.sYMin > pContext->sClipRegion.sYMax) ||
  169. (sTemp.sYMax < pContext->sClipRegion.sYMin))
  170. {
  171. return;
  172. }
  173. //
  174. // Clip the X coordinates to the edges of the clipping region if necessary.
  175. //
  176. if(sTemp.sXMin < pContext->sClipRegion.sXMin)
  177. {
  178. sTemp.sXMin = pContext->sClipRegion.sXMin;
  179. }
  180. if(sTemp.sXMax > pContext->sClipRegion.sXMax)
  181. {
  182. sTemp.sXMax = pContext->sClipRegion.sXMax;
  183. }
  184. //
  185. // Clip the Y coordinates to the edges of the clipping region if necessary.
  186. //
  187. if(sTemp.sYMin < pContext->sClipRegion.sYMin)
  188. {
  189. sTemp.sYMin = pContext->sClipRegion.sYMin;
  190. }
  191. if(sTemp.sYMax > pContext->sClipRegion.sYMax)
  192. {
  193. sTemp.sYMax = pContext->sClipRegion.sYMax;
  194. }
  195. //
  196. // Call the low level rectangle fill routine.
  197. //
  198. DpyRectFill(pContext->pDisplay, &sTemp, pContext->ulForeground);
  199. }
  200. //*****************************************************************************
  201. //
  202. //! Determines if two rectangles overlap.
  203. //!
  204. //! \param psRect1 is a pointer to the first rectangle.
  205. //! \param psRect2 is a pointer to the second rectangle.
  206. //!
  207. //! This function determines whether two rectangles overlap. It assumes that
  208. //! rectangles \e psRect1 and \e psRect2 are valid with \e sXMin < \e sXMax and
  209. //! \e sYMin < \e sYMax.
  210. //!
  211. //! \return Returns 1 if there is an overlap or 0 if not.
  212. //
  213. //*****************************************************************************
  214. long
  215. GrRectOverlapCheck(tRectangle *psRect1, tRectangle *psRect2)
  216. {
  217. if((psRect1->sXMax < psRect2->sXMin) ||
  218. (psRect2->sXMax < psRect1->sXMin) ||
  219. (psRect1->sYMax < psRect2->sYMin) ||
  220. (psRect2->sYMax < psRect1->sYMin))
  221. {
  222. return(0);
  223. }
  224. else
  225. {
  226. return(1);
  227. }
  228. }
  229. //*****************************************************************************
  230. //
  231. //! Determines the intersection of two rectangles.
  232. //!
  233. //! \param psRect1 is a pointer to the first rectangle.
  234. //! \param psRect2 is a pointer to the second rectangle.
  235. //! \param psIntersect is a pointer to a rectangle which will be written with
  236. //! the intersection of \e psRect1 and \e psRect2.
  237. //!
  238. //! This function determines if two rectangles overlap and, if they do,
  239. //! calculates the rectangle representing their intersection. If the rectangles
  240. //! do not overlap, 0 is returned and \e psIntersect is not written.
  241. //!
  242. //! \return Returns 1 if there is an overlap or 0 if not.
  243. //
  244. //*****************************************************************************
  245. long
  246. GrRectIntersectGet(tRectangle *psRect1, tRectangle *psRect2,
  247. tRectangle *psIntersect)
  248. {
  249. //
  250. // Make sure we were passed valid rectangles.
  251. //
  252. if((psRect1->sXMax <= psRect1->sXMin) ||
  253. (psRect1->sYMax <= psRect1->sYMin) ||
  254. (psRect2->sXMax <= psRect2->sXMin) ||
  255. (psRect2->sYMax <= psRect2->sYMin))
  256. {
  257. return(0);
  258. }
  259. //
  260. // Make sure that there is an intersection between the two rectangles.
  261. //
  262. if(!GrRectOverlapCheck(psRect1, psRect2))
  263. {
  264. return(0);
  265. }
  266. //
  267. // The rectangles do intersect so determine the rectangle of the
  268. // intersection.
  269. //
  270. psIntersect->sXMin = max(psRect1->sXMin, psRect2->sXMin);
  271. psIntersect->sXMax = min(psRect1->sXMax, psRect2->sXMax);
  272. psIntersect->sYMin = max(psRect1->sYMin, psRect2->sYMin);
  273. psIntersect->sYMax = min(psRect1->sYMax, psRect2->sYMax);
  274. return(1);
  275. }
  276. //*****************************************************************************
  277. //
  278. // Close the Doxygen group.
  279. //! @}
  280. //
  281. //*****************************************************************************