basegfx/test/LengthUnitTest.cxx | 23 ++++++++++++++++++----- include/basegfx/units/LengthUnitBase.hxx | 11 +++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-)
New commits: commit 3f6a8c8d1d7f9bd5d9a8668d7567ba89f7755800 Author: Tomaž Vajngerl <[email protected]> AuthorDate: Thu Oct 13 10:12:27 2022 +0200 Commit: Tomaž Vajngerl <[email protected]> CommitDate: Thu Oct 13 10:19:24 2022 +0200 add division of length units to LengthUnitBase Reult of division of 2 length units is a ratio (double). Change-Id: I3d0e605492ca0af93580de138b5bc0c3e7877fa5 diff --git a/basegfx/test/LengthUnitTest.cxx b/basegfx/test/LengthUnitTest.cxx index 8861a2ade316..11095f3222b5 100644 --- a/basegfx/test/LengthUnitTest.cxx +++ b/basegfx/test/LengthUnitTest.cxx @@ -32,11 +32,6 @@ public: CPPUNIT_ASSERT_EQUAL(sal_Int64(34200000000), cm3.raw()); CPPUNIT_ASSERT_DOUBLES_EQUAL(95000.0, cm3.as_cm(), 1e-4); - gfx::Length cm4(1_cm); - cm4 /= 2; - CPPUNIT_ASSERT_EQUAL(sal_Int64(180000), cm4.raw()); - CPPUNIT_ASSERT_DOUBLES_EQUAL(0.5, cm4.as_cm(), 1e-4); - // (635 * 20) + 3 * (635 * 15) = 41275EMU gfx::Length pt = 1_pt + 3_px; CPPUNIT_ASSERT_DOUBLES_EQUAL(3.25, pt.as_pt(), 1e-4); @@ -92,6 +87,23 @@ public: CPPUNIT_ASSERT_EQUAL(sal_Int64(3636), bb.raw()); } + void testDivision() + { + gfx::Length cm(1_cm); + cm /= 2; + CPPUNIT_ASSERT_EQUAL(sal_Int64(180000), cm.raw()); + CPPUNIT_ASSERT_DOUBLES_EQUAL(0.5, cm.as_cm(), 1e-4); + + gfx::Length cm4(1_cm); + cm4 /= 2.0; + CPPUNIT_ASSERT_EQUAL(sal_Int64(180000), cm4.raw()); + CPPUNIT_ASSERT_DOUBLES_EQUAL(0.5, cm4.as_cm(), 1e-4); + + // with division of 2 length units you get a ratio + double aRatio = gfx::Length::hmm(10) / gfx::Length::hmm(20); + CPPUNIT_ASSERT_DOUBLES_EQUAL(0.5, aRatio, 1e-9); + } + void testInRange() { gfx::Range2DL aRange(1_cm, 2_cm, 2_cm, 30_mm); @@ -159,6 +171,7 @@ public: CPPUNIT_TEST_SUITE(LengthTest); CPPUNIT_TEST(testBasic); + CPPUNIT_TEST(testDivision); CPPUNIT_TEST(testInRange); CPPUNIT_TEST(testInTuple); CPPUNIT_TEST(testConversionToRectanle); diff --git a/include/basegfx/units/LengthUnitBase.hxx b/include/basegfx/units/LengthUnitBase.hxx index c1d8a8e8e431..30daccef8015 100644 --- a/include/basegfx/units/LengthUnitBase.hxx +++ b/include/basegfx/units/LengthUnitBase.hxx @@ -173,14 +173,25 @@ inline LengthUnitBase<T> operator-(LengthUnitBase<T> lhs, const LengthUnitBase<T return lhs -= rhs; } +/// Multiplication of a length unit with a scalar value. +/// example 1cm * 2 = 2cm template <typename T> inline LengthUnitBase<T> operator*(LengthUnitBase<T> lhs, const long rhs) { return lhs *= rhs; } +/// Division of a length unit with a scalar value. +/// example 1cm / 2 = 0.5cm template <typename T> inline LengthUnitBase<T> operator/(LengthUnitBase<T> lhs, const long rhs) { return lhs /= rhs; } +/// Division of 2 length units, which results in a ratio. +/// example 1cm / 2cm = 0.5 +template <typename T> inline double operator/(LengthUnitBase<T> lhs, const LengthUnitBase<T> rhs) +{ + return lhs.raw() / double(rhs.raw()); +} + } // end namespace gfx
