update dev300-m58
[ooovba.git] / basegfx / source / tools / liangbarsky.cxx
blob32678c67ffbed5932f45772a0976b7cd530b2095
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: liangbarsky.cxx,v $
10 * $Revision: 1.5 $
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/numeric/ftools.hxx"
36 #include "basegfx/range/b2drange.hxx"
39 namespace basegfx
41 namespace tools
43 namespace
45 // see Foley/vanDam, pp. 122 for the Liang-Barsky line
46 // clipping algorithm
47 inline bool liangBarskyClipT( double nDenom,
48 double nNumerator,
49 double& io_rTE,
50 double& io_rTL )
52 double t;
53 if( nDenom > 0 )
55 t = nNumerator / nDenom;
56 if( t > io_rTL )
57 return false;
58 else if( t > io_rTE )
59 io_rTE = t;
61 else if( nDenom < 0 )
63 t = nNumerator / nDenom;
64 if( t < io_rTE )
65 return false;
66 else
67 io_rTL = t;
69 else if( nNumerator > 0 )
71 return false;
74 return true;
78 // see Foley/vanDam, pp. 122 for the Liang-Barsky line
79 // clipping algorithm
80 bool liangBarskyClip2D( ::basegfx::B2DPoint& io_rStart,
81 ::basegfx::B2DPoint& io_rEnd,
82 const ::basegfx::B2DRange& rClipRect )
84 const double nDX( io_rEnd.getX() - io_rStart.getX() );
85 const double nDY( io_rEnd.getY() - io_rStart.getY() );
87 if( ::basegfx::fTools::equalZero( nDX ) &&
88 ::basegfx::fTools::equalZero( nDY ) )
90 return rClipRect.isInside( io_rStart );
92 else
94 double nTE( 0.0 );
95 double nTL( 1.0 );
96 if( liangBarskyClipT(nDX, rClipRect.getMinX() - io_rStart.getX(),
97 nTE, nTL ) ) // inside wrt. left edge
99 if( liangBarskyClipT(-nDX, io_rStart.getX() - rClipRect.getMaxX(),
100 nTE, nTL ) ) // inside wrt. right edge
102 if( liangBarskyClipT(nDY, rClipRect.getMinY() - io_rStart.getY(),
103 nTE, nTL ) ) // inside wrt. bottom edge
105 if( liangBarskyClipT(-nDY, io_rStart.getY() - rClipRect.getMaxY(),
106 nTE, nTL ) ) // inside wrt. top edge
108 // compute actual intersection points,
109 // if nTL has changed
110 if( nTL < 1.0 )
112 io_rEnd.setX( io_rStart.getX() + nTL*nDX );
113 io_rEnd.setY( io_rStart.getY() + nTL*nDY );
116 // compute actual intersection points,
117 // if nTE has changed
118 if( nTE > 0.0 )
120 io_rStart.setX( io_rStart.getX() + nTE*nDX );
121 io_rStart.setY( io_rStart.getY() + nTE*nDY );
124 // line is (at least partially) visible
125 return true;
132 return false;