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 42060 : bool Line::Intersection( const Line& rLine, double& rIntersectionX, double& rIntersectionY ) const
50 : {
51 42060 : const double fAx = maEnd.X() - maStart.X();
52 42060 : const double fAy = maEnd.Y() - maStart.Y();
53 42060 : const double fBx = rLine.maStart.X() - rLine.maEnd.X();
54 42060 : const double fBy = rLine.maStart.Y() - rLine.maEnd.Y();
55 42060 : const double fDen = fAy * fBx - fAx * fBy;
56 42060 : bool bOk = false;
57 :
58 42060 : if( fDen != 0. )
59 : {
60 22000 : const double fCx = maStart.X() - rLine.maStart.X();
61 22000 : const double fCy = maStart.Y() - rLine.maStart.Y();
62 22000 : const double fA = fBy * fCx - fBx * fCy;
63 22000 : const bool bGreater = ( fDen > 0. );
64 :
65 22000 : bOk = true;
66 :
67 22000 : if ( bGreater )
68 : {
69 11000 : if ( ( fA < 0. ) || ( fA > fDen ) )
70 296 : bOk = false;
71 : }
72 11000 : else if ( ( fA > 0. ) || ( fA < fDen ) )
73 296 : bOk = false;
74 :
75 22000 : if ( bOk )
76 : {
77 21408 : const double fB = fAx * fCy - fAy * fCx;
78 :
79 21408 : if ( bGreater )
80 : {
81 10704 : if ( ( fB < 0. ) || ( fB > fDen ) )
82 770 : bOk = false;
83 : }
84 10704 : else if ( ( fB > 0. ) || ( fB < fDen ) )
85 778 : bOk = false;
86 :
87 21408 : if( bOk )
88 : {
89 19860 : const double fAlpha = fA / fDen;
90 :
91 19860 : rIntersectionX = ( maStart.X() + fAlpha * fAx );
92 19860 : rIntersectionY = ( maStart.Y() + fAlpha * fAy );
93 : }
94 : }
95 : }
96 :
97 42060 : return bOk;
98 : }
99 :
100 232 : double Line::GetDistance( const double& rPtX, const double& rPtY ) const
101 : {
102 : double fDist;
103 :
104 232 : if( maStart != maEnd )
105 : {
106 232 : const double fDistX = maEnd.X() - maStart.X();
107 232 : const double fDistY = maEnd.Y() - maStart.Y();
108 232 : const double fACX = maStart.X() - rPtX;
109 232 : const double fACY = maStart.Y() - rPtY;
110 232 : const double fL2 = fDistX * fDistX + fDistY * fDistY;
111 232 : const double fR = ( fACY * -fDistY - fACX * fDistX ) / fL2;
112 232 : const double fS = ( fACY * fDistX - fACX * fDistY ) / fL2;
113 :
114 232 : 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 232 : else if( fR <= 1.0 )
122 232 : 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 232 : return fDist;
135 : }
136 :
137 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|