xref: /AOO41X/main/canvas/source/tools/surfacerect.hxx (revision 91c99ff47293b0d0832bde809a8978c8bfc37548)
1*91c99ff4SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*91c99ff4SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*91c99ff4SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*91c99ff4SAndrew Rist  * distributed with this work for additional information
6*91c99ff4SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*91c99ff4SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*91c99ff4SAndrew Rist  * "License"); you may not use this file except in compliance
9*91c99ff4SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*91c99ff4SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*91c99ff4SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*91c99ff4SAndrew Rist  * software distributed under the License is distributed on an
15*91c99ff4SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*91c99ff4SAndrew Rist  * KIND, either express or implied.  See the License for the
17*91c99ff4SAndrew Rist  * specific language governing permissions and limitations
18*91c99ff4SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*91c99ff4SAndrew Rist  *************************************************************/
21*91c99ff4SAndrew Rist 
22*91c99ff4SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef INCLUDED_CANVAS_SURFACERECT_HXX
25cdf0e10cSrcweir #define INCLUDED_CANVAS_SURFACERECT_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <basegfx/point/b2ipoint.hxx>
28cdf0e10cSrcweir #include <basegfx/vector/b2isize.hxx>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir namespace canvas
31cdf0e10cSrcweir {
32cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
33cdf0e10cSrcweir 	// SurfaceRect
34cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
35cdf0e10cSrcweir 
36cdf0e10cSrcweir 	struct SurfaceRect
37cdf0e10cSrcweir 	{
38cdf0e10cSrcweir 		::basegfx::B2IPoint maPos;
39cdf0e10cSrcweir 		::basegfx::B2ISize  maSize;
40cdf0e10cSrcweir 		::basegfx::B2IPoint maBackup;
41cdf0e10cSrcweir 		bool                bEnabled;
42cdf0e10cSrcweir 
SurfaceRectcanvas::SurfaceRect43cdf0e10cSrcweir 		explicit SurfaceRect( const ::basegfx::B2ISize &rSize ) :
44cdf0e10cSrcweir 			maPos(),
45cdf0e10cSrcweir 			maSize(rSize),
46cdf0e10cSrcweir 			maBackup(),
47cdf0e10cSrcweir 			bEnabled(true)
48cdf0e10cSrcweir 		{
49cdf0e10cSrcweir 		}
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 		// coordinates contained in this rectangle are
52cdf0e10cSrcweir 		// constrained to the following rules:
53cdf0e10cSrcweir 		// 1) p.x >= pos.x
54cdf0e10cSrcweir 		// 2) p.x <= pos.x+size.x
55cdf0e10cSrcweir 		// 3) p.y >= pos.y
56cdf0e10cSrcweir 		// 4) p.y <= pos.y+size.y
57cdf0e10cSrcweir 		// in other words, 'size' means the number of pixels
58cdf0e10cSrcweir 		// this rectangle encloses plus one. for example with pos[0,0]
59cdf0e10cSrcweir 		// and size[512,512], p[512,512] would return inside.
60cdf0e10cSrcweir 		// a size of [0,0] therefore denotes a one-by-one rectangle.
pointInsidecanvas::SurfaceRect61cdf0e10cSrcweir 		bool pointInside( sal_Int32 px, sal_Int32 py ) const
62cdf0e10cSrcweir 		{
63cdf0e10cSrcweir 			const sal_Int32 x1(maPos.getX());
64cdf0e10cSrcweir 			const sal_Int32 y1(maPos.getY());
65cdf0e10cSrcweir 			const sal_Int32 x2(maPos.getX()+maSize.getX());
66cdf0e10cSrcweir 			const sal_Int32 y2(maPos.getY()+maSize.getY());
67cdf0e10cSrcweir 			if(px  < x1) return false;
68cdf0e10cSrcweir 			if(px >= x2) return false;
69cdf0e10cSrcweir 			if(py  < y1) return false;
70cdf0e10cSrcweir 			if(py >= y2) return false;
71cdf0e10cSrcweir 			return true;
72cdf0e10cSrcweir 		}
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 		// returns true if the horizontal line intersects the rect.
hLineIntersectcanvas::SurfaceRect75cdf0e10cSrcweir 		bool hLineIntersect( sal_Int32 lx1, sal_Int32 lx2, sal_Int32 ly ) const
76cdf0e10cSrcweir 		{
77cdf0e10cSrcweir 			const sal_Int32 x1(maPos.getX());
78cdf0e10cSrcweir 			const sal_Int32 y1(maPos.getY());
79cdf0e10cSrcweir 			const sal_Int32 x2(maPos.getX()+maSize.getX());
80cdf0e10cSrcweir 			const sal_Int32 y2(maPos.getY()+maSize.getY());
81cdf0e10cSrcweir 			if(ly < y1) return false;
82cdf0e10cSrcweir 			if(ly >= y2) return false;
83cdf0e10cSrcweir 			if((lx1 < x1) && (lx2 < x1)) return false;
84cdf0e10cSrcweir 			if((lx1 >= x2) && (lx2 >= x2)) return false;
85cdf0e10cSrcweir 			return true;
86cdf0e10cSrcweir 		}
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 		//! Returns true if the vertical line intersects the rect.
vLineIntersectcanvas::SurfaceRect89cdf0e10cSrcweir 		bool vLineIntersect( sal_Int32 lx, sal_Int32 ly1, sal_Int32 ly2 ) const
90cdf0e10cSrcweir 		{
91cdf0e10cSrcweir 			const sal_Int32 x1(maPos.getX());
92cdf0e10cSrcweir 			const sal_Int32 y1(maPos.getY());
93cdf0e10cSrcweir 			const sal_Int32 x2(maPos.getX()+maSize.getX());
94cdf0e10cSrcweir 			const sal_Int32 y2(maPos.getY()+maSize.getY());
95cdf0e10cSrcweir 			if(lx < x1) return false;
96cdf0e10cSrcweir 			if(lx >= x2) return false;
97cdf0e10cSrcweir 			if((ly1 < y1) && (ly2 < y1)) return false;
98cdf0e10cSrcweir 			if((ly1 >= y2) && (ly2 >= y2)) return false;
99cdf0e10cSrcweir 			return true;
100cdf0e10cSrcweir 		}
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 		// returns true if the passed rect intersects this one.
intersectioncanvas::SurfaceRect103cdf0e10cSrcweir 		bool intersection( const SurfaceRect& r ) const
104cdf0e10cSrcweir 		{
105cdf0e10cSrcweir 			const sal_Int32 x1(maPos.getX());
106cdf0e10cSrcweir 			const sal_Int32 y1(maPos.getY());
107cdf0e10cSrcweir 			const sal_Int32 x2(maPos.getX()+maSize.getX());
108cdf0e10cSrcweir 			const sal_Int32 y2(maPos.getY()+maSize.getY());
109cdf0e10cSrcweir 			if(r.hLineIntersect(x1,x2,y1)) return true;
110cdf0e10cSrcweir 			if(r.hLineIntersect(x1,x2,y2)) return true;
111cdf0e10cSrcweir 			if(r.vLineIntersect(x1,y1,y2)) return true;
112cdf0e10cSrcweir 			if(r.vLineIntersect(x2,y1,y2)) return true;
113cdf0e10cSrcweir 			return false;
114cdf0e10cSrcweir 		}
115cdf0e10cSrcweir 
insidecanvas::SurfaceRect116cdf0e10cSrcweir 		bool inside( const SurfaceRect& r ) const
117cdf0e10cSrcweir 		{
118cdf0e10cSrcweir 			const sal_Int32 x1(maPos.getX());
119cdf0e10cSrcweir 			const sal_Int32 y1(maPos.getY());
120cdf0e10cSrcweir 			const sal_Int32 x2(maPos.getX()+maSize.getX());
121cdf0e10cSrcweir 			const sal_Int32 y2(maPos.getY()+maSize.getY());
122cdf0e10cSrcweir 			if(!(r.pointInside(x1,y1))) return false;
123cdf0e10cSrcweir 			if(!(r.pointInside(x2,y1))) return false;
124cdf0e10cSrcweir 			if(!(r.pointInside(x2,y2))) return false;
125cdf0e10cSrcweir 			if(!(r.pointInside(x1,y2))) return false;
126cdf0e10cSrcweir 			return true;
127cdf0e10cSrcweir 		}
128cdf0e10cSrcweir 	};
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir #endif
132