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