xref: /AOO41X/main/basegfx/inc/basegfx/range/basicbox.hxx (revision ce9c7ef7bd056b6da7d6eeebb749fbf2160d647b)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _BGFX_RANGE_BASICBOX_HXX
25 #define _BGFX_RANGE_BASICBOX_HXX
26 
27 #include <basegfx/range/basicrange.hxx>
28 
29 
30 namespace basegfx
31 {
32     /** Specialization of BasicRange, handling the inside predicates
33         differently.
34 
35         This template considers the rightmost and bottommost border as
36         <em>outside</em> of the range, in contrast to BasicRange,
37         which considers them inside.
38      */
39     class BasicBox : public BasicRange< sal_Int32, Int32Traits >
40     {
41         typedef BasicRange< sal_Int32, Int32Traits > Base;
42     public:
BasicBox()43         BasicBox() :
44             Base()
45         {
46         }
47 
BasicBox(sal_Int32 nValue)48         BasicBox( sal_Int32 nValue ) :
49             Base( nValue )
50         {
51         }
52 
BasicBox(const BasicBox & rBox)53         BasicBox(const BasicBox& rBox) :
54             Base( rBox )
55         {
56         }
57 
getCenter() const58         double getCenter() const
59         {
60             if(isEmpty())
61             {
62                 return 0.0;
63             }
64             else
65             {
66                 return ((mnMaximum + mnMinimum - 1.0) / 2.0);
67             }
68         }
69 
70         using Base::isInside;
71 
isInside(sal_Int32 nValue) const72         bool isInside(sal_Int32 nValue) const
73         {
74             if(isEmpty())
75             {
76                 return false;
77             }
78             else
79             {
80                 return (nValue >= mnMinimum) && (nValue < mnMaximum);
81             }
82         }
83 
84         using Base::overlaps;
85 
overlaps(const BasicBox & rBox) const86         bool overlaps(const BasicBox& rBox) const
87         {
88             if(isEmpty())
89             {
90                 return false;
91             }
92             else
93             {
94                 if(rBox.isEmpty())
95                 {
96                     return false;
97                 }
98                 else
99                 {
100                     return !((rBox.mnMaximum <= mnMinimum) || (rBox.mnMinimum >= mnMaximum));
101                 }
102             }
103         }
104 
grow(sal_Int32 nValue)105         void grow(sal_Int32 nValue)
106         {
107             if(!isEmpty())
108             {
109                 bool bLessThanZero(nValue < 0);
110 
111                 if(nValue > 0 || bLessThanZero)
112                 {
113                     mnMinimum -= nValue;
114                     mnMaximum += nValue;
115 
116                     if(bLessThanZero)
117                     {
118                         // test if range did collapse
119                         if(mnMinimum > mnMaximum)
120                         {
121                             // if yes, collapse to center
122                             mnMinimum = mnMaximum = ((mnMaximum + mnMinimum - 1) / 2);
123                         }
124                     }
125                 }
126             }
127         }
128     };
129 
130 } // end of namespace basegfx
131 
132 #endif /* _BGFX_RANGE_BASICBOX_HXX */
133