0

I draw Bezier Lines with the QuadTo-Method on a SKPath. My requirement is to get a point on the rendered Bezier Line which is more or less in the middle of the line. I use this point to show a label on the line and to provide a hit point to interact with the line.
Currently, I shoehorned a simple algorithm which tries to resolve the points of the line vie GetFillPath and then try to detect the point which is nearest to the center of a thought straight line from the Bezier’s start and end.
This works, however, it feels extremely brutish. Is there a more sophisticated way to fulfill my requirement?

HCL
  • 36,053
  • 27
  • 163
  • 213
  • Does the case helpful : https://stackoverflow.com/questions/23596802/calculate-middle-point-of-bezier-curve ? – ColeX Apr 26 '22 at 05:33

1 Answers1

0

The quadratic Bezier has a pretty simple parametric formula to calculate points lying on it, you can find it in many places, e.g. https://en.wikipedia.org/wiki/B%C3%A9zier_curve

Bezier formula

As per description of your problem it is important to notice, that you don't specifally want to calculate the point B(0.5), as the length of curves (P0, B(0.5)) and (B(0.5), P2) can be different for non-symmetric Beziers. What I would do is:

  1. Flatten the curve to a polyline with an arbitrary precision p (selecting all the t params so that for every 3 consecutive t such that: tk < tk+1 < tk+2 distance between line (Pk, Pk+2) and point B(tk+1) < p)
  2. Find k such that: L(P0, ..., B(tk)) <= L(P0,..., P2)/2 < L(B(tk+1), ..., P2), where L is the length of polyline.
  3. Now with found k we know our middle point tmid fulfills tk <= tmid < tk+1. This can again be computed with an arbitrary precision.

For any higher degree Beziers you can use de Casteljau algorithm to calculate points for any given 0 <= t <= 1.

Maku
  • 1,464
  • 11
  • 20