fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / tools / source / generic / line.cxx
blobf84003cb6e002ca46addb6d00859f9043eefdc71
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/link.hxx>
21 #include <tools/line.hxx>
22 #include <tools/helpers.hxx>
24 #include <cstdlib>
25 #include <math.h>
27 double Line::GetLength() const
29 return hypot( maStart.X() - maEnd.X(), maStart.Y() - maEnd.Y() );
32 bool Line::Intersection( const Line& rLine, Point& rIntersection ) const
34 double fX, fY;
35 bool bRet;
37 if( Intersection( rLine, fX, fY ) )
39 rIntersection.X() = FRound( fX );
40 rIntersection.Y() = FRound( fY );
41 bRet = true;
43 else
44 bRet = false;
46 return bRet;
49 bool Line::Intersection( const Line& rLine, double& rIntersectionX, double& rIntersectionY ) const
51 const double fAx = maEnd.X() - maStart.X();
52 const double fAy = maEnd.Y() - maStart.Y();
53 const double fBx = rLine.maStart.X() - rLine.maEnd.X();
54 const double fBy = rLine.maStart.Y() - rLine.maEnd.Y();
55 const double fDen = fAy * fBx - fAx * fBy;
56 bool bOk = false;
58 if( fDen != 0. )
60 const double fCx = maStart.X() - rLine.maStart.X();
61 const double fCy = maStart.Y() - rLine.maStart.Y();
62 const double fA = fBy * fCx - fBx * fCy;
63 const bool bGreater = ( fDen > 0. );
65 bOk = true;
67 if ( bGreater )
69 if ( ( fA < 0. ) || ( fA > fDen ) )
70 bOk = false;
72 else if ( ( fA > 0. ) || ( fA < fDen ) )
73 bOk = false;
75 if ( bOk )
77 const double fB = fAx * fCy - fAy * fCx;
79 if ( bGreater )
81 if ( ( fB < 0. ) || ( fB > fDen ) )
82 bOk = false;
84 else if ( ( fB > 0. ) || ( fB < fDen ) )
85 bOk = false;
87 if( bOk )
89 const double fAlpha = fA / fDen;
91 rIntersectionX = ( maStart.X() + fAlpha * fAx );
92 rIntersectionY = ( maStart.Y() + fAlpha * fAy );
97 return bOk;
100 double Line::GetDistance( const double& rPtX, const double& rPtY ) const
102 double fDist;
104 if( maStart != maEnd )
106 const double fDistX = maEnd.X() - maStart.X();
107 const double fDistY = maEnd.Y() - maStart.Y();
108 const double fACX = maStart.X() - rPtX;
109 const double fACY = maStart.Y() - rPtY;
110 const double fL2 = fDistX * fDistX + fDistY * fDistY;
111 const double fR = ( fACY * -fDistY - fACX * fDistX ) / fL2;
112 const double fS = ( fACY * fDistX - fACX * fDistY ) / fL2;
114 if( fR < 0.0 )
116 fDist = hypot( maStart.X() - rPtX, maStart.Y() - rPtY );
118 if( fS < 0.0 )
119 fDist *= -1.0;
121 else if( fR <= 1.0 )
122 fDist = fS * sqrt( fL2 );
123 else
125 fDist = hypot( maEnd.X() - rPtX, maEnd.Y() - rPtY );
127 if( fS < 0.0 )
128 fDist *= -1.0;
131 else
132 fDist = hypot( maStart.X() - rPtX, maStart.Y() - rPtY );
134 return fDist;
137 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */