LibreOffice Module basegfx (master) 1
tools.cxx
Go to the documentation of this file.
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
22
23#include <algorithm>
24
25namespace basegfx::utils
26{
27 namespace
28 {
29 double distance( const double& nX,
30 const double& nY,
31 const ::basegfx::B2DVector& rNormal,
32 const double& nC )
33 {
34 return nX*rNormal.getX() + nY*rNormal.getY() - nC;
35 }
36
37 void moveLineOutsideRect( ::basegfx::B2DPoint& io_rStart,
38 ::basegfx::B2DPoint& io_rEnd,
39 const ::basegfx::B2DVector& rMoveDirection,
40 const ::basegfx::B2DRange& rFitTarget )
41 {
42 // calc c for normal line form equation n x - c = 0
43 const double nC( rMoveDirection.scalar( io_rStart ) );
44
45 // calc maximum orthogonal distance for all four bound
46 // rect corners to the line
47 const double nMaxDistance( std::max(
48 0.0,
49 std::max(
50 distance(rFitTarget.getMinX(),
51 rFitTarget.getMinY(),
52 rMoveDirection,
53 nC),
54 std::max(
55 distance(rFitTarget.getMinX(),
56 rFitTarget.getMaxY(),
57 rMoveDirection,
58 nC),
59 std::max(
60 distance(rFitTarget.getMaxX(),
61 rFitTarget.getMinY(),
62 rMoveDirection,
63 nC),
64 distance(rFitTarget.getMaxX(),
65 rFitTarget.getMaxY(),
66 rMoveDirection,
67 nC) ) ) ) ) );
68
69 // now move line points, such that the bound rect
70 // points are all either 'on' or on the negative side
71 // of the half-plane
72 io_rStart += nMaxDistance*rMoveDirection;
73 io_rEnd += nMaxDistance*rMoveDirection;
74 }
75 }
76
78 ::basegfx::B2DPoint& io_rLeftBottom,
79 ::basegfx::B2DPoint& io_rRightTop,
80 ::basegfx::B2DPoint& io_rRightBottom,
81 const ::basegfx::B2DRange& rFitTarget )
82 {
83 // For the top and bottom border line of the
84 // parallelogram, we determine the distance to all four
85 // corner points of the bound rect (tl, tr, bl, br). When
86 // using the unit normal form for lines (n x - c = 0), and
87 // choosing n to point 'outwards' the parallelogram, then
88 // all bound rect corner points having positive distance
89 // to the line lie outside the extended gradient rect, and
90 // thus, the corresponding border line must be moved the
91 // maximum distance outwards.
92
93 // don't use the top and bottom border line direction, and
94 // calculate the normal from them. Instead, use the
95 // vertical lines (lt - lb or rt - rb), as they more
96 // faithfully represent the direction of the
97 // to-be-generated infinite line
98 ::basegfx::B2DVector aDirectionVertical( io_rLeftTop - io_rLeftBottom );
99 aDirectionVertical.normalize();
100
101 const ::basegfx::B2DVector aNormalTop( aDirectionVertical );
102 const ::basegfx::B2DVector aNormalBottom( -aDirectionVertical );
103
104 // now extend parallelogram, such that the bound rect
105 // point are included
106 moveLineOutsideRect( io_rLeftTop, io_rRightTop, aNormalTop, rFitTarget );
107 moveLineOutsideRect( io_rLeftBottom, io_rRightBottom, aNormalBottom, rFitTarget );
108 }
109}
110
111/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Base Point class with two double values.
Definition: b2dpoint.hxx:42
Base Point class with two double values.
Definition: b2dvector.hxx:40
B2DVector & normalize()
Normalize this 2D Vector.
Definition: b2dvector.cxx:26
double distance
void infiniteLineFromParallelogram(::basegfx::B2DPoint &io_rLeftTop, ::basegfx::B2DPoint &io_rLeftBottom, ::basegfx::B2DPoint &io_rRightTop, ::basegfx::B2DPoint &io_rRightBottom, const ::basegfx::B2DRange &rFitTarget)
Expand given parallelogram, such that it extends beyond bound rect in a given direction.
Definition: tools.cxx:77