LCOV - code coverage report
Current view: top level - libreoffice/tools/source/generic - line.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 90 0.0 %
Date: 2012-12-27 Functions: 0 5 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <tools/link.hxx>
      21             : #include <tools/line.hxx>
      22             : #include <tools/debug.hxx>
      23             : #include <tools/helpers.hxx>
      24             : 
      25             : #include <cstdlib>
      26             : #include <math.h>
      27             : 
      28           0 : double Line::GetLength() const
      29             : {
      30           0 :     return hypot( maStart.X() - maEnd.X(), maStart.Y() - maEnd.Y() );
      31             : }
      32             : 
      33           0 : sal_Bool Line::Intersection( const Line& rLine, Point& rIntersection ) const
      34             : {
      35             :     double  fX, fY;
      36             :     sal_Bool    bRet;
      37             : 
      38           0 :     if( Intersection( rLine, fX, fY ) )
      39             :     {
      40           0 :         rIntersection.X() = FRound( fX );
      41           0 :         rIntersection.Y() = FRound( fY );
      42           0 :         bRet = sal_True;
      43             :     }
      44             :     else
      45           0 :         bRet = sal_False;
      46             : 
      47           0 :     return bRet;
      48             : }
      49             : 
      50           0 : sal_Bool Line::Intersection( const Line& rLine, double& rIntersectionX, double& rIntersectionY ) const
      51             : {
      52           0 :     const double    fAx = maEnd.X() - maStart.X();
      53           0 :     const double    fAy = maEnd.Y() - maStart.Y();
      54           0 :     const double    fBx = rLine.maStart.X() - rLine.maEnd.X();
      55           0 :     const double    fBy = rLine.maStart.Y() - rLine.maEnd.Y();
      56           0 :     const double    fDen = fAy * fBx - fAx * fBy;
      57           0 :     sal_Bool            bOk = sal_False;
      58             : 
      59           0 :     if( fDen != 0. )
      60             :     {
      61           0 :         const double    fCx = maStart.X() - rLine.maStart.X();
      62           0 :         const double    fCy = maStart.Y() - rLine.maStart.Y();
      63           0 :         const double    fA = fBy * fCx - fBx * fCy;
      64           0 :         const sal_Bool      bGreater = ( fDen > 0. );
      65             : 
      66           0 :         bOk = sal_True;
      67             : 
      68           0 :         if ( bGreater )
      69             :         {
      70           0 :             if ( ( fA < 0. ) || ( fA > fDen ) )
      71           0 :                 bOk = sal_False;
      72             :         }
      73           0 :         else if ( ( fA > 0. ) || ( fA < fDen ) )
      74           0 :             bOk = sal_False;
      75             : 
      76           0 :         if ( bOk )
      77             :         {
      78           0 :             const double fB = fAx * fCy - fAy * fCx;
      79             : 
      80           0 :             if ( bGreater )
      81             :             {
      82           0 :                 if ( ( fB < 0. ) || ( fB > fDen ) )
      83           0 :                     bOk = sal_False;
      84             :             }
      85           0 :             else if ( ( fB > 0. ) || ( fB < fDen ) )
      86           0 :                 bOk = sal_False;
      87             : 
      88           0 :             if( bOk )
      89             :             {
      90           0 :                 const double fAlpha = fA / fDen;
      91             : 
      92           0 :                 rIntersectionX = ( maStart.X() + fAlpha * fAx );
      93           0 :                 rIntersectionY = ( maStart.Y() + fAlpha * fAy );
      94             :             }
      95             :         }
      96             :     }
      97             : 
      98           0 :     return bOk;
      99             : }
     100             : 
     101           0 : sal_Bool Line::Intersection( const Rectangle& rRect, Line& rIntersection ) const
     102             : {
     103           0 :     const sal_Bool  bStartInside = rRect.IsInside( maStart );
     104           0 :     const sal_Bool  bEndInside = rRect.IsInside( maEnd );
     105           0 :     sal_Bool        bRet = sal_True;
     106             : 
     107           0 :     if( bStartInside && bEndInside )
     108             :     {
     109             :         // line completely inside rect
     110           0 :         rIntersection.maStart = maStart;
     111           0 :         rIntersection.maEnd = maEnd;
     112             :     }
     113             :     else
     114             :     {
     115             :         // calculate intersections
     116           0 :         const Point aTL( rRect.TopLeft() ), aTR( rRect.TopRight() );
     117           0 :         const Point aBR( rRect.BottomRight() ), aBL( rRect.BottomLeft() );
     118           0 :         Point       aIntersect1, aIntersect2;
     119           0 :         Point*      pCurIntersection = &aIntersect1;
     120             : 
     121           0 :         if( Intersection( Line( aTL, aTR ), *pCurIntersection ) )
     122           0 :             pCurIntersection = &aIntersect2;
     123             : 
     124           0 :         if( Intersection( Line( aTR, aBR ), *pCurIntersection ) )
     125           0 :             pCurIntersection = ( pCurIntersection == &aIntersect1 ) ? &aIntersect2 : NULL;
     126             : 
     127           0 :         if( pCurIntersection && Intersection( Line( aBR, aBL ), *pCurIntersection ) )
     128           0 :             pCurIntersection = ( pCurIntersection == &aIntersect1 ) ? &aIntersect2 : NULL;
     129             : 
     130           0 :         if( pCurIntersection && Intersection( Line( aBL, aTL ), *pCurIntersection ) )
     131           0 :             pCurIntersection = ( pCurIntersection == &aIntersect1 ) ? &aIntersect2 : NULL;
     132             : 
     133           0 :         if( !pCurIntersection )
     134             :         {
     135             :             // two intersections
     136           0 :             rIntersection.maStart = aIntersect1;
     137           0 :             rIntersection.maEnd = aIntersect2;
     138             :         }
     139           0 :         else if( pCurIntersection == &aIntersect2 )
     140             :         {
     141             :             // one intersection
     142           0 :             rIntersection.maStart = aIntersect1;
     143             : 
     144           0 :             if( ( maStart != aIntersect1 ) && bStartInside )
     145           0 :                 rIntersection.maEnd = maStart;
     146           0 :             else if( ( maEnd != aIntersect1 ) && bEndInside )
     147           0 :                 rIntersection.maEnd = maEnd;
     148             :             else
     149           0 :                 rIntersection.maEnd = rIntersection.maStart;
     150             :         }
     151             :         else
     152           0 :             bRet = sal_False;
     153             :     }
     154             : 
     155           0 :     return bRet;
     156             : }
     157             : 
     158           0 : double Line::GetDistance( const double& rPtX, const double& rPtY ) const
     159             : {
     160             :     double fDist;
     161             : 
     162           0 :     if( maStart != maEnd )
     163             :     {
     164           0 :         const double    fDistX = maEnd.X() - maStart.X();
     165           0 :         const double    fDistY = maEnd.Y() - maStart.Y();
     166           0 :         const double    fACX = maStart.X() - rPtX;
     167           0 :         const double    fACY = maStart.Y() - rPtY;
     168           0 :         const double    fL2 = fDistX * fDistX + fDistY * fDistY;
     169           0 :         const double    fR = ( fACY * -fDistY - fACX * fDistX ) / fL2;
     170           0 :         const double    fS = ( fACY * fDistX - fACX * fDistY ) / fL2;
     171             : 
     172           0 :         if( fR < 0.0 )
     173             :         {
     174           0 :             fDist = hypot( maStart.X() - rPtX, maStart.Y() - rPtY );
     175             : 
     176           0 :             if( fS < 0.0 )
     177           0 :                 fDist *= -1.0;
     178             :         }
     179           0 :         else if( fR <= 1.0 )
     180           0 :             fDist = fS * sqrt( fL2 );
     181             :         else
     182             :         {
     183           0 :             fDist = hypot( maEnd.X() - rPtX, maEnd.Y() - rPtY );
     184             : 
     185           0 :             if( fS < 0.0 )
     186           0 :                 fDist *= -1.0;
     187             :         }
     188             :     }
     189             :     else
     190           0 :         fDist = hypot( maStart.X() - rPtX, maStart.Y() - rPtY );
     191             : 
     192           0 :     return fDist;
     193             : }
     194             : 
     195             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10