Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / basegfx / source / polygon / WaveLine.cxx
blobbc85c45362503ba6678e1b507d96dc804370b057
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 */
11 #include <basegfx/polygon/WaveLine.hxx>
12 #include <basegfx/point/b2dpoint.hxx>
14 namespace basegfx
16 BASEGFX_DLLPUBLIC B2DPolygon createWaveLinePolygon(basegfx::B2DRectangle const& rRectangle)
18 basegfx::B2DPolygon aPolygon;
20 double fWaveHeight = rRectangle.getHeight();
21 // Wavelength depends on the wave height so it looks nice
22 double fHalfWaveLength = fWaveHeight + 1.0;
23 double fWaveAmplitude = fWaveHeight / 2.0;
25 double fLastX = rRectangle.getMinX();
26 double fBaseY = rRectangle.getMinY() + fWaveAmplitude;
27 double fDirection = 1.0;
29 // In quadratic bezier the curve is 1/2 of the control height
30 // so we need to compensate for that.
31 constexpr double fHeightCompensation = 2.0;
33 aPolygon.append(basegfx::B2DPoint(fLastX, fBaseY));
35 for (double fI = fHalfWaveLength; fI <= rRectangle.getWidth(); fI += fHalfWaveLength)
37 basegfx::B2DPoint aPoint(fLastX + fHalfWaveLength, fBaseY);
38 basegfx::B2DPoint aControl(fLastX + (fHalfWaveLength / 2.0),
39 fBaseY + fDirection * fWaveAmplitude * fHeightCompensation);
41 aPolygon.appendQuadraticBezierSegment(aControl, aPoint);
43 fLastX = aPoint.getX(); // next iteration
44 fDirection *= -1.0; // fDirection iterates between 1 and -1
47 return aPolygon;
50 } // end of namespace basegfx
52 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */