Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/tools/source/generic/line.cxx
Line
Count
Source
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 <sal/config.h>
21
22
#include <basegfx/numeric/ftools.hxx>
23
#include <tools/line.hxx>
24
#include <tools/helpers.hxx>
25
26
#include <cmath>
27
28
namespace tools
29
{
30
31
double Line::GetLength() const
32
0
{
33
0
    return hypot( maStart.X() - maEnd.X(), maStart.Y() - maEnd.Y() );
34
0
}
35
36
bool Line::Intersection( const Line& rLine, Point& rIntersection ) const
37
0
{
38
0
    double  fX, fY;
39
0
    bool    bRet;
40
41
0
    if( Intersection( rLine, fX, fY ) )
42
0
    {
43
0
        rIntersection.setX(basegfx::fround<tools::Long>(fX));
44
0
        rIntersection.setY(basegfx::fround<tools::Long>(fY));
45
0
        bRet = true;
46
0
    }
47
0
    else
48
0
        bRet = false;
49
50
0
    return bRet;
51
0
}
52
53
bool Line::Intersection( const tools::Line& rLine, double& rIntersectionX, double& rIntersectionY ) const
54
14.2M
{
55
14.2M
    const double    fAx = static_cast<double>(maEnd.X()) - maStart.X();
56
14.2M
    const double    fAy = static_cast<double>(maEnd.Y()) - maStart.Y();
57
14.2M
    const double    fBx = static_cast<double>(rLine.maStart.X()) - rLine.maEnd.X();
58
14.2M
    const double    fBy = static_cast<double>(rLine.maStart.Y()) - rLine.maEnd.Y();
59
14.2M
    const double    fDen = fAy * fBx - fAx * fBy;
60
14.2M
    bool            bOk = false;
61
62
14.2M
    if( fDen != 0. )
63
12.4M
    {
64
12.4M
        const double    fCx = static_cast<double>(maStart.X()) - rLine.maStart.X();
65
12.4M
        const double    fCy = static_cast<double>(maStart.Y()) - rLine.maStart.Y();
66
12.4M
        const double    fA = fBy * fCx - fBx * fCy;
67
12.4M
        const bool      bGreater = ( fDen > 0. );
68
69
12.4M
        bOk = true;
70
71
12.4M
        if ( bGreater )
72
6.40M
        {
73
6.40M
            if ( ( fA < 0. ) || ( fA > fDen ) )
74
1.99M
                bOk = false;
75
6.40M
        }
76
6.00M
        else if ( ( fA > 0. ) || ( fA < fDen ) )
77
1.51M
            bOk = false;
78
79
12.4M
        if ( bOk )
80
8.90M
        {
81
8.90M
            const double fB = fAx * fCy - fAy * fCx;
82
83
8.90M
            if ( bGreater )
84
4.41M
            {
85
4.41M
                if ( ( fB < 0. ) || ( fB > fDen ) )
86
2.92M
                    bOk = false;
87
4.41M
            }
88
4.48M
            else if ( ( fB > 0. ) || ( fB < fDen ) )
89
2.99M
                bOk = false;
90
91
8.90M
            if( bOk )
92
2.98M
            {
93
2.98M
                const double fAlpha = fA / fDen;
94
95
2.98M
                rIntersectionX = ( maStart.X() + fAlpha * fAx );
96
2.98M
                rIntersectionY = ( maStart.Y() + fAlpha * fAy );
97
2.98M
            }
98
8.90M
        }
99
12.4M
    }
100
101
14.2M
    return bOk;
102
14.2M
}
103
104
double Line::GetDistance( const double& rPtX, const double& rPtY ) const
105
18.7k
{
106
18.7k
    double fDist;
107
108
18.7k
    if( maStart != maEnd )
109
18.7k
    {
110
18.7k
        const double fDistX = static_cast<double>(maEnd.X()) - maStart.X();
111
18.7k
        const double fDistY = static_cast<double>(maEnd.Y()) - maStart.Y();
112
18.7k
        const double fACX = static_cast<double>(maStart.X()) - rPtX;
113
18.7k
        const double fACY = static_cast<double>(maStart.Y()) - rPtY;
114
18.7k
        const double fL2 = fDistX * fDistX + fDistY * fDistY;
115
18.7k
        const double fR = ( fACY * -fDistY - fACX * fDistX ) / fL2;
116
18.7k
        const double fS = ( fACY * fDistX - fACX * fDistY ) / fL2;
117
118
18.7k
        if( fR < 0.0 )
119
60
        {
120
60
            fDist = hypot( maStart.X() - rPtX, maStart.Y() - rPtY );
121
122
60
            if( fS < 0.0 )
123
60
                fDist *= -1.0;
124
60
        }
125
18.7k
        else if( fR <= 1.0 )
126
18.6k
            fDist = fS * sqrt( fL2 );
127
57
        else
128
57
        {
129
57
            fDist = hypot( maEnd.X() - rPtX, maEnd.Y() - rPtY );
130
131
57
            if( fS < 0.0 )
132
0
                fDist *= -1.0;
133
57
        }
134
18.7k
    }
135
0
    else
136
0
        fDist = hypot( maStart.X() - rPtX, maStart.Y() - rPtY );
137
138
18.7k
    return fDist;
139
18.7k
}
140
141
} //namespace tools
142
143
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */