SkRect.cpp revision ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976e
1
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#include "SkRect.h"
11
12void SkIRect::join(int32_t left, int32_t top, int32_t right, int32_t bottom) {
13    // do nothing if the params are empty
14    if (left >= right || top >= bottom) {
15        return;
16    }
17
18    // if we are empty, just assign
19    if (fLeft >= fRight || fTop >= fBottom) {
20        this->set(left, top, right, bottom);
21    } else {
22        if (left < fLeft) fLeft = left;
23        if (top < fTop) fTop = top;
24        if (right > fRight) fRight = right;
25        if (bottom > fBottom) fBottom = bottom;
26    }
27}
28
29void SkIRect::sort() {
30    if (fLeft > fRight) {
31        SkTSwap<int32_t>(fLeft, fRight);
32    }
33    if (fTop > fBottom) {
34        SkTSwap<int32_t>(fTop, fBottom);
35    }
36}
37
38/////////////////////////////////////////////////////////////////////////////
39
40bool SkRect::hasValidCoordinates() const {
41    return  SkScalarIsFinite(fLeft) &&
42            SkScalarIsFinite(fTop) &&
43            SkScalarIsFinite(fRight) &&
44            SkScalarIsFinite(fBottom);
45}
46
47void SkRect::sort() {
48    if (fLeft > fRight) {
49        SkTSwap<SkScalar>(fLeft, fRight);
50    }
51    if (fTop > fBottom) {
52        SkTSwap<SkScalar>(fTop, fBottom);
53    }
54}
55
56void SkRect::toQuad(SkPoint quad[4]) const {
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
65void SkRect::set(const SkPoint pts[], int count) {
66    SkASSERT((pts && count > 0) || count == 0);
67
68    if (count <= 0) {
69        sk_bzero(this, sizeof(SkRect));
70    } else {
71#ifdef SK_SCALAR_SLOW_COMPARES
72        int32_t    l, t, r, b;
73
74        l = r = SkScalarAs2sCompliment(pts[0].fX);
75        t = b = SkScalarAs2sCompliment(pts[0].fY);
76
77        for (int i = 1; i < count; i++) {
78            int32_t x = SkScalarAs2sCompliment(pts[i].fX);
79            int32_t y = SkScalarAs2sCompliment(pts[i].fY);
80
81            if (x < l) l = x; else if (x > r) r = x;
82            if (y < t) t = y; else if (y > b) b = y;
83        }
84        this->set(Sk2sComplimentAsScalar(l),
85                  Sk2sComplimentAsScalar(t),
86                  Sk2sComplimentAsScalar(r),
87                  Sk2sComplimentAsScalar(b));
88#else
89        SkScalar    l, t, r, b;
90
91        l = r = pts[0].fX;
92        t = b = pts[0].fY;
93
94        for (int i = 1; i < count; i++) {
95            SkScalar x = pts[i].fX;
96            SkScalar y = pts[i].fY;
97
98            if (x < l) l = x; else if (x > r) r = x;
99            if (y < t) t = y; else if (y > b) b = y;
100        }
101        this->set(l, t, r, b);
102#endif
103    }
104}
105
106bool SkRect::intersect(SkScalar left, SkScalar top, SkScalar right,
107                       SkScalar bottom) {
108    if (left < right && top < bottom && !this->isEmpty() && // check for empties
109        fLeft < right && left < fRight && fTop < bottom && top < fBottom)
110    {
111        if (fLeft < left) fLeft = left;
112        if (fTop < top) fTop = top;
113        if (fRight > right) fRight = right;
114        if (fBottom > bottom) fBottom = bottom;
115        return true;
116    }
117    return false;
118}
119
120bool SkRect::intersect(const SkRect& r) {
121    SkASSERT(&r);
122    return this->intersect(r.fLeft, r.fTop, r.fRight, r.fBottom);
123}
124
125void SkRect::join(SkScalar left, SkScalar top, SkScalar right,
126                  SkScalar bottom) {
127    // do nothing if the params are empty
128    if (left >= right || top >= bottom) {
129        return;
130    }
131
132    // if we are empty, just assign
133    if (fLeft >= fRight || fTop >= fBottom) {
134        this->set(left, top, right, bottom);
135    } else {
136        if (left < fLeft) fLeft = left;
137        if (top < fTop) fTop = top;
138        if (right > fRight) fRight = right;
139        if (bottom > fBottom) fBottom = bottom;
140    }
141}
142
143