feat(QgsCircle): Add segment calculation methods #60454
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
In this PR, I'm adding several new methods to compute the "good number of segments" needed to discretize a circle. I’ve often seen people significantly increasing the segment count for circles because the default value of 36 is a bit on the low side. In my humble opinion, around 64 segments makes for a good compromise. Regardless, with these new methods, you now really have the choice to pick what works best for your needs.
The following methods have been introduced:
Standard (Sagitta-based):
Uses the sagitta formula to calculate the segment angle, ensuring that the maximum deviation between the circle and its polygonal approximation stays within a specified tolerance. This is our traditional, tried-and-true approach.
Adaptive:
Adjusts the tolerance dynamically based on the circle’s radius. For smaller circles, it decreases the tolerance to provide better visual quality, while for larger circles, it increases the tolerance to avoid an excessive number of segments. This method ensures a smooth transition across different scales.
AreaError:
Determines the number of segments based on the acceptable area error between the actual circle and its regular polygon approximation. By using a Taylor series approximation for the sine function, this approach guarantees that the relative area error remains within user-defined limits.
ConstantDensity:
Implements a simple linear scaling between the circle’s radius and the number of segments. Although this method doesn’t provide precise error control like the others, it offers an intuitive and fast approximation when exact bounds aren’t critical.
Additionally, I’ve added a test to facilitate comparisons between these methods. For your reference, I’ve attached the results visualized in a GPKG.
Thanks to @ptitjano to reviewing my maths
circle_segments.zip