Coverage Report

Created: 2026-04-09 11:41

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
19.4M
{
55
19.4M
    const double    fAx = static_cast<double>(maEnd.X()) - maStart.X();
56
19.4M
    const double    fAy = static_cast<double>(maEnd.Y()) - maStart.Y();
57
19.4M
    const double    fBx = static_cast<double>(rLine.maStart.X()) - rLine.maEnd.X();
58
19.4M
    const double    fBy = static_cast<double>(rLine.maStart.Y()) - rLine.maEnd.Y();
59
19.4M
    const double    fDen = fAy * fBx - fAx * fBy;
60
19.4M
    bool            bOk = false;
61
62
19.4M
    if( fDen != 0. )
63
16.5M
    {
64
16.5M
        const double    fCx = static_cast<double>(maStart.X()) - rLine.maStart.X();
65
16.5M
        const double    fCy = static_cast<double>(maStart.Y()) - rLine.maStart.Y();
66
16.5M
        const double    fA = fBy * fCx - fBx * fCy;
67
16.5M
        const bool      bGreater = ( fDen > 0. );
68
69
16.5M
        bOk = true;
70
71
16.5M
        if ( bGreater )
72
8.41M
        {
73
8.41M
            if ( ( fA < 0. ) || ( fA > fDen ) )
74
2.44M
                bOk = false;
75
8.41M
        }
76
8.11M
        else if ( ( fA > 0. ) || ( fA < fDen ) )
77
2.07M
            bOk = false;
78
79
16.5M
        if ( bOk )
80
12.0M
        {
81
12.0M
            const double fB = fAx * fCy - fAy * fCx;
82
83
12.0M
            if ( bGreater )
84
5.96M
            {
85
5.96M
                if ( ( fB < 0. ) || ( fB > fDen ) )
86
4.08M
                    bOk = false;
87
5.96M
            }
88
6.04M
            else if ( ( fB > 0. ) || ( fB < fDen ) )
89
4.16M
                bOk = false;
90
91
12.0M
            if( bOk )
92
3.75M
            {
93
3.75M
                const double fAlpha = fA / fDen;
94
95
3.75M
                rIntersectionX = ( maStart.X() + fAlpha * fAx );
96
3.75M
                rIntersectionY = ( maStart.Y() + fAlpha * fAy );
97
3.75M
            }
98
12.0M
        }
99
16.5M
    }
100
101
19.4M
    return bOk;
102
19.4M
}
103
104
double Line::GetDistance( const double& rPtX, const double& rPtY ) const
105
35.5k
{
106
35.5k
    double fDist;
107
108
35.5k
    if( maStart != maEnd )
109
35.5k
    {
110
35.5k
        const double fDistX = static_cast<double>(maEnd.X()) - maStart.X();
111
35.5k
        const double fDistY = static_cast<double>(maEnd.Y()) - maStart.Y();
112
35.5k
        const double fACX = static_cast<double>(maStart.X()) - rPtX;
113
35.5k
        const double fACY = static_cast<double>(maStart.Y()) - rPtY;
114
35.5k
        const double fL2 = fDistX * fDistX + fDistY * fDistY;
115
35.5k
        const double fR = ( fACY * -fDistY - fACX * fDistX ) / fL2;
116
35.5k
        const double fS = ( fACY * fDistX - fACX * fDistY ) / fL2;
117
118
35.5k
        if( fR < 0.0 )
119
78
        {
120
78
            fDist = hypot( maStart.X() - rPtX, maStart.Y() - rPtY );
121
122
78
            if( fS < 0.0 )
123
50
                fDist *= -1.0;
124
78
        }
125
35.4k
        else if( fR <= 1.0 )
126
35.2k
            fDist = fS * sqrt( fL2 );
127
222
        else
128
222
        {
129
222
            fDist = hypot( maEnd.X() - rPtX, maEnd.Y() - rPtY );
130
131
222
            if( fS < 0.0 )
132
135
                fDist *= -1.0;
133
222
        }
134
35.5k
    }
135
0
    else
136
0
        fDist = hypot( maStart.X() - rPtX, maStart.Y() - rPtY );
137
138
35.5k
    return fDist;
139
35.5k
}
140
141
} //namespace tools
142
143
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */