diff --git a/modules/juce_graphics/geometry/juce_Path.cpp b/modules/juce_graphics/geometry/juce_Path.cpp index 7eecd66218..a4ff1e178a 100644 --- a/modules/juce_graphics/geometry/juce_Path.cpp +++ b/modules/juce_graphics/geometry/juce_Path.cpp @@ -60,6 +60,9 @@ const float Path::quadMarker = 100003.0f; const float Path::cubicMarker = 100004.0f; const float Path::closeSubPathMarker = 100005.0f; +const float Path::defaultToleranceForTesting = 1.0f; +const float Path::defaultToleranceForMeasurement = 0.6f; + //============================================================================== Path::PathBounds::PathBounds() noexcept : pathXMin (0), pathXMax (0), pathYMin (0), pathYMax (0) @@ -1060,7 +1063,7 @@ bool Path::contains (const Point point, const float tolerance) const return contains (point.x, point.y, tolerance); } -bool Path::intersectsLine (const Line& line, const float tolerance) +bool Path::intersectsLine (Line line, const float tolerance) { PathFlatteningIterator i (*this, AffineTransform(), tolerance); Point intersection; @@ -1072,7 +1075,7 @@ bool Path::intersectsLine (const Line& line, const float tolerance) return false; } -Line Path::getClippedLine (const Line& line, const bool keepSectionOutsidePath) const +Line Path::getClippedLine (Line line, const bool keepSectionOutsidePath) const { Line result (line); const bool startInside = contains (line.getStart()); @@ -1103,10 +1106,10 @@ Line Path::getClippedLine (const Line& line, const bool keepSectio return result; } -float Path::getLength (const AffineTransform& transform) const +float Path::getLength (const AffineTransform& transform, float tolerance) const { float length = 0; - PathFlatteningIterator i (*this, transform); + PathFlatteningIterator i (*this, transform, tolerance); while (i.next()) length += Line (i.x1, i.y1, i.x2, i.y2).getLength(); @@ -1114,9 +1117,11 @@ float Path::getLength (const AffineTransform& transform) const return length; } -Point Path::getPointAlongPath (float distanceFromStart, const AffineTransform& transform) const +Point Path::getPointAlongPath (float distanceFromStart, + const AffineTransform& transform, + float tolerance) const { - PathFlatteningIterator i (*this, transform); + PathFlatteningIterator i (*this, transform, tolerance); while (i.next()) { @@ -1133,9 +1138,10 @@ Point Path::getPointAlongPath (float distanceFromStart, const AffineTrans } float Path::getNearestPoint (const Point targetPoint, Point& pointOnPath, - const AffineTransform& transform) const + const AffineTransform& transform, + float tolerance) const { - PathFlatteningIterator i (*this, transform); + PathFlatteningIterator i (*this, transform, tolerance); float bestPosition = 0, bestDistance = std::numeric_limits::max(); float length = 0; Point pointOnLine; diff --git a/modules/juce_graphics/geometry/juce_Path.h b/modules/juce_graphics/geometry/juce_Path.h index 9a0bfe7063..6ddca5dd18 100644 --- a/modules/juce_graphics/geometry/juce_Path.h +++ b/modules/juce_graphics/geometry/juce_Path.h @@ -83,12 +83,14 @@ public: bool operator== (const Path&) const noexcept; bool operator!= (const Path&) const noexcept; + static const float defaultToleranceForTesting; + static const float defaultToleranceForMeasurement; + //============================================================================== /** Returns true if the path doesn't contain any lines or curves. */ bool isEmpty() const noexcept; - /** Returns the smallest rectangle that contains all points within the path. - */ + /** Returns the smallest rectangle that contains all points within the path. */ Rectangle getBounds() const noexcept; /** Returns the smallest rectangle that contains all points within the path @@ -110,7 +112,7 @@ public: @see closeSubPath, setUsingNonZeroWinding */ bool contains (float x, float y, - float tolerance = 1.0f) const; + float tolerance = defaultToleranceForTesting) const; /** Checks whether a point lies within the path. @@ -126,7 +128,7 @@ public: @see closeSubPath, setUsingNonZeroWinding */ bool contains (const Point point, - float tolerance = 1.0f) const; + float tolerance = defaultToleranceForTesting) const; /** Checks whether a line crosses the path. @@ -138,8 +140,8 @@ public: so this method could return a false positive when your point is up to this distance outside the path's boundary. */ - bool intersectsLine (const Line& line, - float tolerance = 1.0f); + bool intersectsLine (Line line, + float tolerance = defaultToleranceForTesting); /** Cuts off parts of a line to keep the parts that are either inside or outside this path. @@ -153,12 +155,13 @@ public: that will be kept; if false its the section inside the path */ - Line getClippedLine (const Line& line, bool keepSectionOutsidePath) const; + Line getClippedLine (Line line, bool keepSectionOutsidePath) const; /** Returns the length of the path. @see getPointAlongPath */ - float getLength (const AffineTransform& transform = AffineTransform()) const; + float getLength (const AffineTransform& transform = AffineTransform(), + float tolerance = defaultToleranceForMeasurement) const; /** Returns a point that is the specified distance along the path. If the distance is greater than the total length of the path, this will return the @@ -166,15 +169,17 @@ public: @see getLength */ Point getPointAlongPath (float distanceFromStart, - const AffineTransform& transform = AffineTransform()) const; + const AffineTransform& transform = AffineTransform(), + float tolerance = defaultToleranceForMeasurement) const; /** Finds the point along the path which is nearest to a given position. This sets pointOnPath to the nearest point, and returns the distance of this point from the start of the path. */ - float getNearestPoint (const Point targetPoint, + float getNearestPoint (Point targetPoint, Point& pointOnPath, - const AffineTransform& transform = AffineTransform()) const; + const AffineTransform& transform = AffineTransform(), + float tolerance = defaultToleranceForMeasurement) const; //============================================================================== /** Removes all lines and curves, resetting the path completely. */ diff --git a/modules/juce_graphics/geometry/juce_PathIterator.cpp b/modules/juce_graphics/geometry/juce_PathIterator.cpp index ce3c463101..76663ace05 100644 --- a/modules/juce_graphics/geometry/juce_PathIterator.cpp +++ b/modules/juce_graphics/geometry/juce_PathIterator.cpp @@ -26,8 +26,6 @@ #pragma optimize ("t", on) #endif -const float PathFlatteningIterator::defaultTolerance = 0.6f; - //============================================================================== PathFlatteningIterator::PathFlatteningIterator (const Path& path_, const AffineTransform& transform_, diff --git a/modules/juce_graphics/geometry/juce_PathIterator.h b/modules/juce_graphics/geometry/juce_PathIterator.h index 6994e28c32..0e0c5de795 100644 --- a/modules/juce_graphics/geometry/juce_PathIterator.h +++ b/modules/juce_graphics/geometry/juce_PathIterator.h @@ -53,7 +53,7 @@ public: */ PathFlatteningIterator (const Path& path, const AffineTransform& transform = AffineTransform(), - float tolerance = defaultTolerance); + float tolerance = Path::defaultToleranceForMeasurement); /** Destructor. */ ~PathFlatteningIterator(); @@ -90,9 +90,6 @@ public: /** Returns true if the current segment is the last in the current sub-path. */ bool isLastInSubpath() const noexcept; - /** This is the default value that should be used for the tolerance value (see the constructor parameters). */ - static const float defaultTolerance; - private: //============================================================================== const Path& path; diff --git a/modules/juce_graphics/geometry/juce_PathStrokeType.cpp b/modules/juce_graphics/geometry/juce_PathStrokeType.cpp index 7f917f629b..c0dd878983 100644 --- a/modules/juce_graphics/geometry/juce_PathStrokeType.cpp +++ b/modules/juce_graphics/geometry/juce_PathStrokeType.cpp @@ -570,7 +570,7 @@ namespace PathStrokeHelpers // Iterate the path, creating a list of the // left/right-hand lines along either side of it... - PathFlatteningIterator it (*sourcePath, transform, PathFlatteningIterator::defaultTolerance / extraAccuracy); + PathFlatteningIterator it (*sourcePath, transform, Path::defaultToleranceForMeasurement / extraAccuracy); Array subPath; subPath.ensureStorageAllocated (512); @@ -668,7 +668,7 @@ void PathStrokeType::createDashedStroke (Path& destPath, return; Path newDestPath; - PathFlatteningIterator it (sourcePath, transform, PathFlatteningIterator::defaultTolerance / extraAccuracy); + PathFlatteningIterator it (sourcePath, transform, Path::defaultToleranceForMeasurement / extraAccuracy); bool first = true; int dashNum = 0;