1 /*
2  * Copyright (C) 2024 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 "rect.h"
18 
19 #include <algorithm>
20 #include <cmath>
21 
22 namespace pdfClient {
23 
IntPoint(const int x,const int y)24 Point_i IntPoint(const int x, const int y) {
25     return Point_i{x, y};
26 }
27 
DoublePoint(const double x,const double y)28 Point_d DoublePoint(const double x, const double y) {
29     return Point_d{x, y};
30 }
31 
IntRect(const int x1,const int y1,const int x2,const int y2)32 Rectangle_i IntRect(const int x1, const int y1, const int x2, const int y2) {
33     Rectangle_i output;
34     output.left = std::min(x1, x2);
35     output.top = std::min(y1, y2);
36     output.right = std::max(x1, x2);
37     output.bottom = std::max(y1, y2);
38     return output;
39 }
40 
IntRect(const Point_i & p1,const Point_i & p2)41 Rectangle_i IntRect(const Point_i& p1, const Point_i& p2) {
42     return IntRect(p1.x, p1.y, p2.x, p2.y);
43 }
44 
IntRectWithSize(const int left,const int top,const int width,const int height)45 Rectangle_i IntRectWithSize(const int left, const int top, const int width, const int height) {
46     return Rectangle_i{left, top, left + width, top + height};
47 }
48 
OuterIntRect(const Rectangle_d & input)49 Rectangle_i OuterIntRect(const Rectangle_d& input) {
50     return IntRect(floor(input.left), floor(input.top), ceil(input.right), ceil(input.bottom));
51 }
52 
DoubleRect(const double x1,const double y1,const double x2,const double y2)53 Rectangle_d DoubleRect(const double x1, const double y1, const double x2, const double y2) {
54     Rectangle_d output;
55     output.left = std::min(x1, x2);
56     output.top = std::min(y1, y2);
57     output.right = std::max(x1, x2);
58     output.bottom = std::max(y1, y2);
59     return output;
60 }
61 
Intersect(const Rectangle_i & lhs,const Rectangle_i & rhs)62 Rectangle_i Intersect(const Rectangle_i& lhs, const Rectangle_i& rhs) {
63     Rectangle_i output;
64     output.left = std::max(lhs.left, rhs.left);
65     output.top = std::max(lhs.top, rhs.top);
66     output.right = std::min(lhs.right, rhs.right);
67     output.bottom = std::min(lhs.bottom, rhs.bottom);
68     if (output.Width() < 0 || output.Height() < 0) {
69         return IntRect(0, 0, 0, 0);
70     }
71     return output;
72 }
73 
Intersect(const Rectangle_d & lhs,const Rectangle_d & rhs)74 Rectangle_d Intersect(const Rectangle_d& lhs, const Rectangle_d& rhs) {
75     Rectangle_d output;
76     output.left = std::max(lhs.left, rhs.left);
77     output.top = std::max(lhs.top, rhs.top);
78     output.right = std::min(lhs.right, rhs.right);
79     output.bottom = std::min(lhs.bottom, rhs.bottom);
80     if (output.Width() < 0 || output.Height() < 0) {
81         return DoubleRect(0, 0, 0, 0);
82     }
83     return output;
84 }
85 
Union(const Rectangle_i & lhs,const Rectangle_i & rhs)86 Rectangle_i Union(const Rectangle_i& lhs, const Rectangle_i& rhs) {
87     Rectangle_i output;
88     output.left = std::min(lhs.left, rhs.left);
89     output.top = std::min(lhs.top, rhs.top);
90     output.right = std::max(lhs.right, rhs.right);
91     output.bottom = std::max(lhs.bottom, rhs.bottom);
92     return output;
93 }
94 
Union(const Rectangle_d & lhs,const Rectangle_d & rhs)95 Rectangle_d Union(const Rectangle_d& lhs, const Rectangle_d& rhs) {
96     Rectangle_d output;
97     output.left = std::min(lhs.left, rhs.left);
98     output.top = std::min(lhs.top, rhs.top);
99     output.right = std::max(lhs.right, rhs.right);
100     output.bottom = std::max(lhs.bottom, rhs.bottom);
101     return output;
102 }
103 
104 }  // namespace pdfClient