u8g2_intersection.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. u8g2_intersection.c
  3. Intersection calculation, code taken from u8g_clip.c
  4. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  5. Copyright (c) 2016, olikraus@gmail.com
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this
  12. list of conditions and the following disclaimer in the documentation and/or other
  13. materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  15. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "u8g2.h"
  29. #ifdef __GNUC__
  30. #define U8G2_ALWAYS_INLINE __inline__ __attribute__((always_inline))
  31. #else
  32. #define U8G2_ALWAYS_INLINE
  33. #endif
  34. #if defined(U8G2_WITH_INTERSECTION) || defined(U8G2_WITH_CLIP_WINDOW_SUPPORT)
  35. #ifdef OLD_VERSION_WITH_SYMETRIC_BOUNDARIES
  36. /*
  37. intersection assumptions:
  38. a1 <= a2 is always true
  39. minimized version
  40. ---1----0 1 b1 <= a2 && b1 > b2
  41. -----1--0 1 b2 >= a1 && b1 > b2
  42. ---1-1--- 1 b1 <= a2 && b2 >= a1
  43. */
  44. /*
  45. calculate the intersection between a0/a1 and v0/v1
  46. The intersection check returns one if the range of a0/a1 has an intersection with v0/v1.
  47. The intersection check includes the boundary values v1 and a1.
  48. The following asserts will succeed:
  49. assert( u8g2_is_intersection_decision_tree(4, 6, 7, 9) == 0 );
  50. assert( u8g2_is_intersection_decision_tree(4, 6, 6, 9) != 0 );
  51. assert( u8g2_is_intersection_decision_tree(6, 9, 4, 6) != 0 );
  52. assert( u8g2_is_intersection_decision_tree(7, 9, 4, 6) == 0 );
  53. */
  54. //static uint8_t U8G2_ALWAYS_INLINE u8g2_is_intersection_decision_tree(u8g_uint_t a0, u8g_uint_t a1, u8g_uint_t v0, u8g_uint_t v1)
  55. static uint8_t u8g2_is_intersection_decision_tree(u8g2_uint_t a0, u8g2_uint_t a1, u8g2_uint_t v0, u8g2_uint_t v1)
  56. {
  57. if ( v0 <= a1 )
  58. {
  59. if ( v1 >= a0 )
  60. {
  61. return 1;
  62. }
  63. else
  64. {
  65. if ( v0 > v1 )
  66. {
  67. return 1;
  68. }
  69. else
  70. {
  71. return 0;
  72. }
  73. }
  74. }
  75. else
  76. {
  77. if ( v1 >= a0 )
  78. {
  79. if ( v0 > v1 )
  80. {
  81. return 1;
  82. }
  83. else
  84. {
  85. return 0;
  86. }
  87. }
  88. else
  89. {
  90. return 0;
  91. }
  92. }
  93. }
  94. #endif /* OLD_VERSION_WITH_SYMETRIC_BOUNDARIES */
  95. /*
  96. version with asymetric boundaries.
  97. a1 and v1 are excluded
  98. v0 == v1 is not support end return 1
  99. */
  100. uint8_t u8g2_is_intersection_decision_tree(u8g2_uint_t a0, u8g2_uint_t a1, u8g2_uint_t v0, u8g2_uint_t v1)
  101. {
  102. if ( v0 < a1 ) // v0 <= a1
  103. {
  104. if ( v1 > a0 ) // v1 >= a0
  105. {
  106. return 1;
  107. }
  108. else
  109. {
  110. if ( v0 > v1 ) // v0 > v1
  111. {
  112. return 1;
  113. }
  114. else
  115. {
  116. return 0;
  117. }
  118. }
  119. }
  120. else
  121. {
  122. if ( v1 > a0 ) // v1 >= a0
  123. {
  124. if ( v0 > v1 ) // v0 > v1
  125. {
  126. return 1;
  127. }
  128. else
  129. {
  130. return 0;
  131. }
  132. }
  133. else
  134. {
  135. return 0;
  136. }
  137. }
  138. }
  139. /* upper limits are not included (asymetric boundaries) */
  140. uint8_t u8g2_IsIntersection(u8g2_t *u8g2, u8g2_uint_t x0, u8g2_uint_t y0, u8g2_uint_t x1, u8g2_uint_t y1)
  141. {
  142. if ( u8g2_is_intersection_decision_tree(u8g2->user_y0, u8g2->user_y1, y0, y1) == 0 )
  143. return 0;
  144. return u8g2_is_intersection_decision_tree(u8g2->user_x0, u8g2->user_x1, x0, x1);
  145. }
  146. #endif /* U8G2_WITH_INTERSECTION */