Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / basegfx / source / numeric / ftools.cxx
blob1cbbe5cc21365841da5b7e8e39441806df2e2858
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/numeric/ftools.hxx>
21 #include <algorithm>
23 namespace basegfx
25 // init static member of class fTools
26 double ::basegfx::fTools::mfSmallValue = 0.000000001;
28 double snapToNearestMultiple(double v, const double fStep)
30 if(fTools::equalZero(fStep))
32 // with a zero step, all snaps to 0.0
33 return 0.0;
35 else
37 const double fHalfStep(fStep * 0.5);
38 const double fChange(fHalfStep - fmod(v + fHalfStep, fStep));
40 if(basegfx::fTools::equal(fabs(v), fabs(fChange)))
42 return 0.0;
44 else
46 return v + fChange;
51 double snapToZeroRange(double v, double fWidth)
53 if(fTools::equalZero(fWidth))
55 // with no range all snaps to range bound
56 return 0.0;
58 else
60 if(v < 0.0 || v > fWidth)
62 double fRetval(fmod(v, fWidth));
64 if(fRetval < 0.0)
66 fRetval += fWidth;
69 return fRetval;
71 else
73 return v;
78 double snapToRange(double v, double fLow, double fHigh)
80 if(fTools::equal(fLow, fHigh))
82 // with no range all snaps to range bound
83 return 0.0;
85 else
87 if(fLow > fHigh)
89 // correct range order. Evtl. assert this (?)
90 std::swap(fLow, fHigh);
93 if(v < fLow || v > fHigh)
95 return snapToZeroRange(v - fLow, fHigh - fLow) + fLow;
97 else
99 return v;
103 } // end of namespace basegfx
105 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */