From 3fc4a4c9749c61d02b0d9f3bebde26252a582abb Mon Sep 17 00:00:00 2001 From: SAOMDVN <34767287+SAOMDVN@users.noreply.github.com> Date: Sun, 3 Oct 2021 17:15:56 +0700 Subject: [PATCH] Added DrawLineBezierCubic() (#2021) Co-authored-by: SAOMDVN --- src/raylib.h | 1 + src/rshapes.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/raylib.h b/src/raylib.h index cd41cea6..a925f73e 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1148,6 +1148,7 @@ RLAPI void DrawLineV(Vector2 startPos, Vector2 endPos, Color color); RLAPI void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color); // Draw a line defining thickness RLAPI void DrawLineBezier(Vector2 startPos, Vector2 endPos, float thick, Color color); // Draw a line using cubic-bezier curves in-out RLAPI void DrawLineBezierQuad(Vector2 startPos, Vector2 endPos, Vector2 controlPos, float thick, Color color); //Draw line using quadratic bezier curves with a control point +RLAPI void DrawLineBezierCubic(Vector2 startPos, Vector2 endPos, Vector2 startControlPos, Vector2 endControlPos, float thick, Color color) //Draw line using cubic bezier curves with 2 control points RLAPI void DrawLineStrip(Vector2 *points, int pointCount, Color color); // Draw lines sequence RLAPI void DrawCircle(int centerX, int centerY, float radius, Color color); // Draw a color-filled circle RLAPI void DrawCircleSector(Vector2 center, float radius, float startAngle, float endAngle, int segments, Color color); // Draw a piece of a circle diff --git a/src/rshapes.c b/src/rshapes.c index 605cdad4..f42d5621 100644 --- a/src/rshapes.c +++ b/src/rshapes.c @@ -189,6 +189,32 @@ void DrawLineBezierQuad(Vector2 startPos, Vector2 endPos, Vector2 controlPos, fl } } +//Draw line using cubic bezier curves with 2 control points +void DrawLineBezierCubic(Vector2 startPos, Vector2 endPos, Vector2 startControlPos, Vector2 endControlPos, float thick, Color color) +{ + const float step = 1.0f/BEZIER_LINE_DIVISIONS; + + Vector2 previous = startPos; + Vector2 current = { 0 }; + float t = 0.0f; + + for (int i = 0; i <= BEZIER_LINE_DIVISIONS; i++) + { + t = step*i; + float a = powf(1 - t, 3); + float b = 3*powf(1 - t, 2)*t; + float c = 3*(1-t)*powf(t, 2); + float d = powf(t, 3); + + current.y = a*startPos.y + b*startControlPos.y + c*endControlPos.y + d*endPos.y; + current.x = a*startPos.x + b*startControlPos.x + c*endControlPos.x + d*endPos.x; + + DrawLineEx(previous, current, thick, color); + + previous = current; + } +} + // Draw lines sequence void DrawLineStrip(Vector2 *points, int pointCount, Color color) {