1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: surfacerect.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef INCLUDED_CANVAS_SURFACERECT_HXX
32 #define INCLUDED_CANVAS_SURFACERECT_HXX
34 #include <basegfx/point/b2ipoint.hxx>
35 #include <basegfx/vector/b2isize.hxx>
39 //////////////////////////////////////////////////////////////////////////////////
41 //////////////////////////////////////////////////////////////////////////////////
45 ::basegfx::B2IPoint maPos
;
46 ::basegfx::B2ISize maSize
;
47 ::basegfx::B2IPoint maBackup
;
50 explicit SurfaceRect( const ::basegfx::B2ISize
&rSize
) :
58 // coordinates contained in this rectangle are
59 // constrained to the following rules:
61 // 2) p.x <= pos.x+size.x
63 // 4) p.y <= pos.y+size.y
64 // in other words, 'size' means the number of pixels
65 // this rectangle encloses plus one. for example with pos[0,0]
66 // and size[512,512], p[512,512] would return inside.
67 // a size of [0,0] therefore denotes a one-by-one rectangle.
68 bool pointInside( sal_Int32 px
, sal_Int32 py
) const
70 const sal_Int32
x1(maPos
.getX());
71 const sal_Int32
y1(maPos
.getY());
72 const sal_Int32
x2(maPos
.getX()+maSize
.getX());
73 const sal_Int32
y2(maPos
.getY()+maSize
.getY());
74 if(px
< x1
) return false;
75 if(px
>= x2
) return false;
76 if(py
< y1
) return false;
77 if(py
>= y2
) return false;
81 // returns true if the horizontal line intersects the rect.
82 bool hLineIntersect( sal_Int32 lx1
, sal_Int32 lx2
, sal_Int32 ly
) const
84 const sal_Int32
x1(maPos
.getX());
85 const sal_Int32
y1(maPos
.getY());
86 const sal_Int32
x2(maPos
.getX()+maSize
.getX());
87 const sal_Int32
y2(maPos
.getY()+maSize
.getY());
88 if(ly
< y1
) return false;
89 if(ly
>= y2
) return false;
90 if((lx1
< x1
) && (lx2
< x1
)) return false;
91 if((lx1
>= x2
) && (lx2
>= x2
)) return false;
95 //! Returns true if the vertical line intersects the rect.
96 bool vLineIntersect( sal_Int32 lx
, sal_Int32 ly1
, sal_Int32 ly2
) const
98 const sal_Int32
x1(maPos
.getX());
99 const sal_Int32
y1(maPos
.getY());
100 const sal_Int32
x2(maPos
.getX()+maSize
.getX());
101 const sal_Int32
y2(maPos
.getY()+maSize
.getY());
102 if(lx
< x1
) return false;
103 if(lx
>= x2
) return false;
104 if((ly1
< y1
) && (ly2
< y1
)) return false;
105 if((ly1
>= y2
) && (ly2
>= y2
)) return false;
109 // returns true if the passed rect intersects this one.
110 bool intersection( const SurfaceRect
& r
) const
112 const sal_Int32
x1(maPos
.getX());
113 const sal_Int32
y1(maPos
.getY());
114 const sal_Int32
x2(maPos
.getX()+maSize
.getX());
115 const sal_Int32
y2(maPos
.getY()+maSize
.getY());
116 if(r
.hLineIntersect(x1
,x2
,y1
)) return true;
117 if(r
.hLineIntersect(x1
,x2
,y2
)) return true;
118 if(r
.vLineIntersect(x1
,y1
,y2
)) return true;
119 if(r
.vLineIntersect(x2
,y1
,y2
)) return true;
123 bool inside( const SurfaceRect
& r
) const
125 const sal_Int32
x1(maPos
.getX());
126 const sal_Int32
y1(maPos
.getY());
127 const sal_Int32
x2(maPos
.getX()+maSize
.getX());
128 const sal_Int32
y2(maPos
.getY()+maSize
.getY());
129 if(!(r
.pointInside(x1
,y1
))) return false;
130 if(!(r
.pointInside(x2
,y1
))) return false;
131 if(!(r
.pointInside(x2
,y2
))) return false;
132 if(!(r
.pointInside(x1
,y2
))) return false;