1*09dbbe93SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*09dbbe93SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*09dbbe93SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*09dbbe93SAndrew Rist * distributed with this work for additional information 6*09dbbe93SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*09dbbe93SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*09dbbe93SAndrew Rist * "License"); you may not use this file except in compliance 9*09dbbe93SAndrew Rist * with the License. You may obtain a copy of the License at 10*09dbbe93SAndrew Rist * 11*09dbbe93SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*09dbbe93SAndrew Rist * 13*09dbbe93SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*09dbbe93SAndrew Rist * software distributed under the License is distributed on an 15*09dbbe93SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*09dbbe93SAndrew Rist * KIND, either express or implied. See the License for the 17*09dbbe93SAndrew Rist * specific language governing permissions and limitations 18*09dbbe93SAndrew Rist * under the License. 19*09dbbe93SAndrew Rist * 20*09dbbe93SAndrew Rist *************************************************************/ 21*09dbbe93SAndrew Rist 22*09dbbe93SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_basegfx.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include "basegfx/tools/tools.hxx" 28cdf0e10cSrcweir #include "basegfx/range/b2drange.hxx" 29cdf0e10cSrcweir 30cdf0e10cSrcweir #include <algorithm> 31cdf0e10cSrcweir 32cdf0e10cSrcweir 33cdf0e10cSrcweir namespace basegfx 34cdf0e10cSrcweir { 35cdf0e10cSrcweir namespace tools 36cdf0e10cSrcweir { 37cdf0e10cSrcweir namespace 38cdf0e10cSrcweir { distance(const double & nX,const double & nY,const::basegfx::B2DVector & rNormal,const double & nC)39cdf0e10cSrcweir inline double distance( const double& nX, 40cdf0e10cSrcweir const double& nY, 41cdf0e10cSrcweir const ::basegfx::B2DVector& rNormal, 42cdf0e10cSrcweir const double& nC ) 43cdf0e10cSrcweir { 44cdf0e10cSrcweir return nX*rNormal.getX() + nY*rNormal.getY() - nC; 45cdf0e10cSrcweir } 46cdf0e10cSrcweir moveLineOutsideRect(::basegfx::B2DPoint & io_rStart,::basegfx::B2DPoint & io_rEnd,const::basegfx::B2DVector & rMoveDirection,const::basegfx::B2DRange & rFitTarget)47cdf0e10cSrcweir void moveLineOutsideRect( ::basegfx::B2DPoint& io_rStart, 48cdf0e10cSrcweir ::basegfx::B2DPoint& io_rEnd, 49cdf0e10cSrcweir const ::basegfx::B2DVector& rMoveDirection, 50cdf0e10cSrcweir const ::basegfx::B2DRange& rFitTarget ) 51cdf0e10cSrcweir { 52cdf0e10cSrcweir // calc c for normal line form equation n x - c = 0 53cdf0e10cSrcweir const double nC( rMoveDirection.scalar( io_rStart ) ); 54cdf0e10cSrcweir 55cdf0e10cSrcweir // calc maximum orthogonal distance for all four bound 56cdf0e10cSrcweir // rect corners to the line 57cdf0e10cSrcweir const double nMaxDistance( ::std::max( 58cdf0e10cSrcweir 0.0, 59cdf0e10cSrcweir ::std::max( 60cdf0e10cSrcweir distance(rFitTarget.getMinX(), 61cdf0e10cSrcweir rFitTarget.getMinY(), 62cdf0e10cSrcweir rMoveDirection, 63cdf0e10cSrcweir nC), 64cdf0e10cSrcweir ::std::max( 65cdf0e10cSrcweir distance(rFitTarget.getMinX(), 66cdf0e10cSrcweir rFitTarget.getMaxY(), 67cdf0e10cSrcweir rMoveDirection, 68cdf0e10cSrcweir nC), 69cdf0e10cSrcweir ::std::max( 70cdf0e10cSrcweir distance(rFitTarget.getMaxX(), 71cdf0e10cSrcweir rFitTarget.getMinY(), 72cdf0e10cSrcweir rMoveDirection, 73cdf0e10cSrcweir nC), 74cdf0e10cSrcweir distance(rFitTarget.getMaxX(), 75cdf0e10cSrcweir rFitTarget.getMaxY(), 76cdf0e10cSrcweir rMoveDirection, 77cdf0e10cSrcweir nC) ) ) ) ) ); 78cdf0e10cSrcweir 79cdf0e10cSrcweir // now move line points, such that the bound rect 80cdf0e10cSrcweir // points are all either 'on' or on the negative side 81cdf0e10cSrcweir // of the half-plane 82cdf0e10cSrcweir io_rStart += nMaxDistance*rMoveDirection; 83cdf0e10cSrcweir io_rEnd += nMaxDistance*rMoveDirection; 84cdf0e10cSrcweir } 85cdf0e10cSrcweir } 86cdf0e10cSrcweir infiniteLineFromParallelogram(::basegfx::B2DPoint & io_rLeftTop,::basegfx::B2DPoint & io_rLeftBottom,::basegfx::B2DPoint & io_rRightTop,::basegfx::B2DPoint & io_rRightBottom,const::basegfx::B2DRange & rFitTarget)87cdf0e10cSrcweir void infiniteLineFromParallelogram( ::basegfx::B2DPoint& io_rLeftTop, 88cdf0e10cSrcweir ::basegfx::B2DPoint& io_rLeftBottom, 89cdf0e10cSrcweir ::basegfx::B2DPoint& io_rRightTop, 90cdf0e10cSrcweir ::basegfx::B2DPoint& io_rRightBottom, 91cdf0e10cSrcweir const ::basegfx::B2DRange& rFitTarget ) 92cdf0e10cSrcweir { 93cdf0e10cSrcweir // For the top and bottom border line of the 94cdf0e10cSrcweir // parallelogram, we determine the distance to all four 95cdf0e10cSrcweir // corner points of the bound rect (tl, tr, bl, br). When 96cdf0e10cSrcweir // using the unit normal form for lines (n x - c = 0), and 97cdf0e10cSrcweir // choosing n to point 'outwards' the parallelogram, then 98cdf0e10cSrcweir // all bound rect corner points having positive distance 99cdf0e10cSrcweir // to the line lie outside the extended gradient rect, and 100cdf0e10cSrcweir // thus, the corresponding border line must be moved the 101cdf0e10cSrcweir // maximum distance outwards. 102cdf0e10cSrcweir 103cdf0e10cSrcweir // don't use the top and bottom border line direction, and 104cdf0e10cSrcweir // calculate the normal from them. Instead, use the 105cdf0e10cSrcweir // vertical lines (lt - lb or rt - rb), as they more 106cdf0e10cSrcweir // faithfully represent the direction of the 107cdf0e10cSrcweir // to-be-generated infinite line 108cdf0e10cSrcweir ::basegfx::B2DVector aDirectionVertical( io_rLeftTop - io_rLeftBottom ); 109cdf0e10cSrcweir aDirectionVertical.normalize(); 110cdf0e10cSrcweir 111cdf0e10cSrcweir const ::basegfx::B2DVector aNormalTop( aDirectionVertical ); 112cdf0e10cSrcweir const ::basegfx::B2DVector aNormalBottom( -aDirectionVertical ); 113cdf0e10cSrcweir 114cdf0e10cSrcweir // now extend parallelogram, such that the bound rect 115cdf0e10cSrcweir // point are included 116cdf0e10cSrcweir moveLineOutsideRect( io_rLeftTop, io_rRightTop, aNormalTop, rFitTarget ); 117cdf0e10cSrcweir moveLineOutsideRect( io_rLeftBottom, io_rRightBottom, aNormalBottom, rFitTarget ); 118cdf0e10cSrcweir } 119cdf0e10cSrcweir } 120cdf0e10cSrcweir } 121