File size: 5,787 Bytes
a5ffdcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2015 R. van Twisk (librecad@rvt.dds.nl)
** Copyright (C) 2015 Dongxu Li (dongxuli2011@gmail.com)
** Copyright (C) 2015 librecad.org (www.librecad.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**********************************************************************/

#ifndef LC_RECT_H
#define LC_RECT_H
#include <array>

#include "rs_vector.h"

//ported from LibreCAD V3
namespace lc {
namespace geo {

typedef RS_Vector Coordinate;

/**
		  * Class that describes an area or window.
		  */
class Area {

public:
	Area();
	/**
		  * Create a new Area. The coordinates coordA and coordB will be ordered so that minP will always be < maxP
		  * The coordinates are not allowed to describe a volume
		  *
		  * @param CoordA First coordinate of an area
		  * @param CoordB Second coordinate of an area
		  */
	Area(const Coordinate& coordA, const Coordinate& coordB);


	/**
		 * @brief Area
		 * given at a coordinate with a given width and height
		 * @param coordA
		 * @param width
		 * @param height
		 */
	explicit Area(const Coordinate& coord, double width, double height);

        /**
         * @brief empty(): whether the area is empty
         * @return true, if at least one of the min-x/y coordinates is larger or equal the corresponding x/y coordinates.
         */
        bool isEmpty(double tolerance) const;

	/**
                  * Return the smallest corner
		  */
	const Coordinate& minP() const;

	/**
		  * Return the highest corner
		  */
	const Coordinate& maxP() const;
	/**
		 * @brief topLeftCorner return the upperLeftCorner coordinates
		 * _minP is considered lowerLeft, _maxP is the upperRight
		 * @return {_minP.x, _maxP.y}
		 */
	Coordinate upperLeftCorner() const;
	Coordinate upperRightCorner() const;
	/**
		 * @brief lowerRightCorner return the lowerRight coordinates
		 * _minP is considered lowerLeft, _maxP is the upperRight
		 * @return {_maxP.x, _minP.y}
		 */
	Coordinate lowerLeftCorner() const;
	Coordinate lowerRightCorner() const;

	/**
		 * @brief width
		 * Returns the width of this area
		 * @return
		 */
	double width() const;

	/**
		 * @brief height
		 * Returns the height of this area
		 * @return
		 */
	double height() const;

	/**
		  * @brief Test of a specific point lies within an area
		  * @param point Point to test against
		  * @return boolean true of the point is within the area
		  */
	bool inArea(const Coordinate& point, double tolerance = 0.) const;

	/**
		 * @brief inArea
		 * test if this object's fit's fully in area
		 * @param area
		 * @return
		 */
	bool inArea(const Area& area) const;

	/**
		 * @brief overlaps
		 * returns true if any overlap is happening between the two area's, even if otherArea fit's within this area
		 * @param other
		 * @return
		 */
	bool overlaps(const Area& otherArea) const;

	/**
		 * @brief numCornersInside
		 * count the number of corners this object has in otherArea
		 * @param other
		 * @return
		 */
	short numCornersInside(const Area& otherArea) const;

	/**
		 * @brief merge
		 * two area's and expand if required to largest containing area
		 * @param other
		 * @return
		 */
	Area merge(const Area& other) const;

	/**
		  * @brief merge
		  * two area's and expand if required to largest containing area
		  * @param other
		  * @return
		  */
	Area merge(const Coordinate& other) const;

	/**
         * @brief intersection
         * the intersection area of two rectangular areas
		 * @param other
		 * @param tolerance, tolerance to detect zero size intersection
         * @return the rectangular intersection of two areas
		 */
	Area intersection(const Area& other, double tolerance = 0.) const;

	/**
	 * @brief intersects whether two rectangular area
	 * if the closest distance between two Areas is smaller than tolerance, they
	 * are considered to have intersection
	 * @param rhs the other rect
	 * @return true if closest distance is smaller than or equal to tolerance
	 */
	bool intersects(Area const& rhs, double tolerance = 0.) const;

	/**
		 * @brief top
		 * vector of this area
		 * @return
		 */
	Coordinate top() const;

	/**
		 * @brief bottom
		 * vector of this area
		 * @return
		 */
	Coordinate bottom() const;

	/**
		 * @brief left
		 * vector for this area
		 * @return
		 */
	Coordinate left() const;

	/**
		 * @brief right
		 * vector of this area
		 * @return
		 */
	Coordinate right() const;

	/**
		 * Increase the area on each side by increaseBy
		 */
	Area increaseBy (double increaseBy) const;
	/**
	 * @brief vertices generate vertices of the rectangular area
	 * @return array of vertices by the order {ll, lr, ur, rl} starting with
	 * lower-left corner, i.e. _minP
	 */
	std::array<Coordinate, 4> vertices() const;

	static void unitTest();

private:
	static Coordinate Vector(Coordinate const& p, Coordinate const& q);
	friend std::ostream& operator<<(std::ostream& os, const Area& area);

private:
    Coordinate _minP{};
    Coordinate _maxP{};
};
}
}

using LC_Rect = lc::geo::Area;
#endif // LC_RECT_H