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 // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_tools.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #define _LINE_CXX 32*cdf0e10cSrcweir #include <tools/link.hxx> 33*cdf0e10cSrcweir #include <tools/line.hxx> 34*cdf0e10cSrcweir #include <tools/debug.hxx> 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir #include <cstdlib> 37*cdf0e10cSrcweir #include <math.h> 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir // -------- 40*cdf0e10cSrcweir // - Line - 41*cdf0e10cSrcweir // -------- 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir double Line::GetLength() const 44*cdf0e10cSrcweir { 45*cdf0e10cSrcweir return hypot( maStart.X() - maEnd.X(), maStart.Y() - maEnd.Y() ); 46*cdf0e10cSrcweir } 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir // ------------------------------------------------------------------------ 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir sal_Bool Line::Intersection( const Line& rLine, Point& rIntersection ) const 51*cdf0e10cSrcweir { 52*cdf0e10cSrcweir double fX, fY; 53*cdf0e10cSrcweir sal_Bool bRet; 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir if( Intersection( rLine, fX, fY ) ) 56*cdf0e10cSrcweir { 57*cdf0e10cSrcweir rIntersection.X() = FRound( fX ); 58*cdf0e10cSrcweir rIntersection.Y() = FRound( fY ); 59*cdf0e10cSrcweir bRet = sal_True; 60*cdf0e10cSrcweir } 61*cdf0e10cSrcweir else 62*cdf0e10cSrcweir bRet = sal_False; 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir return bRet; 65*cdf0e10cSrcweir } 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir // ------------------------------------------------------------------------ 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir sal_Bool Line::Intersection( const Line& rLine, double& rIntersectionX, double& rIntersectionY ) const 70*cdf0e10cSrcweir { 71*cdf0e10cSrcweir const double fAx = maEnd.X() - maStart.X(); 72*cdf0e10cSrcweir const double fAy = maEnd.Y() - maStart.Y(); 73*cdf0e10cSrcweir const double fBx = rLine.maStart.X() - rLine.maEnd.X(); 74*cdf0e10cSrcweir const double fBy = rLine.maStart.Y() - rLine.maEnd.Y(); 75*cdf0e10cSrcweir const double fDen = fAy * fBx - fAx * fBy; 76*cdf0e10cSrcweir sal_Bool bOk = sal_False; 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir if( fDen != 0. ) 79*cdf0e10cSrcweir { 80*cdf0e10cSrcweir const double fCx = maStart.X() - rLine.maStart.X(); 81*cdf0e10cSrcweir const double fCy = maStart.Y() - rLine.maStart.Y(); 82*cdf0e10cSrcweir const double fA = fBy * fCx - fBx * fCy; 83*cdf0e10cSrcweir const sal_Bool bGreater = ( fDen > 0. ); 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir bOk = sal_True; 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir if ( bGreater ) 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir if ( ( fA < 0. ) || ( fA > fDen ) ) 90*cdf0e10cSrcweir bOk = sal_False; 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir else if ( ( fA > 0. ) || ( fA < fDen ) ) 93*cdf0e10cSrcweir bOk = sal_False; 94*cdf0e10cSrcweir 95*cdf0e10cSrcweir if ( bOk ) 96*cdf0e10cSrcweir { 97*cdf0e10cSrcweir const double fB = fAx * fCy - fAy * fCx; 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir if ( bGreater ) 100*cdf0e10cSrcweir { 101*cdf0e10cSrcweir if ( ( fB < 0. ) || ( fB > fDen ) ) 102*cdf0e10cSrcweir bOk = sal_False; 103*cdf0e10cSrcweir } 104*cdf0e10cSrcweir else if ( ( fB > 0. ) || ( fB < fDen ) ) 105*cdf0e10cSrcweir bOk = sal_False; 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir if( bOk ) 108*cdf0e10cSrcweir { 109*cdf0e10cSrcweir const double fAlpha = fA / fDen; 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir rIntersectionX = ( maStart.X() + fAlpha * fAx ); 112*cdf0e10cSrcweir rIntersectionY = ( maStart.Y() + fAlpha * fAy ); 113*cdf0e10cSrcweir } 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir } 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir return bOk; 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir // ------------------------------------------------------------------------ 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir sal_Bool Line::Intersection( const Rectangle& rRect, Line& rIntersection ) const 123*cdf0e10cSrcweir { 124*cdf0e10cSrcweir const sal_Bool bStartInside = rRect.IsInside( maStart ); 125*cdf0e10cSrcweir const sal_Bool bEndInside = rRect.IsInside( maEnd ); 126*cdf0e10cSrcweir sal_Bool bRet = sal_True; 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir if( bStartInside && bEndInside ) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir // line completely inside rect 131*cdf0e10cSrcweir rIntersection.maStart = maStart; 132*cdf0e10cSrcweir rIntersection.maEnd = maEnd; 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir else 135*cdf0e10cSrcweir { 136*cdf0e10cSrcweir // calculate intersections 137*cdf0e10cSrcweir const Point aTL( rRect.TopLeft() ), aTR( rRect.TopRight() ); 138*cdf0e10cSrcweir const Point aBR( rRect.BottomRight() ), aBL( rRect.BottomLeft() ); 139*cdf0e10cSrcweir Point aIntersect1, aIntersect2; 140*cdf0e10cSrcweir Point* pCurIntersection = &aIntersect1; 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir if( Intersection( Line( aTL, aTR ), *pCurIntersection ) ) 143*cdf0e10cSrcweir pCurIntersection = &aIntersect2; 144*cdf0e10cSrcweir 145*cdf0e10cSrcweir if( Intersection( Line( aTR, aBR ), *pCurIntersection ) ) 146*cdf0e10cSrcweir pCurIntersection = ( pCurIntersection == &aIntersect1 ) ? &aIntersect2 : NULL; 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir if( pCurIntersection && Intersection( Line( aBR, aBL ), *pCurIntersection ) ) 149*cdf0e10cSrcweir pCurIntersection = ( pCurIntersection == &aIntersect1 ) ? &aIntersect2 : NULL; 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir if( pCurIntersection && Intersection( Line( aBL, aTL ), *pCurIntersection ) ) 152*cdf0e10cSrcweir pCurIntersection = ( pCurIntersection == &aIntersect1 ) ? &aIntersect2 : NULL; 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir if( !pCurIntersection ) 155*cdf0e10cSrcweir { 156*cdf0e10cSrcweir // two intersections 157*cdf0e10cSrcweir rIntersection.maStart = aIntersect1; 158*cdf0e10cSrcweir rIntersection.maEnd = aIntersect2; 159*cdf0e10cSrcweir } 160*cdf0e10cSrcweir else if( pCurIntersection == &aIntersect2 ) 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir // one intersection 163*cdf0e10cSrcweir rIntersection.maStart = aIntersect1; 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir if( ( maStart != aIntersect1 ) && bStartInside ) 166*cdf0e10cSrcweir rIntersection.maEnd = maStart; 167*cdf0e10cSrcweir else if( ( maEnd != aIntersect1 ) && bEndInside ) 168*cdf0e10cSrcweir rIntersection.maEnd = maEnd; 169*cdf0e10cSrcweir else 170*cdf0e10cSrcweir rIntersection.maEnd = rIntersection.maStart; 171*cdf0e10cSrcweir } 172*cdf0e10cSrcweir else 173*cdf0e10cSrcweir bRet = sal_False; 174*cdf0e10cSrcweir } 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir return bRet; 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir // ------------------------------------------------------------------------ 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir Point Line::NearestPoint( const Point& rPoint ) const 182*cdf0e10cSrcweir { 183*cdf0e10cSrcweir Point aRetPt; 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir if ( maStart != maEnd ) 186*cdf0e10cSrcweir { 187*cdf0e10cSrcweir const double fDistX = maEnd.X() - maStart.X(); 188*cdf0e10cSrcweir const double fDistY = maStart.Y() - maEnd.Y(); 189*cdf0e10cSrcweir const double fTau = ( ( maStart.Y() - rPoint.Y() ) * fDistY - 190*cdf0e10cSrcweir ( maStart.X() - rPoint.X() ) * fDistX ) / 191*cdf0e10cSrcweir ( fDistX * fDistX + fDistY * fDistY ); 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir if( fTau < 0.0 ) 194*cdf0e10cSrcweir aRetPt = maStart; 195*cdf0e10cSrcweir else if( fTau <= 1.0 ) 196*cdf0e10cSrcweir { 197*cdf0e10cSrcweir aRetPt.X() = FRound( maStart.X() + fTau * fDistX ); 198*cdf0e10cSrcweir aRetPt.Y() = FRound( maStart.Y() - fTau * fDistY ); 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir else 201*cdf0e10cSrcweir aRetPt = maEnd; 202*cdf0e10cSrcweir } 203*cdf0e10cSrcweir else 204*cdf0e10cSrcweir aRetPt = maStart; 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir return aRetPt; 207*cdf0e10cSrcweir } 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir // ------------------------------------------------------------------------ 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir double Line::GetDistance( const double& rPtX, const double& rPtY ) const 212*cdf0e10cSrcweir { 213*cdf0e10cSrcweir double fDist; 214*cdf0e10cSrcweir 215*cdf0e10cSrcweir if( maStart != maEnd ) 216*cdf0e10cSrcweir { 217*cdf0e10cSrcweir const double fDistX = maEnd.X() - maStart.X(); 218*cdf0e10cSrcweir const double fDistY = maEnd.Y() - maStart.Y(); 219*cdf0e10cSrcweir const double fACX = maStart.X() - rPtX; 220*cdf0e10cSrcweir const double fACY = maStart.Y() - rPtY; 221*cdf0e10cSrcweir const double fL2 = fDistX * fDistX + fDistY * fDistY; 222*cdf0e10cSrcweir const double fR = ( fACY * -fDistY - fACX * fDistX ) / fL2; 223*cdf0e10cSrcweir const double fS = ( fACY * fDistX - fACX * fDistY ) / fL2; 224*cdf0e10cSrcweir 225*cdf0e10cSrcweir if( fR < 0.0 ) 226*cdf0e10cSrcweir { 227*cdf0e10cSrcweir fDist = hypot( maStart.X() - rPtX, maStart.Y() - rPtY ); 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir if( fS < 0.0 ) 230*cdf0e10cSrcweir fDist *= -1.0; 231*cdf0e10cSrcweir } 232*cdf0e10cSrcweir else if( fR <= 1.0 ) 233*cdf0e10cSrcweir fDist = fS * sqrt( fL2 ); 234*cdf0e10cSrcweir else 235*cdf0e10cSrcweir { 236*cdf0e10cSrcweir fDist = hypot( maEnd.X() - rPtX, maEnd.Y() - rPtY ); 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir if( fS < 0.0 ) 239*cdf0e10cSrcweir fDist *= -1.0; 240*cdf0e10cSrcweir } 241*cdf0e10cSrcweir } 242*cdf0e10cSrcweir else 243*cdf0e10cSrcweir fDist = hypot( maStart.X() - rPtX, maStart.Y() - rPtY ); 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir return fDist; 246*cdf0e10cSrcweir } 247*cdf0e10cSrcweir 248*cdf0e10cSrcweir // ------------------------------------------------------------------------ 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir void Line::Enum( const Link& rEnumLink ) 251*cdf0e10cSrcweir { 252*cdf0e10cSrcweir DBG_ASSERT( rEnumLink.IsSet(), "This call doesn't make any sense with !rEnumLink.IsSet()" ); 253*cdf0e10cSrcweir 254*cdf0e10cSrcweir Point aEnum; 255*cdf0e10cSrcweir long nX; 256*cdf0e10cSrcweir long nY; 257*cdf0e10cSrcweir 258*cdf0e10cSrcweir if( maStart.X() == maEnd.X() ) 259*cdf0e10cSrcweir { 260*cdf0e10cSrcweir const long nEndY = maEnd.Y(); 261*cdf0e10cSrcweir 262*cdf0e10cSrcweir nX = maStart.X(); 263*cdf0e10cSrcweir nY = maStart.Y(); 264*cdf0e10cSrcweir 265*cdf0e10cSrcweir if( nEndY > nY ) 266*cdf0e10cSrcweir { 267*cdf0e10cSrcweir while( nY <= nEndY ) 268*cdf0e10cSrcweir { 269*cdf0e10cSrcweir aEnum.X() = nX; 270*cdf0e10cSrcweir aEnum.Y() = nY++; 271*cdf0e10cSrcweir rEnumLink.Call( &aEnum ); 272*cdf0e10cSrcweir } 273*cdf0e10cSrcweir } 274*cdf0e10cSrcweir else 275*cdf0e10cSrcweir { 276*cdf0e10cSrcweir while( nY >= nEndY ) 277*cdf0e10cSrcweir { 278*cdf0e10cSrcweir aEnum.X() = nX; 279*cdf0e10cSrcweir aEnum.Y() = nY--; 280*cdf0e10cSrcweir rEnumLink.Call( &aEnum ); 281*cdf0e10cSrcweir } 282*cdf0e10cSrcweir } 283*cdf0e10cSrcweir } 284*cdf0e10cSrcweir else if( maStart.Y() == maEnd.Y() ) 285*cdf0e10cSrcweir { 286*cdf0e10cSrcweir const long nEndX = maEnd.X(); 287*cdf0e10cSrcweir 288*cdf0e10cSrcweir nX = maStart.X(); 289*cdf0e10cSrcweir nY = maStart.Y(); 290*cdf0e10cSrcweir 291*cdf0e10cSrcweir if( nEndX > nX ) 292*cdf0e10cSrcweir { 293*cdf0e10cSrcweir while( nX <= nEndX ) 294*cdf0e10cSrcweir { 295*cdf0e10cSrcweir aEnum.X() = nX++; 296*cdf0e10cSrcweir aEnum.Y() = nY; 297*cdf0e10cSrcweir rEnumLink.Call( &aEnum ); 298*cdf0e10cSrcweir } 299*cdf0e10cSrcweir } 300*cdf0e10cSrcweir else 301*cdf0e10cSrcweir { 302*cdf0e10cSrcweir while( nX >= nEndX ) 303*cdf0e10cSrcweir { 304*cdf0e10cSrcweir aEnum.X() = nX--; 305*cdf0e10cSrcweir aEnum.Y() = nY; 306*cdf0e10cSrcweir rEnumLink.Call( &aEnum ); 307*cdf0e10cSrcweir } 308*cdf0e10cSrcweir } 309*cdf0e10cSrcweir } 310*cdf0e10cSrcweir else 311*cdf0e10cSrcweir { 312*cdf0e10cSrcweir const long nDX = labs( maEnd.X() - maStart.X() ); 313*cdf0e10cSrcweir const long nDY = labs( maEnd.Y() - maStart.Y() ); 314*cdf0e10cSrcweir const long nStartX = maStart.X(); 315*cdf0e10cSrcweir const long nStartY = maStart.Y(); 316*cdf0e10cSrcweir const long nEndX = maEnd.X(); 317*cdf0e10cSrcweir const long nEndY = maEnd.Y(); 318*cdf0e10cSrcweir const long nXInc = ( nStartX < nEndX ) ? 1L : -1L; 319*cdf0e10cSrcweir const long nYInc = ( nStartY < nEndY ) ? 1L : -1L; 320*cdf0e10cSrcweir 321*cdf0e10cSrcweir if( nDX >= nDY ) 322*cdf0e10cSrcweir { 323*cdf0e10cSrcweir const long nDYX = ( nDY - nDX ) << 1; 324*cdf0e10cSrcweir const long nDY2 = nDY << 1; 325*cdf0e10cSrcweir long nD = nDY2 - nDX; 326*cdf0e10cSrcweir 327*cdf0e10cSrcweir for( nX = nStartX, nY = nStartY; nX != nEndX; nX += nXInc ) 328*cdf0e10cSrcweir { 329*cdf0e10cSrcweir aEnum.X() = nX; 330*cdf0e10cSrcweir aEnum.Y() = nY; 331*cdf0e10cSrcweir rEnumLink.Call( &aEnum ); 332*cdf0e10cSrcweir 333*cdf0e10cSrcweir if( nD < 0L ) 334*cdf0e10cSrcweir nD += nDY2; 335*cdf0e10cSrcweir else 336*cdf0e10cSrcweir nD += nDYX, nY += nYInc; 337*cdf0e10cSrcweir } 338*cdf0e10cSrcweir } 339*cdf0e10cSrcweir else 340*cdf0e10cSrcweir { 341*cdf0e10cSrcweir const long nDYX = ( nDX - nDY ) << 1; 342*cdf0e10cSrcweir const long nDY2 = nDX << 1; 343*cdf0e10cSrcweir long nD = nDY2 - nDY; 344*cdf0e10cSrcweir 345*cdf0e10cSrcweir for( nX = nStartX, nY = nStartY; nY != nEndY; nY += nYInc ) 346*cdf0e10cSrcweir { 347*cdf0e10cSrcweir aEnum.X() = nX; 348*cdf0e10cSrcweir aEnum.Y() = nY; 349*cdf0e10cSrcweir rEnumLink.Call( &aEnum ); 350*cdf0e10cSrcweir 351*cdf0e10cSrcweir if( nD < 0L ) 352*cdf0e10cSrcweir nD += nDY2; 353*cdf0e10cSrcweir else 354*cdf0e10cSrcweir nD += nDYX, nX += nXInc; 355*cdf0e10cSrcweir } 356*cdf0e10cSrcweir } 357*cdf0e10cSrcweir 358*cdf0e10cSrcweir // last point 359*cdf0e10cSrcweir aEnum.X() = nEndX; 360*cdf0e10cSrcweir aEnum.Y() = nEndY; 361*cdf0e10cSrcweir rEnumLink.Call( &aEnum ); 362*cdf0e10cSrcweir } 363*cdf0e10cSrcweir } 364