/src/libreoffice/basegfx/source/polygon/WaveLine.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 | | */ |
10 | | |
11 | | #include <basegfx/polygon/WaveLine.hxx> |
12 | | #include <basegfx/point/b2dpoint.hxx> |
13 | | #include <basegfx/polygon/b2dpolygon.hxx> |
14 | | |
15 | | namespace basegfx |
16 | | { |
17 | | BASEGFX_DLLPUBLIC B2DPolygon createWaveLinePolygon(basegfx::B2DRectangle const& rRectangle) |
18 | 0 | { |
19 | 0 | basegfx::B2DPolygon aPolygon; |
20 | |
|
21 | 0 | double fWaveHeight = rRectangle.getHeight(); |
22 | | // Wavelength depends on the wave height so it looks nice |
23 | 0 | double fHalfWaveLength = fWaveHeight + 1.0; |
24 | 0 | double fWaveAmplitude = fWaveHeight / 2.0; |
25 | |
|
26 | 0 | double fLastX = rRectangle.getMinX(); |
27 | 0 | double fBaseY = rRectangle.getMinY() + fWaveAmplitude; |
28 | 0 | double fDirection = 1.0; |
29 | | |
30 | | // In quadratic bezier the curve is 1/2 of the control height |
31 | | // so we need to compensate for that. |
32 | 0 | constexpr double fHeightCompensation = 2.0; |
33 | |
|
34 | 0 | aPolygon.append(basegfx::B2DPoint(fLastX, fBaseY)); |
35 | |
|
36 | 0 | for (double fI = fHalfWaveLength; fI <= rRectangle.getWidth(); fI += fHalfWaveLength) |
37 | 0 | { |
38 | 0 | basegfx::B2DPoint aPoint(fLastX + fHalfWaveLength, fBaseY); |
39 | 0 | basegfx::B2DPoint aControl(fLastX + (fHalfWaveLength / 2.0), |
40 | 0 | fBaseY + fDirection * fWaveAmplitude * fHeightCompensation); |
41 | |
|
42 | 0 | aPolygon.appendQuadraticBezierSegment(aControl, aPoint); |
43 | |
|
44 | 0 | fLastX = aPoint.getX(); // next iteration |
45 | 0 | fDirection *= -1.0; // fDirection iterates between 1 and -1 |
46 | 0 | } |
47 | |
|
48 | 0 | return aPolygon; |
49 | 0 | } |
50 | | |
51 | | } // end of namespace basegfx |
52 | | |
53 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |