2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
23 ==============================================================================
29 //==============================================================================
31 Represents a parallelogram that is defined by 3 points.
32 @see Rectangle, Point, Line
36 template <typename ValueType
>
40 //==============================================================================
41 /** Creates a parallelogram with zero size at the origin.
43 Parallelogram() = default;
45 /** Creates a copy of another parallelogram. */
46 Parallelogram (const Parallelogram
&) = default;
48 /** Creates a parallelogram based on 3 points. */
49 Parallelogram (Point
<ValueType
> topLeftPosition
,
50 Point
<ValueType
> topRightPosition
,
51 Point
<ValueType
> bottomLeftPosition
) noexcept
52 : topLeft (topLeftPosition
), topRight (topRightPosition
), bottomLeft (bottomLeftPosition
)
56 /** Creates a parallelogram from a rectangle. */
57 Parallelogram (Rectangle
<ValueType
> rectangle
) noexcept
58 : topLeft (rectangle
.getTopLeft()),
59 topRight (rectangle
.getTopRight()),
60 bottomLeft (rectangle
.getBottomLeft())
64 Parallelogram
& operator= (const Parallelogram
&) = default;
67 ~Parallelogram() = default;
69 //==============================================================================
70 /** Returns true if the parallelogram has a width or height of more than zero. */
71 bool isEmpty() const noexcept
{ return topLeft
!= topRight
|| topLeft
!= bottomLeft
; }
73 /** Returns true if the parallelogram's coordinates are all finite numbers, i.e. not NaN or infinity. */
74 inline bool isFinite() const noexcept
{ return topLeft
.isFinite() && topRight
.isFinite() && bottomLeft
.isFinite(); }
76 /** Returns the width of the parallelogram (i.e. the straight-line distance between the top-left and top-right. */
77 inline ValueType
getWidth() const noexcept
{ return Line
<ValueType
> (topLeft
, topRight
).getLength(); }
79 /** Returns the height of the parallelogram (i.e. the straight-line distance between the top-left and bottom-left. */
80 inline ValueType
getHeight() const noexcept
{ return Line
<ValueType
> (topLeft
, bottomLeft
).getLength(); }
82 //==============================================================================
83 /** Returns the parallelogram's top-left position as a Point. */
84 Point
<ValueType
> getTopLeft() const noexcept
{ return topLeft
; }
86 /** Returns the parallelogram's top-right position as a Point. */
87 Point
<ValueType
> getTopRight() const noexcept
{ return topRight
; }
89 /** Returns the parallelogram's bottom-left position as a Point. */
90 Point
<ValueType
> getBottomLeft() const noexcept
{ return bottomLeft
; }
92 /** Returns the parallelogram's bottom-right position as a Point. */
93 Point
<ValueType
> getBottomRight() const noexcept
{ return topRight
+ (bottomLeft
- topLeft
); }
95 //==============================================================================
96 /** Returns true if the two parallelograms are identical. */
97 bool operator== (const Parallelogram
& other
) const noexcept
{ return topLeft
== other
.topLeft
&& topRight
== other
.topRight
&& bottomLeft
== other
.bottomLeft
; }
99 /** Returns true if the two parallelograms are not identical. */
100 bool operator!= (const Parallelogram
& other
) const noexcept
{ return ! operator== (other
); }
102 //==============================================================================
103 /** Returns a parallelogram which is the same as this one moved by a given amount. */
104 Parallelogram
operator+ (Point
<ValueType
> deltaPosition
) const noexcept
111 /** Moves this parallelogram by a given amount. */
112 Parallelogram
& operator+= (Point
<ValueType
> deltaPosition
) noexcept
114 topLeft
+= deltaPosition
;
115 topRight
+= deltaPosition
;
116 bottomLeft
+= deltaPosition
;
120 /** Returns a parallelogram which is the same as this one moved by a given amount. */
121 Parallelogram
operator- (Point
<ValueType
> deltaPosition
) const noexcept
123 return operator+ (-deltaPosition
);
126 /** Moves this parallelogram by a given amount. */
127 Parallelogram
& operator-= (Point
<ValueType
> deltaPosition
) noexcept
129 return operator-= (-deltaPosition
);
132 /** Returns a parallelogram that has been scaled by the given amount, centred around the origin. */
133 template <typename PointOrScalarType
>
134 Parallelogram
operator* (PointOrScalarType scaleFactor
) const noexcept
141 /** Scales this parallelogram by the given amount, centred around the origin. */
142 template <typename PointOrScalarType
>
143 Parallelogram
operator*= (PointOrScalarType scaleFactor
) noexcept
145 topLeft
*= scaleFactor
;
146 topRight
*= scaleFactor
;
147 bottomLeft
*= scaleFactor
;
151 //==============================================================================
152 /** Returns a point within this parallelogram, specified as proportional coordinates.
153 The relative X and Y values should be between 0 and 1, where 0 is the left or
154 top of this parallelogram, and 1 is the right or bottom. (Out-of-bounds values
155 will return a point outside the parallelogram).
157 Point
<ValueType
> getRelativePoint (Point
<ValueType
> relativePosition
) const noexcept
160 + (topRight
- topLeft
) * relativePosition
.x
161 + (bottomLeft
- topLeft
) * relativePosition
.y
;
164 /** Returns a transformed version of the parallelogram. */
165 Parallelogram
transformedBy (const AffineTransform
& transform
) const noexcept
168 transform
.transformPoints (p
.topLeft
.x
, p
.topLeft
.y
,
169 p
.topRight
.x
, p
.topRight
.y
,
170 p
.bottomLeft
.x
, p
.bottomLeft
.y
);
175 /** Returns the smallest rectangle that encloses this parallelogram. */
176 Rectangle
<ValueType
> getBoundingBox() const noexcept
178 const Point
<ValueType
> points
[] = { topLeft
, topRight
, bottomLeft
, getBottomRight() };
179 return Rectangle
<ValueType
>::findAreaContainingPoints (points
, 4);
182 Point
<ValueType
> topLeft
, topRight
, bottomLeft
;