blob: 5e3d93c339f4ffa81353b404219b01110b931fa4 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkRect.h"
11
reed@google.com91a2ae92011-02-08 21:59:59 +000012void SkIRect::join(int32_t left, int32_t top, int32_t right, int32_t bottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000013 // do nothing if the params are empty
reed@google.com91a2ae92011-02-08 21:59:59 +000014 if (left >= right || top >= bottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000015 return;
reed@google.com91a2ae92011-02-08 21:59:59 +000016 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000017
18 // if we are empty, just assign
reed@google.com91a2ae92011-02-08 21:59:59 +000019 if (fLeft >= fRight || fTop >= fBottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000020 this->set(left, top, right, bottom);
reed@google.com91a2ae92011-02-08 21:59:59 +000021 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000022 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
reed@google.com91a2ae92011-02-08 21:59:59 +000029void SkIRect::sort() {
30 if (fLeft > fRight) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 SkTSwap<int32_t>(fLeft, fRight);
reed@google.com91a2ae92011-02-08 21:59:59 +000032 }
33 if (fTop > fBottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 SkTSwap<int32_t>(fTop, fBottom);
reed@google.com91a2ae92011-02-08 21:59:59 +000035 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000036}
37
38/////////////////////////////////////////////////////////////////////////////
39
reed@google.com91a2ae92011-02-08 21:59:59 +000040void SkRect::sort() {
41 if (fLeft > fRight) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 SkTSwap<SkScalar>(fLeft, fRight);
reed@google.com91a2ae92011-02-08 21:59:59 +000043 }
44 if (fTop > fBottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 SkTSwap<SkScalar>(fTop, fBottom);
reed@google.com91a2ae92011-02-08 21:59:59 +000046 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000047}
48
reed@google.com91a2ae92011-02-08 21:59:59 +000049void SkRect::toQuad(SkPoint quad[4]) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 SkASSERT(quad);
51
52 quad[0].set(fLeft, fTop);
53 quad[1].set(fRight, fTop);
54 quad[2].set(fRight, fBottom);
55 quad[3].set(fLeft, fBottom);
56}
57
reed@google.comfe701122011-11-08 19:41:23 +000058#ifdef SK_SCALAR_IS_FLOAT
59 #define SkFLOATCODE(code) code
60#else
61 #define SkFLOATCODE(code)
62#endif
63
reed@google.com91a2ae92011-02-08 21:59:59 +000064void SkRect::set(const SkPoint pts[], int count) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 SkASSERT((pts && count > 0) || count == 0);
66
67 if (count <= 0) {
reed@android.com4516f472009-06-29 16:25:36 +000068 sk_bzero(this, sizeof(SkRect));
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 } else {
70#ifdef SK_SCALAR_SLOW_COMPARES
71 int32_t l, t, r, b;
reed@google.com077910e2011-02-08 21:56:39 +000072
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 l = r = SkScalarAs2sCompliment(pts[0].fX);
74 t = b = SkScalarAs2sCompliment(pts[0].fY);
reed@google.com077910e2011-02-08 21:56:39 +000075
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 for (int i = 1; i < count; i++) {
77 int32_t x = SkScalarAs2sCompliment(pts[i].fX);
78 int32_t y = SkScalarAs2sCompliment(pts[i].fY);
reed@google.com077910e2011-02-08 21:56:39 +000079
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 if (x < l) l = x; else if (x > r) r = x;
81 if (y < t) t = y; else if (y > b) b = y;
82 }
83 this->set(Sk2sComplimentAsScalar(l),
84 Sk2sComplimentAsScalar(t),
85 Sk2sComplimentAsScalar(r),
86 Sk2sComplimentAsScalar(b));
87#else
88 SkScalar l, t, r, b;
reed@google.comfe701122011-11-08 19:41:23 +000089 SkFLOATCODE(int isNaN;)
reed@android.com8a1c16f2008-12-17 15:59:43 +000090
91 l = r = pts[0].fX;
92 t = b = pts[0].fY;
reed@google.comfe701122011-11-08 19:41:23 +000093 SkFLOATCODE(isNaN = (l != l) | (t != t);)
reed@android.com8a1c16f2008-12-17 15:59:43 +000094
95 for (int i = 1; i < count; i++) {
96 SkScalar x = pts[i].fX;
97 SkScalar y = pts[i].fY;
reed@google.comfe701122011-11-08 19:41:23 +000098 SkFLOATCODE(isNaN |= (x != x) | (y != y);)
reed@android.com8a1c16f2008-12-17 15:59:43 +000099
100 if (x < l) l = x; else if (x > r) r = x;
101 if (y < t) t = y; else if (y > b) b = y;
102 }
reed@google.comfe701122011-11-08 19:41:23 +0000103
104#ifdef SK_SCALAR_IS_FLOAT
105 if (isNaN) {
106 l = t = r = b = 0;
107 }
108#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 this->set(l, t, r, b);
110#endif
111 }
112}
113
reed@google.com91a2ae92011-02-08 21:59:59 +0000114bool SkRect::intersect(SkScalar left, SkScalar top, SkScalar right,
115 SkScalar bottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 if (left < right && top < bottom && !this->isEmpty() && // check for empties
117 fLeft < right && left < fRight && fTop < bottom && top < fBottom)
118 {
119 if (fLeft < left) fLeft = left;
120 if (fTop < top) fTop = top;
121 if (fRight > right) fRight = right;
122 if (fBottom > bottom) fBottom = bottom;
123 return true;
124 }
125 return false;
126}
127
reed@google.com91a2ae92011-02-08 21:59:59 +0000128bool SkRect::intersect(const SkRect& r) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 SkASSERT(&r);
130 return this->intersect(r.fLeft, r.fTop, r.fRight, r.fBottom);
131}
132
reed@google.comf0f617a2011-10-21 14:05:06 +0000133bool SkRect::intersect(const SkRect& a, const SkRect& b) {
134 SkASSERT(&a && &b);
135
136 if (!a.isEmpty() && !b.isEmpty() &&
137 a.fLeft < b.fRight && b.fLeft < a.fRight &&
138 a.fTop < b.fBottom && b.fTop < a.fBottom) {
139 fLeft = SkMaxScalar(a.fLeft, b.fLeft);
140 fTop = SkMaxScalar(a.fTop, b.fTop);
141 fRight = SkMinScalar(a.fRight, b.fRight);
142 fBottom = SkMinScalar(a.fBottom, b.fBottom);
143 return true;
144 }
145 return false;
146}
147
reed@google.com91a2ae92011-02-08 21:59:59 +0000148void SkRect::join(SkScalar left, SkScalar top, SkScalar right,
149 SkScalar bottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 // do nothing if the params are empty
reed@google.com91a2ae92011-02-08 21:59:59 +0000151 if (left >= right || top >= bottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 return;
reed@google.com91a2ae92011-02-08 21:59:59 +0000153 }
reed@google.com077910e2011-02-08 21:56:39 +0000154
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 // if we are empty, just assign
reed@google.com91a2ae92011-02-08 21:59:59 +0000156 if (fLeft >= fRight || fTop >= fBottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 this->set(left, top, right, bottom);
reed@google.com91a2ae92011-02-08 21:59:59 +0000158 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 if (left < fLeft) fLeft = left;
160 if (top < fTop) fTop = top;
161 if (right > fRight) fRight = right;
162 if (bottom > fBottom) fBottom = bottom;
163 }
164}
165