#Bezier curve Gradient search
1 messages · Page 1 of 1 (latest)
float2 ClosestPointOnCurve(CurveSegment curve, float2 Point, float stroke, float tMin, float tMax, float maxAccruacy)
{
float t = (tMin + tMax) / 2;
float delta = (t - tMin) / 2;
float epsilon = 0.001;
float dir = 1;
float count = 0;
float2 s1, s2;
float2 D1, D2;
float d1, d2;
for (count = 0; count < maxAccruacy; count++)
{
s1 = CalculateBezierAt(curve, t);
s2 = CalculateBezierAt(curve, t+epsilon*dir);
D1 = s1 - Point;
D2 = s2 - Point;
d1 = dot(D1, D1);
d2 = dot(D2, D2);
if (d1 < d2)
{
dir = -dir;
}
delta *= 0.67;
t += delta * dir;
t = clamp01(t);
}
return CalculateBezierAt(curve, t);
}
This is my algorithm to find the closest Point on a bezier curve segment, it takes in a curve and a range in which to search, note that range cannot have any extreme Values in the given range,
I just had the idea to limit my search to 1 coordinate instead of searching for the closest Distance, I would now just find the closest X or Y, this makes my curve look janky and I don t understand why.
[21:26]
this is the result I get when searching for only the closest X