From b0f4a7c594ae72ad0775cc54a04106761824a4ab Mon Sep 17 00:00:00 2001 From: "U. Bruhin" Date: Sun, 18 Feb 2018 17:08:57 +0100 Subject: [PATCH] Small improvements in Path::flatArc() --- libs/librepcb/common/geometry/path.cpp | 15 +++++++++++---- libs/librepcb/common/geometry/path.h | 3 ++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/libs/librepcb/common/geometry/path.cpp b/libs/librepcb/common/geometry/path.cpp index 18bc36d26c..4140874dd3 100644 --- a/libs/librepcb/common/geometry/path.cpp +++ b/libs/librepcb/common/geometry/path.cpp @@ -276,12 +276,19 @@ Path Path::octagon(const Length& width, const Length& height) noexcept return p; } -Path Path::flatArc(const Point& p1, const Point& p2, const Angle& angle, qreal tolerance) noexcept +Path Path::flatArc(const Point& p1, const Point& p2, const Angle& angle, + const Length& maxTolerance) noexcept { + // return straight line if radius is smaller than half of the allowed tolerance + Length radiusAbs = Toolbox::arcRadius(p1, p2, angle).abs(); + if (radiusAbs <= maxTolerance.abs() / 2) { + return line(p1, p2); + } + // calculate how many lines we need to create - Length radius = Toolbox::arcRadius(p1, p2, angle).abs(); - qreal y = qBound(0.0, tolerance, (qreal)radius.toNm() / 4); - qreal stepsPerRad = qMin(0.5 / qAcos(1 - y / radius.toNm()), radius.toNm() / 2.0); + qreal radiusAbsNm = static_cast(radiusAbs.toNm()); + qreal y = qBound(0.0, static_cast(maxTolerance.toNm()), radiusAbsNm / 4); + qreal stepsPerRad = qMin(0.5 / qAcos(1 - y / radiusAbsNm), radiusAbsNm / 2); int steps = qCeil(stepsPerRad * angle.abs().toRad()); // some other very complex calculations... diff --git a/libs/librepcb/common/geometry/path.h b/libs/librepcb/common/geometry/path.h index e80d0dac0f..526fa0e8ac 100644 --- a/libs/librepcb/common/geometry/path.h +++ b/libs/librepcb/common/geometry/path.h @@ -96,7 +96,8 @@ class Path final : public SerializableObject static Path rect(const Point& p1, const Point& p2) noexcept; static Path centeredRect(const Length& width, const Length& height) noexcept; static Path octagon(const Length& width, const Length& height) noexcept; - static Path flatArc(const Point& p1, const Point& p2, const Angle& angle, qreal tolerance) noexcept; + static Path flatArc(const Point& p1, const Point& p2, const Angle& angle, + const Length& maxTolerance) noexcept; static QPainterPath toQPainterPathPx(const QList& paths) noexcept;