1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/curve/b2dbeziertools.hxx>
21 #include <basegfx/curve/b2dcubicbezier.hxx>
24 //////////////////////////////////////////////////////////////////////////////
28 B2DCubicBezierHelper::B2DCubicBezierHelper(const B2DCubicBezier
& rBase
, sal_uInt32 nDivisions
)
32 const bool bIsBezier(rBase
.isBezier());
36 // check nDivisions; at least one is needed, but also prevent too big values
41 else if(nDivisions
> 1000)
47 mnEdgeCount
= nDivisions
+ 1;
49 // fill in maLengthArray
50 maLengthArray
.clear();
51 maLengthArray
.reserve(mnEdgeCount
);
52 B2DPoint
aCurrent(rBase
.getStartPoint());
55 for(sal_uInt32
a(1);;)
57 const B2DPoint
aNext(rBase
.interpolatePoint((double)a
/ (double)mnEdgeCount
));
58 const B2DVector
aEdge(aNext
- aCurrent
);
60 fLength
+= aEdge
.getLength();
61 maLengthArray
.push_back(fLength
);
69 const B2DPoint
aLastNext(rBase
.getEndPoint());
70 const B2DVector
aLastEdge(aLastNext
- aNext
);
72 fLength
+= aLastEdge
.getLength();
73 maLengthArray
.push_back(fLength
);
80 maLengthArray
.clear();
81 maLengthArray
.push_back(rBase
.getEdgeLength());
86 double B2DCubicBezierHelper::distanceToRelative(double fDistance
) const
93 const double fLength(getLength());
95 if(fTools::moreOrEqual(fDistance
, fLength
))
100 // fDistance is in ]0.0 .. fLength[
104 // not a bezier, linear edge
105 return fDistance
/ fLength
;
109 ::std::vector
< double >::const_iterator aIter
= ::std::lower_bound(maLengthArray
.begin(), maLengthArray
.end(), fDistance
);
110 const sal_uInt32
nIndex(aIter
- maLengthArray
.begin());
111 const double fHighBound(maLengthArray
[nIndex
]);
112 const double fLowBound(nIndex
? maLengthArray
[nIndex
- 1] : 0.0);
113 const double fLinearInterpolatedLength((fDistance
- fLowBound
) / (fHighBound
- fLowBound
));
115 return (static_cast< double >(nIndex
) + fLinearInterpolatedLength
) / static_cast< double >(mnEdgeCount
);
118 } // end of namespace basegfx
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */