xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/desktop_geometry.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "modules/desktop_capture/desktop_geometry.h"
12 
13 #include <algorithm>
14 #include <cmath>
15 
16 namespace webrtc {
17 
Contains(const DesktopVector & point) const18 bool DesktopRect::Contains(const DesktopVector& point) const {
19   return point.x() >= left() && point.x() < right() && point.y() >= top() &&
20          point.y() < bottom();
21 }
22 
ContainsRect(const DesktopRect & rect) const23 bool DesktopRect::ContainsRect(const DesktopRect& rect) const {
24   return rect.left() >= left() && rect.right() <= right() &&
25          rect.top() >= top() && rect.bottom() <= bottom();
26 }
27 
IntersectWith(const DesktopRect & rect)28 void DesktopRect::IntersectWith(const DesktopRect& rect) {
29   left_ = std::max(left(), rect.left());
30   top_ = std::max(top(), rect.top());
31   right_ = std::min(right(), rect.right());
32   bottom_ = std::min(bottom(), rect.bottom());
33   if (is_empty()) {
34     left_ = 0;
35     top_ = 0;
36     right_ = 0;
37     bottom_ = 0;
38   }
39 }
40 
UnionWith(const DesktopRect & rect)41 void DesktopRect::UnionWith(const DesktopRect& rect) {
42   if (is_empty()) {
43     *this = rect;
44     return;
45   }
46 
47   if (rect.is_empty()) {
48     return;
49   }
50 
51   left_ = std::min(left(), rect.left());
52   top_ = std::min(top(), rect.top());
53   right_ = std::max(right(), rect.right());
54   bottom_ = std::max(bottom(), rect.bottom());
55 }
56 
Translate(int32_t dx,int32_t dy)57 void DesktopRect::Translate(int32_t dx, int32_t dy) {
58   left_ += dx;
59   top_ += dy;
60   right_ += dx;
61   bottom_ += dy;
62 }
63 
Extend(int32_t left_offset,int32_t top_offset,int32_t right_offset,int32_t bottom_offset)64 void DesktopRect::Extend(int32_t left_offset,
65                          int32_t top_offset,
66                          int32_t right_offset,
67                          int32_t bottom_offset) {
68   left_ -= left_offset;
69   top_ -= top_offset;
70   right_ += right_offset;
71   bottom_ += bottom_offset;
72 }
73 
Scale(double horizontal,double vertical)74 void DesktopRect::Scale(double horizontal, double vertical) {
75   right_ += static_cast<int>(std::round(width() * (horizontal - 1)));
76   bottom_ += static_cast<int>(std::round(height() * (vertical - 1)));
77 }
78 
79 }  // namespace webrtc
80