Home | History | Annotate | Download | only in core
      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     // do nothing if the params are empty
     21     if (left >= right || top >= bottom) {
     22         return;
     23     }
     24 
     25     // if we are empty, just assign
     26     if (fLeft >= fRight || fTop >= fBottom) {
     27         this->set(left, top, right, bottom);
     28     } else {
     29         if (left < fLeft) fLeft = left;
     30         if (top < fTop) fTop = top;
     31         if (right > fRight) fRight = right;
     32         if (bottom > fBottom) fBottom = bottom;
     33     }
     34 }
     35 
     36 void SkIRect::sort() {
     37     if (fLeft > fRight) {
     38         SkTSwap<int32_t>(fLeft, fRight);
     39     }
     40     if (fTop > fBottom) {
     41         SkTSwap<int32_t>(fTop, fBottom);
     42     }
     43 }
     44 
     45 /////////////////////////////////////////////////////////////////////////////
     46 
     47 bool SkRect::hasValidCoordinates() const {
     48     return  SkScalarIsFinite(fLeft) &&
     49             SkScalarIsFinite(fTop) &&
     50             SkScalarIsFinite(fRight) &&
     51             SkScalarIsFinite(fBottom);
     52 }
     53 
     54 void SkRect::sort() {
     55     if (fLeft > fRight) {
     56         SkTSwap<SkScalar>(fLeft, fRight);
     57     }
     58     if (fTop > fBottom) {
     59         SkTSwap<SkScalar>(fTop, fBottom);
     60     }
     61 }
     62 
     63 void SkRect::toQuad(SkPoint quad[4]) const {
     64     SkASSERT(quad);
     65 
     66     quad[0].set(fLeft, fTop);
     67     quad[1].set(fRight, fTop);
     68     quad[2].set(fRight, fBottom);
     69     quad[3].set(fLeft, fBottom);
     70 }
     71 
     72 void SkRect::set(const SkPoint pts[], int count) {
     73     SkASSERT((pts && count > 0) || count == 0);
     74 
     75     if (count <= 0) {
     76         sk_bzero(this, sizeof(SkRect));
     77     } else {
     78 #ifdef SK_SCALAR_SLOW_COMPARES
     79         int32_t    l, t, r, b;
     80 
     81         l = r = SkScalarAs2sCompliment(pts[0].fX);
     82         t = b = SkScalarAs2sCompliment(pts[0].fY);
     83 
     84         for (int i = 1; i < count; i++) {
     85             int32_t x = SkScalarAs2sCompliment(pts[i].fX);
     86             int32_t y = SkScalarAs2sCompliment(pts[i].fY);
     87 
     88             if (x < l) l = x; else if (x > r) r = x;
     89             if (y < t) t = y; else if (y > b) b = y;
     90         }
     91         this->set(Sk2sComplimentAsScalar(l),
     92                   Sk2sComplimentAsScalar(t),
     93                   Sk2sComplimentAsScalar(r),
     94                   Sk2sComplimentAsScalar(b));
     95 #else
     96         SkScalar    l, t, r, b;
     97 
     98         l = r = pts[0].fX;
     99         t = b = pts[0].fY;
    100 
    101         for (int i = 1; i < count; i++) {
    102             SkScalar x = pts[i].fX;
    103             SkScalar y = pts[i].fY;
    104 
    105             if (x < l) l = x; else if (x > r) r = x;
    106             if (y < t) t = y; else if (y > b) b = y;
    107         }
    108         this->set(l, t, r, b);
    109 #endif
    110     }
    111 }
    112 
    113 bool SkRect::intersect(SkScalar left, SkScalar top, SkScalar right,
    114                        SkScalar bottom) {
    115     if (left < right && top < bottom && !this->isEmpty() && // check for empties
    116         fLeft < right && left < fRight && fTop < bottom && top < fBottom)
    117     {
    118         if (fLeft < left) fLeft = left;
    119         if (fTop < top) fTop = top;
    120         if (fRight > right) fRight = right;
    121         if (fBottom > bottom) fBottom = bottom;
    122         return true;
    123     }
    124     return false;
    125 }
    126 
    127 bool SkRect::intersect(const SkRect& r) {
    128     SkASSERT(&r);
    129     return this->intersect(r.fLeft, r.fTop, r.fRight, r.fBottom);
    130 }
    131 
    132 void SkRect::join(SkScalar left, SkScalar top, SkScalar right,
    133                   SkScalar bottom) {
    134     // do nothing if the params are empty
    135     if (left >= right || top >= bottom) {
    136         return;
    137     }
    138 
    139     // if we are empty, just assign
    140     if (fLeft >= fRight || fTop >= fBottom) {
    141         this->set(left, top, right, bottom);
    142     } else {
    143         if (left < fLeft) fLeft = left;
    144         if (top < fTop) fTop = top;
    145         if (right > fRight) fRight = right;
    146         if (bottom > fBottom) fBottom = bottom;
    147     }
    148 }
    149 
    150