OLD | NEW |
| (Empty) |
1 // Copyright 2010 The Native Client Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can | |
3 // be found in the LICENSE file. | |
4 | |
5 #include "c_salt/rect.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 namespace c_salt { | |
10 | |
11 Rect::Rect(int left, int top, int right, int bottom) | |
12 : left_(left), top_(top), right_(right), bottom_(bottom) { | |
13 } | |
14 | |
15 Rect::Rect(int width, int height) | |
16 : left_(0), top_(0), right_(width), bottom_(height) { | |
17 } | |
18 | |
19 Rect::Rect(Size size) | |
20 : left_(0), top_(0), right_(size.width()), bottom_(size.height()) { | |
21 } | |
22 | |
23 bool Rect::Empty() const { | |
24 return (left_ == bottom_) && (top_ == bottom_); | |
25 } | |
26 | |
27 void Rect::Deflate(int sz) { | |
28 right_ = std::max(0, right_ - sz); | |
29 left_ = std::min(right_, left_ + sz); | |
30 bottom_ = std::max(0, bottom_ - sz); | |
31 top_ = std::min(bottom_, top_ + sz); | |
32 } | |
33 | |
34 void Rect::ShrinkToFit(const Rect& dest) { | |
35 double x_scale = 1; | |
36 double y_scale = 1; | |
37 bool need_to_shrink = false; | |
38 if (dest.width() == 0) { | |
39 right_ = left_; | |
40 } else if (dest.width() < width()) { | |
41 x_scale = static_cast<double>(dest.width()) / width(); | |
42 need_to_shrink = true; | |
43 } | |
44 if (dest.height() == 0) { | |
45 bottom_ = top_; | |
46 } else if (dest.height() < height()) { | |
47 y_scale = static_cast<double>(dest.height()) / height(); | |
48 need_to_shrink = true; | |
49 } | |
50 | |
51 if (need_to_shrink) { | |
52 double scale = std::min(x_scale, y_scale); | |
53 right_ = left_ + (width() * scale); | |
54 bottom_ = top_ + (height() * scale); | |
55 } | |
56 } | |
57 | |
58 void Rect::CenterIn(const Rect& dest) { | |
59 int center_x = (left_ + right_) / 2; | |
60 int center_y = (top_ + bottom_) / 2; | |
61 int dest_center_x = (dest.left_ + dest.right_) / 2; | |
62 int dest_center_y = (dest.top_ + dest.bottom_) / 2; | |
63 MoveBy(dest_center_x - center_x, dest_center_y - center_y); | |
64 } | |
65 | |
66 void Rect::MoveBy(int dx, int dy) { | |
67 left_ += dx; | |
68 right_ += dx; | |
69 top_ += dy; | |
70 bottom_ += dy; | |
71 } | |
72 } // namespace c_salt | |
73 | |
OLD | NEW |