Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(QgsCircle): Add segment calculation methods #60454

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

lbartoletti
Copy link
Member

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:

  1. 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.

  2. 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.

  3. 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.

  4. 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

@github-actions github-actions bot added this to the 3.42.0 milestone Feb 5, 2025
@lbartoletti lbartoletti added the API API improvement only, no visible user interface changes label Feb 5, 2025
src/core/geometry/qgscircle.h Outdated Show resolved Hide resolved
src/core/geometry/qgscircle.h Show resolved Hide resolved
tests/src/python/test_qgscircle.py Outdated Show resolved Hide resolved
@lbartoletti lbartoletti force-pushed the qgscircle_segmentcalculation branch from c9f12f5 to 7157d36 Compare February 6, 2025 05:21
Copy link

github-actions bot commented Feb 6, 2025

🪟 Windows builds

Download Windows builds of this PR for testing.
Debug symbols for this build are available here.
(Built from commit 875d806)

🪟 Windows Qt6 builds

Download Windows Qt6 builds of this PR for testing.
(Built from commit 875d806)

@lbartoletti lbartoletti self-assigned this Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API API improvement only, no visible user interface changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants