Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / basegfx / source / tools / tools.cxx
blob16feb0c7ef16a5235dd4374089385959c64694c5
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 <basegfx/utils/tools.hxx>
21 #include <basegfx/range/b2drange.hxx>
23 #include <algorithm>
25 namespace basegfx
27 namespace utils
29 namespace
31 inline double distance( const double& nX,
32 const double& nY,
33 const ::basegfx::B2DVector& rNormal,
34 const double& nC )
36 return nX*rNormal.getX() + nY*rNormal.getY() - nC;
39 void moveLineOutsideRect( ::basegfx::B2DPoint& io_rStart,
40 ::basegfx::B2DPoint& io_rEnd,
41 const ::basegfx::B2DVector& rMoveDirection,
42 const ::basegfx::B2DRange& rFitTarget )
44 // calc c for normal line form equation n x - c = 0
45 const double nC( rMoveDirection.scalar( io_rStart ) );
47 // calc maximum orthogonal distance for all four bound
48 // rect corners to the line
49 const double nMaxDistance( std::max(
50 0.0,
51 std::max(
52 distance(rFitTarget.getMinX(),
53 rFitTarget.getMinY(),
54 rMoveDirection,
55 nC),
56 std::max(
57 distance(rFitTarget.getMinX(),
58 rFitTarget.getMaxY(),
59 rMoveDirection,
60 nC),
61 std::max(
62 distance(rFitTarget.getMaxX(),
63 rFitTarget.getMinY(),
64 rMoveDirection,
65 nC),
66 distance(rFitTarget.getMaxX(),
67 rFitTarget.getMaxY(),
68 rMoveDirection,
69 nC) ) ) ) ) );
71 // now move line points, such that the bound rect
72 // points are all either 'on' or on the negative side
73 // of the half-plane
74 io_rStart += nMaxDistance*rMoveDirection;
75 io_rEnd += nMaxDistance*rMoveDirection;
79 void infiniteLineFromParallelogram( ::basegfx::B2DPoint& io_rLeftTop,
80 ::basegfx::B2DPoint& io_rLeftBottom,
81 ::basegfx::B2DPoint& io_rRightTop,
82 ::basegfx::B2DPoint& io_rRightBottom,
83 const ::basegfx::B2DRange& rFitTarget )
85 // For the top and bottom border line of the
86 // parallelogram, we determine the distance to all four
87 // corner points of the bound rect (tl, tr, bl, br). When
88 // using the unit normal form for lines (n x - c = 0), and
89 // choosing n to point 'outwards' the parallelogram, then
90 // all bound rect corner points having positive distance
91 // to the line lie outside the extended gradient rect, and
92 // thus, the corresponding border line must be moved the
93 // maximum distance outwards.
95 // don't use the top and bottom border line direction, and
96 // calculate the normal from them. Instead, use the
97 // vertical lines (lt - lb or rt - rb), as they more
98 // faithfully represent the direction of the
99 // to-be-generated infinite line
100 ::basegfx::B2DVector aDirectionVertical( io_rLeftTop - io_rLeftBottom );
101 aDirectionVertical.normalize();
103 const ::basegfx::B2DVector aNormalTop( aDirectionVertical );
104 const ::basegfx::B2DVector aNormalBottom( -aDirectionVertical );
106 // now extend parallelogram, such that the bound rect
107 // point are included
108 moveLineOutsideRect( io_rLeftTop, io_rRightTop, aNormalTop, rFitTarget );
109 moveLineOutsideRect( io_rLeftBottom, io_rRightBottom, aNormalBottom, rFitTarget );
114 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */