Avoid potential negative array index access to cached text.
[LibreOffice.git] / tools / source / generic / line.cxx
blobee9ad979793c1d41b7740aa9e769d970648a8521
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <tools/line.hxx>
21 #include <tools/helpers.hxx>
23 #include <cmath>
25 namespace tools
28 double Line::GetLength() const
30 return hypot( maStart.X() - maEnd.X(), maStart.Y() - maEnd.Y() );
33 bool Line::Intersection( const Line& rLine, Point& rIntersection ) const
35 double fX, fY;
36 bool bRet;
38 if( Intersection( rLine, fX, fY ) )
40 rIntersection.setX( FRound( fX ) );
41 rIntersection.setY( FRound( fY ) );
42 bRet = true;
44 else
45 bRet = false;
47 return bRet;
50 bool Line::Intersection( const tools::Line& rLine, double& rIntersectionX, double& rIntersectionY ) const
52 const double fAx = static_cast<double>(maEnd.X()) - maStart.X();
53 const double fAy = static_cast<double>(maEnd.Y()) - maStart.Y();
54 const double fBx = static_cast<double>(rLine.maStart.X()) - rLine.maEnd.X();
55 const double fBy = static_cast<double>(rLine.maStart.Y()) - rLine.maEnd.Y();
56 const double fDen = fAy * fBx - fAx * fBy;
57 bool bOk = false;
59 if( fDen != 0. )
61 const double fCx = static_cast<double>(maStart.X()) - rLine.maStart.X();
62 const double fCy = static_cast<double>(maStart.Y()) - rLine.maStart.Y();
63 const double fA = fBy * fCx - fBx * fCy;
64 const bool bGreater = ( fDen > 0. );
66 bOk = true;
68 if ( bGreater )
70 if ( ( fA < 0. ) || ( fA > fDen ) )
71 bOk = false;
73 else if ( ( fA > 0. ) || ( fA < fDen ) )
74 bOk = false;
76 if ( bOk )
78 const double fB = fAx * fCy - fAy * fCx;
80 if ( bGreater )
82 if ( ( fB < 0. ) || ( fB > fDen ) )
83 bOk = false;
85 else if ( ( fB > 0. ) || ( fB < fDen ) )
86 bOk = false;
88 if( bOk )
90 const double fAlpha = fA / fDen;
92 rIntersectionX = ( maStart.X() + fAlpha * fAx );
93 rIntersectionY = ( maStart.Y() + fAlpha * fAy );
98 return bOk;
101 double Line::GetDistance( const double& rPtX, const double& rPtY ) const
103 double fDist;
105 if( maStart != maEnd )
107 const double fDistX = static_cast<double>(maEnd.X()) - maStart.X();
108 const double fDistY = static_cast<double>(maEnd.Y()) - maStart.Y();
109 const double fACX = static_cast<double>(maStart.X()) - rPtX;
110 const double fACY = static_cast<double>(maStart.Y()) - rPtY;
111 const double fL2 = fDistX * fDistX + fDistY * fDistY;
112 const double fR = ( fACY * -fDistY - fACX * fDistX ) / fL2;
113 const double fS = ( fACY * fDistX - fACX * fDistY ) / fL2;
115 if( fR < 0.0 )
117 fDist = hypot( maStart.X() - rPtX, maStart.Y() - rPtY );
119 if( fS < 0.0 )
120 fDist *= -1.0;
122 else if( fR <= 1.0 )
123 fDist = fS * sqrt( fL2 );
124 else
126 fDist = hypot( maEnd.X() - rPtX, maEnd.Y() - rPtY );
128 if( fS < 0.0 )
129 fDist *= -1.0;
132 else
133 fDist = hypot( maStart.X() - rPtX, maStart.Y() - rPtY );
135 return fDist;
138 } //namespace tools
140 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */