xref: /AOO41X/main/canvas/source/tools/surfacerect.hxx (revision 91c99ff47293b0d0832bde809a8978c8bfc37548)
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 INCLUDED_CANVAS_SURFACERECT_HXX
25 #define INCLUDED_CANVAS_SURFACERECT_HXX
26 
27 #include <basegfx/point/b2ipoint.hxx>
28 #include <basegfx/vector/b2isize.hxx>
29 
30 namespace canvas
31 {
32     //////////////////////////////////////////////////////////////////////////////////
33     // SurfaceRect
34     //////////////////////////////////////////////////////////////////////////////////
35 
36     struct SurfaceRect
37     {
38         ::basegfx::B2IPoint maPos;
39         ::basegfx::B2ISize  maSize;
40         ::basegfx::B2IPoint maBackup;
41         bool                bEnabled;
42 
SurfaceRectcanvas::SurfaceRect43         explicit SurfaceRect( const ::basegfx::B2ISize &rSize ) :
44             maPos(),
45             maSize(rSize),
46             maBackup(),
47             bEnabled(true)
48         {
49         }
50 
51         // coordinates contained in this rectangle are
52         // constrained to the following rules:
53         // 1) p.x >= pos.x
54         // 2) p.x <= pos.x+size.x
55         // 3) p.y >= pos.y
56         // 4) p.y <= pos.y+size.y
57         // in other words, 'size' means the number of pixels
58         // this rectangle encloses plus one. for example with pos[0,0]
59         // and size[512,512], p[512,512] would return inside.
60         // a size of [0,0] therefore denotes a one-by-one rectangle.
pointInsidecanvas::SurfaceRect61         bool pointInside( sal_Int32 px, sal_Int32 py ) const
62         {
63             const sal_Int32 x1(maPos.getX());
64             const sal_Int32 y1(maPos.getY());
65             const sal_Int32 x2(maPos.getX()+maSize.getX());
66             const sal_Int32 y2(maPos.getY()+maSize.getY());
67             if(px  < x1) return false;
68             if(px >= x2) return false;
69             if(py  < y1) return false;
70             if(py >= y2) return false;
71             return true;
72         }
73 
74         // returns true if the horizontal line intersects the rect.
hLineIntersectcanvas::SurfaceRect75         bool hLineIntersect( sal_Int32 lx1, sal_Int32 lx2, sal_Int32 ly ) const
76         {
77             const sal_Int32 x1(maPos.getX());
78             const sal_Int32 y1(maPos.getY());
79             const sal_Int32 x2(maPos.getX()+maSize.getX());
80             const sal_Int32 y2(maPos.getY()+maSize.getY());
81             if(ly < y1) return false;
82             if(ly >= y2) return false;
83             if((lx1 < x1) && (lx2 < x1)) return false;
84             if((lx1 >= x2) && (lx2 >= x2)) return false;
85             return true;
86         }
87 
88         //! Returns true if the vertical line intersects the rect.
vLineIntersectcanvas::SurfaceRect89         bool vLineIntersect( sal_Int32 lx, sal_Int32 ly1, sal_Int32 ly2 ) const
90         {
91             const sal_Int32 x1(maPos.getX());
92             const sal_Int32 y1(maPos.getY());
93             const sal_Int32 x2(maPos.getX()+maSize.getX());
94             const sal_Int32 y2(maPos.getY()+maSize.getY());
95             if(lx < x1) return false;
96             if(lx >= x2) return false;
97             if((ly1 < y1) && (ly2 < y1)) return false;
98             if((ly1 >= y2) && (ly2 >= y2)) return false;
99             return true;
100         }
101 
102         // returns true if the passed rect intersects this one.
intersectioncanvas::SurfaceRect103         bool intersection( const SurfaceRect& r ) const
104         {
105             const sal_Int32 x1(maPos.getX());
106             const sal_Int32 y1(maPos.getY());
107             const sal_Int32 x2(maPos.getX()+maSize.getX());
108             const sal_Int32 y2(maPos.getY()+maSize.getY());
109             if(r.hLineIntersect(x1,x2,y1)) return true;
110             if(r.hLineIntersect(x1,x2,y2)) return true;
111             if(r.vLineIntersect(x1,y1,y2)) return true;
112             if(r.vLineIntersect(x2,y1,y2)) return true;
113             return false;
114         }
115 
insidecanvas::SurfaceRect116         bool inside( const SurfaceRect& r ) const
117         {
118             const sal_Int32 x1(maPos.getX());
119             const sal_Int32 y1(maPos.getY());
120             const sal_Int32 x2(maPos.getX()+maSize.getX());
121             const sal_Int32 y2(maPos.getY()+maSize.getY());
122             if(!(r.pointInside(x1,y1))) return false;
123             if(!(r.pointInside(x2,y1))) return false;
124             if(!(r.pointInside(x2,y2))) return false;
125             if(!(r.pointInside(x1,y2))) return false;
126             return true;
127         }
128     };
129 }
130 
131 #endif
132