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: tools.cxx,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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basegfx.hxx"
34 #include "basegfx/tools/tools.hxx"
35 #include "basegfx/range/b2drange.hxx"
46 inline double distance( const double& nX
,
48 const ::basegfx::B2DVector
& rNormal
,
51 return nX
*rNormal
.getX() + nY
*rNormal
.getY() - nC
;
54 void moveLineOutsideRect( ::basegfx::B2DPoint
& io_rStart
,
55 ::basegfx::B2DPoint
& io_rEnd
,
56 const ::basegfx::B2DVector
& rMoveDirection
,
57 const ::basegfx::B2DRange
& rFitTarget
)
59 // calc c for normal line form equation n x - c = 0
60 const double nC( rMoveDirection
.scalar( io_rStart
) );
62 // calc maximum orthogonal distance for all four bound
63 // rect corners to the line
64 const double nMaxDistance( ::std::max(
67 distance(rFitTarget
.getMinX(),
72 distance(rFitTarget
.getMinX(),
77 distance(rFitTarget
.getMaxX(),
81 distance(rFitTarget
.getMaxX(),
86 // now move line points, such that the bound rect
87 // points are all either 'on' or on the negative side
89 io_rStart
+= nMaxDistance
*rMoveDirection
;
90 io_rEnd
+= nMaxDistance
*rMoveDirection
;
94 void infiniteLineFromParallelogram( ::basegfx::B2DPoint
& io_rLeftTop
,
95 ::basegfx::B2DPoint
& io_rLeftBottom
,
96 ::basegfx::B2DPoint
& io_rRightTop
,
97 ::basegfx::B2DPoint
& io_rRightBottom
,
98 const ::basegfx::B2DRange
& rFitTarget
)
100 // For the top and bottom border line of the
101 // parallelogram, we determine the distance to all four
102 // corner points of the bound rect (tl, tr, bl, br). When
103 // using the unit normal form for lines (n x - c = 0), and
104 // choosing n to point 'outwards' the parallelogram, then
105 // all bound rect corner points having positive distance
106 // to the line lie outside the extended gradient rect, and
107 // thus, the corresponding border line must be moved the
108 // maximum distance outwards.
110 // don't use the top and bottom border line direction, and
111 // calculate the normal from them. Instead, use the
112 // vertical lines (lt - lb or rt - rb), as they more
113 // faithfully represent the direction of the
114 // to-be-generated infinite line
115 ::basegfx::B2DVector
aDirectionVertical( io_rLeftTop
- io_rLeftBottom
);
116 aDirectionVertical
.normalize();
118 const ::basegfx::B2DVector
aNormalTop( aDirectionVertical
);
119 const ::basegfx::B2DVector
aNormalBottom( -aDirectionVertical
);
121 // now extend parallelogram, such that the bound rect
122 // point are included
123 moveLineOutsideRect( io_rLeftTop
, io_rRightTop
, aNormalTop
, rFitTarget
);
124 moveLineOutsideRect( io_rLeftBottom
, io_rRightBottom
, aNormalBottom
, rFitTarget
);