Add ImBezierClosestPoint() function which returns a point on bezier curve which is closed to a specified point.

This commit is contained in:
Rokas Kupstys
2019-12-17 16:29:12 +02:00
committed by omar
parent 0f7105e156
commit 3831d50ab9
3 changed files with 81 additions and 9 deletions

View File

@ -907,6 +907,17 @@ void ImDrawList::PathArcTo(const ImVec2& center, float radius, float a_min, floa
}
}
ImVec2 ImBezierCalc(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, float t)
{
float u = 1.0f - t;
float w1 = u*u*u;
float w2 = 3*u*u*t;
float w3 = 3*u*t*t;
float w4 = t*t*t;
return ImVec2(w1*p1.x + w2*p2.x + w3*p3.x + w4*p4.x, w1*p1.y + w2*p2.y + w3*p3.y + w4*p4.y);
}
// Closely mimicked by ClosestPointBezierCasteljau() in imgui.cpp
static void PathBezierToCasteljau(ImVector<ImVec2>* path, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float tess_tol, int level)
{
float dx = x4 - x1;
@ -945,15 +956,7 @@ void ImDrawList::PathBezierCurveTo(const ImVec2& p2, const ImVec2& p3, const ImV
{
float t_step = 1.0f / (float)num_segments;
for (int i_step = 1; i_step <= num_segments; i_step++)
{
float t = t_step * i_step;
float u = 1.0f - t;
float w1 = u*u*u;
float w2 = 3*u*u*t;
float w3 = 3*u*t*t;
float w4 = t*t*t;
_Path.push_back(ImVec2(w1*p1.x + w2*p2.x + w3*p3.x + w4*p4.x, w1*p1.y + w2*p2.y + w3*p3.y + w4*p4.y));
}
_Path.push_back(ImBezierCalc(p1, p2, p3, p4, t_step * i_step));
}
}