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/helpers.hxx>
23 :
24 : #include <cstdlib>
25 : #include <math.h>
26 :
27 0 : double Line::GetLength() const
28 : {
29 0 : return hypot( maStart.X() - maEnd.X(), maStart.Y() - maEnd.Y() );
30 : }
31 :
32 0 : bool Line::Intersection( const Line& rLine, Point& rIntersection ) const
33 : {
34 : double fX, fY;
35 : bool bRet;
36 :
37 0 : if( Intersection( rLine, fX, fY ) )
38 : {
39 0 : rIntersection.X() = FRound( fX );
40 0 : rIntersection.Y() = FRound( fY );
41 0 : bRet = true;
42 : }
43 : else
44 0 : bRet = false;
45 :
46 0 : return bRet;
47 : }
48 :
49 131182 : bool Line::Intersection( const Line& rLine, double& rIntersectionX, double& rIntersectionY ) const
50 : {
51 131182 : const double fAx = maEnd.X() - maStart.X();
52 131182 : const double fAy = maEnd.Y() - maStart.Y();
53 131182 : const double fBx = rLine.maStart.X() - rLine.maEnd.X();
54 131182 : const double fBy = rLine.maStart.Y() - rLine.maEnd.Y();
55 131182 : const double fDen = fAy * fBx - fAx * fBy;
56 131182 : bool bOk = false;
57 :
58 131182 : if( fDen != 0. )
59 : {
60 66076 : const double fCx = maStart.X() - rLine.maStart.X();
61 66076 : const double fCy = maStart.Y() - rLine.maStart.Y();
62 66076 : const double fA = fBy * fCx - fBx * fCy;
63 66076 : const bool bGreater = ( fDen > 0. );
64 :
65 66076 : bOk = true;
66 :
67 66076 : if ( bGreater )
68 : {
69 33038 : if ( ( fA < 0. ) || ( fA > fDen ) )
70 148 : bOk = false;
71 : }
72 33038 : else if ( ( fA > 0. ) || ( fA < fDen ) )
73 148 : bOk = false;
74 :
75 66076 : if ( bOk )
76 : {
77 65780 : const double fB = fAx * fCy - fAy * fCx;
78 :
79 65780 : if ( bGreater )
80 : {
81 32890 : if ( ( fB < 0. ) || ( fB > fDen ) )
82 1094 : bOk = false;
83 : }
84 32890 : else if ( ( fB > 0. ) || ( fB < fDen ) )
85 1098 : bOk = false;
86 :
87 65780 : if( bOk )
88 : {
89 63588 : const double fAlpha = fA / fDen;
90 :
91 63588 : rIntersectionX = ( maStart.X() + fAlpha * fAx );
92 63588 : rIntersectionY = ( maStart.Y() + fAlpha * fAy );
93 : }
94 : }
95 : }
96 :
97 131182 : return bOk;
98 : }
99 :
100 16 : double Line::GetDistance( const double& rPtX, const double& rPtY ) const
101 : {
102 : double fDist;
103 :
104 16 : if( maStart != maEnd )
105 : {
106 16 : const double fDistX = maEnd.X() - maStart.X();
107 16 : const double fDistY = maEnd.Y() - maStart.Y();
108 16 : const double fACX = maStart.X() - rPtX;
109 16 : const double fACY = maStart.Y() - rPtY;
110 16 : const double fL2 = fDistX * fDistX + fDistY * fDistY;
111 16 : const double fR = ( fACY * -fDistY - fACX * fDistX ) / fL2;
112 16 : const double fS = ( fACY * fDistX - fACX * fDistY ) / fL2;
113 :
114 16 : if( fR < 0.0 )
115 : {
116 0 : fDist = hypot( maStart.X() - rPtX, maStart.Y() - rPtY );
117 :
118 0 : if( fS < 0.0 )
119 0 : fDist *= -1.0;
120 : }
121 16 : else if( fR <= 1.0 )
122 16 : fDist = fS * sqrt( fL2 );
123 : else
124 : {
125 0 : fDist = hypot( maEnd.X() - rPtX, maEnd.Y() - rPtY );
126 :
127 0 : if( fS < 0.0 )
128 0 : fDist *= -1.0;
129 : }
130 : }
131 : else
132 0 : fDist = hypot( maStart.X() - rPtX, maStart.Y() - rPtY );
133 :
134 16 : return fDist;
135 : }
136 :
137 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|