/src/libreoffice/basegfx/source/numeric/ftools.cxx
Line | Count | Source |
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 | | |
20 | | #include <basegfx/numeric/ftools.hxx> |
21 | | |
22 | | namespace basegfx |
23 | | { |
24 | | double snapToNearestMultiple(double v, const double fStep) |
25 | 0 | { |
26 | 0 | if(fTools::equalZero(fStep)) |
27 | 0 | { |
28 | | // with a zero step, all snaps to 0.0 |
29 | 0 | return 0.0; |
30 | 0 | } |
31 | 0 | else |
32 | 0 | { |
33 | 0 | const double fHalfStep(fStep * 0.5); |
34 | 0 | const double fChange(fHalfStep - fmod(v + fHalfStep, fStep)); |
35 | |
|
36 | 0 | if(basegfx::fTools::equal(fabs(v), fabs(fChange))) |
37 | 0 | { |
38 | 0 | return 0.0; |
39 | 0 | } |
40 | 0 | else |
41 | 0 | { |
42 | 0 | return v + fChange; |
43 | 0 | } |
44 | 0 | } |
45 | 0 | } |
46 | | |
47 | | double snapToZeroRange(double v, double fWidth) |
48 | 1.04k | { |
49 | 1.04k | if(fTools::equalZero(fWidth)) |
50 | 0 | { |
51 | | // with no range all snaps to range bound |
52 | 0 | return 0.0; |
53 | 0 | } |
54 | 1.04k | else |
55 | 1.04k | { |
56 | 1.04k | if(v < 0.0 || v > fWidth) |
57 | 3 | { |
58 | 3 | double fRetval(fmod(v, fWidth)); |
59 | | |
60 | 3 | if(fRetval < 0.0) |
61 | 1 | { |
62 | 1 | fRetval += fWidth; |
63 | 1 | } |
64 | | |
65 | 3 | return fRetval; |
66 | 3 | } |
67 | 1.03k | else |
68 | 1.03k | { |
69 | 1.03k | return v; |
70 | 1.03k | } |
71 | 1.04k | } |
72 | 1.04k | } |
73 | | |
74 | | double normalizeToRange(double v, const double fRange) |
75 | 1.20k | { |
76 | 1.20k | if(fRange <= 0.0) |
77 | 0 | { |
78 | | // with a zero (or less) range, all normalizes to 0.0 |
79 | 0 | return 0.0; |
80 | 0 | } |
81 | | |
82 | 1.20k | if(v < 0.0) |
83 | 0 | { |
84 | 0 | if(fTools::moreOrEqual(v, -fRange)) |
85 | 0 | { |
86 | | // in range [-fRange, 0.0[, shift one step |
87 | 0 | return v + fRange; |
88 | 0 | } |
89 | | |
90 | | // re-calculate |
91 | 0 | return v - (floor(v/fRange)*fRange); |
92 | 0 | } |
93 | 1.20k | else |
94 | 1.20k | { |
95 | 1.20k | if(fTools::less(v, fRange)) |
96 | 1.20k | { |
97 | | // already in range [0.0, fRange[, nothing to do |
98 | 1.20k | return v; |
99 | 1.20k | } |
100 | | |
101 | | // re-calculate |
102 | 0 | return v - (floor(v/fRange)*fRange); |
103 | 1.20k | } |
104 | 1.20k | } |
105 | | } // end of namespace basegfx |
106 | | |
107 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |