reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "SkRect.h" |
| 18 | |
| 19 | void SkIRect::join(int32_t left, int32_t top, int32_t right, int32_t bottom) |
| 20 | { |
| 21 | // do nothing if the params are empty |
| 22 | if (left >= right || top >= bottom) |
| 23 | return; |
| 24 | |
| 25 | // if we are empty, just assign |
| 26 | if (fLeft >= fRight || fTop >= fBottom) |
| 27 | this->set(left, top, right, bottom); |
| 28 | else |
| 29 | { |
| 30 | if (left < fLeft) fLeft = left; |
| 31 | if (top < fTop) fTop = top; |
| 32 | if (right > fRight) fRight = right; |
| 33 | if (bottom > fBottom) fBottom = bottom; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | void SkIRect::sort() |
| 38 | { |
| 39 | if (fLeft > fRight) |
| 40 | SkTSwap<int32_t>(fLeft, fRight); |
| 41 | if (fTop > fBottom) |
| 42 | SkTSwap<int32_t>(fTop, fBottom); |
| 43 | } |
| 44 | |
| 45 | ///////////////////////////////////////////////////////////////////////////// |
| 46 | |
| 47 | void SkRect::sort() |
| 48 | { |
| 49 | if (fLeft > fRight) |
| 50 | SkTSwap<SkScalar>(fLeft, fRight); |
| 51 | if (fTop > fBottom) |
| 52 | SkTSwap<SkScalar>(fTop, fBottom); |
| 53 | } |
| 54 | |
| 55 | void SkRect::toQuad(SkPoint quad[4]) const |
| 56 | { |
| 57 | SkASSERT(quad); |
| 58 | |
| 59 | quad[0].set(fLeft, fTop); |
| 60 | quad[1].set(fRight, fTop); |
| 61 | quad[2].set(fRight, fBottom); |
| 62 | quad[3].set(fLeft, fBottom); |
| 63 | } |
| 64 | |
| 65 | void SkRect::set(const SkPoint pts[], int count) |
| 66 | { |
| 67 | SkASSERT((pts && count > 0) || count == 0); |
| 68 | |
| 69 | if (count <= 0) { |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame^] | 70 | sk_bzero(this, sizeof(SkRect)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | } else { |
| 72 | #ifdef SK_SCALAR_SLOW_COMPARES |
| 73 | int32_t l, t, r, b; |
| 74 | |
| 75 | l = r = SkScalarAs2sCompliment(pts[0].fX); |
| 76 | t = b = SkScalarAs2sCompliment(pts[0].fY); |
| 77 | |
| 78 | for (int i = 1; i < count; i++) { |
| 79 | int32_t x = SkScalarAs2sCompliment(pts[i].fX); |
| 80 | int32_t y = SkScalarAs2sCompliment(pts[i].fY); |
| 81 | |
| 82 | if (x < l) l = x; else if (x > r) r = x; |
| 83 | if (y < t) t = y; else if (y > b) b = y; |
| 84 | } |
| 85 | this->set(Sk2sComplimentAsScalar(l), |
| 86 | Sk2sComplimentAsScalar(t), |
| 87 | Sk2sComplimentAsScalar(r), |
| 88 | Sk2sComplimentAsScalar(b)); |
| 89 | #else |
| 90 | SkScalar l, t, r, b; |
| 91 | |
| 92 | l = r = pts[0].fX; |
| 93 | t = b = pts[0].fY; |
| 94 | |
| 95 | for (int i = 1; i < count; i++) { |
| 96 | SkScalar x = pts[i].fX; |
| 97 | SkScalar y = pts[i].fY; |
| 98 | |
| 99 | if (x < l) l = x; else if (x > r) r = x; |
| 100 | if (y < t) t = y; else if (y > b) b = y; |
| 101 | } |
| 102 | this->set(l, t, r, b); |
| 103 | #endif |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | bool SkRect::intersect(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) |
| 108 | { |
| 109 | if (left < right && top < bottom && !this->isEmpty() && // check for empties |
| 110 | fLeft < right && left < fRight && fTop < bottom && top < fBottom) |
| 111 | { |
| 112 | if (fLeft < left) fLeft = left; |
| 113 | if (fTop < top) fTop = top; |
| 114 | if (fRight > right) fRight = right; |
| 115 | if (fBottom > bottom) fBottom = bottom; |
| 116 | return true; |
| 117 | } |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | bool SkRect::intersect(const SkRect& r) |
| 122 | { |
| 123 | SkASSERT(&r); |
| 124 | return this->intersect(r.fLeft, r.fTop, r.fRight, r.fBottom); |
| 125 | } |
| 126 | |
| 127 | void SkRect::join(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) |
| 128 | { |
| 129 | // do nothing if the params are empty |
| 130 | if (left >= right || top >= bottom) |
| 131 | return; |
| 132 | |
| 133 | // if we are empty, just assign |
| 134 | if (fLeft >= fRight || fTop >= fBottom) |
| 135 | this->set(left, top, right, bottom); |
| 136 | else |
| 137 | { |
| 138 | if (left < fLeft) fLeft = left; |
| 139 | if (top < fTop) fTop = top; |
| 140 | if (right > fRight) fRight = right; |
| 141 | if (bottom > fBottom) fBottom = bottom; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | |