xref: /trunk/main/canvas/source/tools/surfacerect.hxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef INCLUDED_CANVAS_SURFACERECT_HXX
29 #define INCLUDED_CANVAS_SURFACERECT_HXX
30 
31 #include <basegfx/point/b2ipoint.hxx>
32 #include <basegfx/vector/b2isize.hxx>
33 
34 namespace canvas
35 {
36     //////////////////////////////////////////////////////////////////////////////////
37     // SurfaceRect
38     //////////////////////////////////////////////////////////////////////////////////
39 
40     struct SurfaceRect
41     {
42         ::basegfx::B2IPoint maPos;
43         ::basegfx::B2ISize  maSize;
44         ::basegfx::B2IPoint maBackup;
45         bool                bEnabled;
46 
47         explicit SurfaceRect( const ::basegfx::B2ISize &rSize ) :
48             maPos(),
49             maSize(rSize),
50             maBackup(),
51             bEnabled(true)
52         {
53         }
54 
55         // coordinates contained in this rectangle are
56         // constrained to the following rules:
57         // 1) p.x >= pos.x
58         // 2) p.x <= pos.x+size.x
59         // 3) p.y >= pos.y
60         // 4) p.y <= pos.y+size.y
61         // in other words, 'size' means the number of pixels
62         // this rectangle encloses plus one. for example with pos[0,0]
63         // and size[512,512], p[512,512] would return inside.
64         // a size of [0,0] therefore denotes a one-by-one rectangle.
65         bool pointInside( sal_Int32 px, sal_Int32 py ) const
66         {
67             const sal_Int32 x1(maPos.getX());
68             const sal_Int32 y1(maPos.getY());
69             const sal_Int32 x2(maPos.getX()+maSize.getX());
70             const sal_Int32 y2(maPos.getY()+maSize.getY());
71             if(px  < x1) return false;
72             if(px >= x2) return false;
73             if(py  < y1) return false;
74             if(py >= y2) return false;
75             return true;
76         }
77 
78         // returns true if the horizontal line intersects the rect.
79         bool hLineIntersect( sal_Int32 lx1, sal_Int32 lx2, sal_Int32 ly ) const
80         {
81             const sal_Int32 x1(maPos.getX());
82             const sal_Int32 y1(maPos.getY());
83             const sal_Int32 x2(maPos.getX()+maSize.getX());
84             const sal_Int32 y2(maPos.getY()+maSize.getY());
85             if(ly < y1) return false;
86             if(ly >= y2) return false;
87             if((lx1 < x1) && (lx2 < x1)) return false;
88             if((lx1 >= x2) && (lx2 >= x2)) return false;
89             return true;
90         }
91 
92         //! Returns true if the vertical line intersects the rect.
93         bool vLineIntersect( sal_Int32 lx, sal_Int32 ly1, sal_Int32 ly2 ) const
94         {
95             const sal_Int32 x1(maPos.getX());
96             const sal_Int32 y1(maPos.getY());
97             const sal_Int32 x2(maPos.getX()+maSize.getX());
98             const sal_Int32 y2(maPos.getY()+maSize.getY());
99             if(lx < x1) return false;
100             if(lx >= x2) return false;
101             if((ly1 < y1) && (ly2 < y1)) return false;
102             if((ly1 >= y2) && (ly2 >= y2)) return false;
103             return true;
104         }
105 
106         // returns true if the passed rect intersects this one.
107         bool intersection( const SurfaceRect& r ) const
108         {
109             const sal_Int32 x1(maPos.getX());
110             const sal_Int32 y1(maPos.getY());
111             const sal_Int32 x2(maPos.getX()+maSize.getX());
112             const sal_Int32 y2(maPos.getY()+maSize.getY());
113             if(r.hLineIntersect(x1,x2,y1)) return true;
114             if(r.hLineIntersect(x1,x2,y2)) return true;
115             if(r.vLineIntersect(x1,y1,y2)) return true;
116             if(r.vLineIntersect(x2,y1,y2)) return true;
117             return false;
118         }
119 
120         bool inside( const SurfaceRect& r ) const
121         {
122             const sal_Int32 x1(maPos.getX());
123             const sal_Int32 y1(maPos.getY());
124             const sal_Int32 x2(maPos.getX()+maSize.getX());
125             const sal_Int32 y2(maPos.getY()+maSize.getY());
126             if(!(r.pointInside(x1,y1))) return false;
127             if(!(r.pointInside(x2,y1))) return false;
128             if(!(r.pointInside(x2,y2))) return false;
129             if(!(r.pointInside(x1,y2))) return false;
130             return true;
131         }
132     };
133 }
134 
135 #endif
136