RoundSuperellipse algorithm v3: Ultrawideband heuristic formula (#164755)
This PR revises the algorithm for RoundSuperellipses, replacing the
current "max ratio" approximation with an algorithm that works for
ratios from 2.0 to infinity.
The previous "max ratio" approximation, which replaces the middle of
edges with straight lines when the ratio is above 2.3, turns out to
produce results too close to classic RRects. After reexamining the
shapes and more calculation, I discovered that the max-ratio
approximation is flawed. Even squircles with with really high ratios
(~100) have a significant part of the edges that must not be
approximated by straight lines.
The new version is much closer to native.
### Comparison
Native: (Notice the long wedgy gap at the end of curves)
<img
src="https://github.com/user-attachments/assets/61b60191-7d45-4c49-9e09-b0422243cd8c"
width="400"/>
Before PR: (Notice the short wedgy gap at the end of curves)
<img
src="https://github.com/user-attachments/assets/15ea374b-4b16-4187-aaa4-94f432fbb61e"
width="400"/>
After PR:
<img
src="https://github.com/user-attachments/assets/973ef4d1-7c26-44a9-b45e-10d109d5618b"
width="400"/>
Another example (after PR). Even though the rectangular RSE has ratios
of around 4, there are still curvature near the middle section of edges,
which can be identified with the help of antialias pixels.
<img width="838" alt="image"
src="https://github.com/user-attachments/assets/5078d098-c582-48a8-81e5-615909def675"
/>
### Details
I found that `n` has really good linearity towards larger ratios.
<img width="844" alt="image"
src="https://github.com/user-attachments/assets/73e99e45-a0f0-450b-8e2b-f6fd97082958"
/>
I also found a good candidate for the precomputed unknown (called
`k_xJ`), which has a smooth curve at the beginning and almost straight
line towards larger `n`, removing the need to cap the scope of
application of the formula.
<img width="1203" alt="image"
src="https://github.com/user-attachments/assets/67664898-2dbd-4f00-a9ba-d76030cf3742"
/>
The algorithm for paths are also updated in a similar way and
approximated the Bezier factors with heuristic formulae for bigger `n`s.
I've also verified that the path deviates from the geometry by no more
than 0.01% over the range of n [15, 100]
Theoretically removing "stretch" should simplify the algorithms.
Unfortunately I had to spend more lines to process cases of zero radii,
which were conveniently handled by stretches.
## Pre-launch Checklist
- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.
If you need help, consider asking for advice on the #hackers-new channel
on [Discord].
<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md