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