| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 |
| 19 | #include "vrect.h" |
| 20 | #include <algorithm> |
| 21 |
| 22 | VRect VRect::operator&(const VRect &r) const |
| 23 | { |
| 24 | if (empty()) return VRect(); |
| 25 |
| 26 | int l1 = x1; |
| 27 | int r1 = x1; |
| 28 | if (x2 - x1 + 1 < 0) |
| 29 | l1 = x2; |
| 30 | else |
| 31 | r1 = x2; |
| 32 |
| 33 | int l2 = r.x1; |
| 34 | int r2 = r.x1; |
| 35 | if (r.x2 - r.x1 + 1 < 0) |
| 36 | l2 = r.x2; |
| 37 | else |
| 38 | r2 = r.x2; |
| 39 |
| 40 | if (l1 > r2 || l2 > r1) return VRect(); |
| 41 |
| 42 | int t1 = y1; |
| 43 | int b1 = y1; |
| 44 | if (y2 - y1 + 1 < 0) |
| 45 | t1 = y2; |
| 46 | else |
| 47 | b1 = y2; |
| 48 |
| 49 | int t2 = r.y1; |
| 50 | int b2 = r.y1; |
| 51 | if (r.y2 - r.y1 + 1 < 0) |
| 52 | t2 = r.y2; |
| 53 | else |
| 54 | b2 = r.y2; |
| 55 |
| 56 | if (t1 > b2 || t2 > b1) return VRect(); |
| 57 |
| 58 | VRect tmp; |
| 59 | tmp.x1 = std::max(l1, l2); |
| 60 | tmp.x2 = std::min(r1, r2); |
| 61 | tmp.y1 = std::max(t1, t2); |
| 62 | tmp.y2 = std::min(b1, b2); |
| 63 | return tmp; |
| 64 | } |
| 65 |