Easy3D 2.5.3
rect.h
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_CORE_RECTANGLE_H
28#define EASY3D_CORE_RECTANGLE_H
29
30#include <algorithm>
31
32#include <easy3d/core/vec.h>
33
34
35namespace easy3d {
36
40
41 template <typename FT>
43 public:
45 GenericRect(const Vec<2, FT>& p, const Vec<2, FT>& q) {
46 x_min_ = std::min(q.x, p.x);
47 x_max_ = std::max(q.x, p.x);
48 y_min_ = std::min(q.y, p.y);
49 y_max_ = std::max(q.y, p.y);
50
51 }
52
55 GenericRect(FT xmin, FT xmax, FT ymin, FT ymax)
56 : x_min_(std::min(xmin, xmax))
57 , y_min_(std::min(ymin, ymax))
58 , x_max_(std::max(xmin, xmax))
59 , y_max_(std::max(ymin, ymax))
60 {
61 }
62
63 FT& x_min() { return x_min_; }
64 FT& y_min() { return y_min_; }
65 FT& x_max() { return x_max_; }
66 FT& y_max() { return y_max_; }
67 FT x_min() const { return x_min_; }
68 FT y_min() const { return y_min_; }
69 FT x_max() const { return x_max_; }
70 FT y_max() const { return y_max_; }
71
72 FT& x() { return x_min_; }
73 FT& y() { return y_min_; }
74 FT x() const { return x_min_; }
75 FT y() const { return y_min_; }
76
78 FT width() const { return x_max() - x_min(); }
80 FT height() const { return y_max() - y_min(); }
81
82 FT& left() { return x_min_; }
83 FT& top() { return y_min_; }
84 FT& right() { return x_max_; }
85 FT& bottom() { return y_max_; }
86 FT left() const { return x_min_; }
87 FT top() const { return y_min_; }
88 FT right() const { return x_max_; }
89 FT bottom() const { return y_max_; }
90
92 Vec<2, FT> top_left() const { return Vec<2, FT>(x_min_, y_min_); }
94 Vec<2, FT> bottom_right() const { return Vec<2, FT>(x_max_, y_max_); }
96 Vec<2, FT> top_right() const { return Vec<2, FT>(x_max_, y_min_); }
98 Vec<2, FT> bottom_left() const { return Vec<2, FT>(x_min_, y_max_); }
99
100 Vec<2, FT> min_point() const { return Vec<2, FT>(x_min_, y_min_); }
101 Vec<2, FT> max_point() const { return Vec<2, FT>(x_max_, y_max_); }
102
105 return Vec<2, FT>(FT(0.5) * (x_max() + x_min()), FT(0.5) * (y_max() + y_min()));
106 }
107
108 private:
109 FT x_min_;
110 FT y_min_;
111 FT x_max_;
112 FT y_max_;
113 };
114
115}
116
117
118#endif // EASY3D_CORE_RECTANGLE_H
The GenericRect class defines a rectangle in the 2D space.
Definition: rect.h:42
FT height() const
Returns the height of the rectangle.
Definition: rect.h:80
FT width() const
Returns the width of the rectangle.
Definition: rect.h:78
Vec< 2, FT > top_right() const
Returns the position of the rectangle's top-right corner.
Definition: rect.h:96
GenericRect(const Vec< 2, FT > &p, const Vec< 2, FT > &q)
Constructs a rectangle from two points p and q.
Definition: rect.h:45
Vec< 2, FT > top_left() const
Returns the position of the rectangle's top-left corner.
Definition: rect.h:92
Vec< 2, FT > bottom_right() const
Returns the position of the rectangle's bottom-right corner.
Definition: rect.h:94
Vec< 2, FT > center() const
Returns the center point of the rectangle.
Definition: rect.h:104
Vec< 2, FT > bottom_left() const
Returns the position of the rectangle's bottom-left corner.
Definition: rect.h:98
GenericRect(FT xmin, FT xmax, FT ymin, FT ymax)
Constructs a rectangle from its min coordinates (xmin and ymin) and max coordinates (xmax and ymax).
Definition: rect.h:55
Base class for vector types. It provides generic functionality for N dimensional vectors.
Definition: vec.h:34
Definition: collider.cpp:182
FT max()
Function returning maximum representable value for a given type.
FT min()
Function returning minimum representable value for a given type.