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