summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorToctave <[email protected]>2023-12-15 18:34:34 +0100
committerGitHub <[email protected]>2023-12-15 18:34:34 +0100
commit0fc1765ff3849d3b1fb8a1b193c48ea96cee2664 (patch)
tree758ddcb64130eb722603cf45aafeabcdacd08e16 /src
parent9de79861ea289822ce965b63e565b04a46c0ec11 (diff)
downloadraylib-0fc1765ff3849d3b1fb8a1b193c48ea96cee2664.tar.gz
raylib-0fc1765ff3849d3b1fb8a1b193c48ea96cee2664.zip
Implement miters in DrawSplineLinear (#3585)
* Implement miters in DrawSplineLinear * Follow raylib style
Diffstat (limited to 'src')
-rw-r--r--src/rshapes.c79
1 files changed, 70 insertions, 9 deletions
diff --git a/src/rshapes.c b/src/rshapes.c
index 4b3f2cfd..b7fc0ed7 100644
--- a/src/rshapes.c
+++ b/src/rshapes.c
@@ -1546,26 +1546,87 @@ void DrawPolyLinesEx(Vector2 center, int sides, float radius, float rotation, fl
// Draw spline: linear, minimum 2 points
void DrawSplineLinear(Vector2 *points, int pointCount, float thick, Color color)
{
- Vector2 delta = { 0 };
- float length = 0.0f;
- float scale = 0.0f;
+ if (pointCount < 2)
+ {
+ return;
+ }
+
+ Vector2 prevNormal = (Vector2){-(points[1].y - points[0].y), (points[1].x - points[0].x)};
+ float prevLength = sqrtf(prevNormal.x*prevNormal.x + prevNormal.y*prevNormal.y);
+ if (prevLength > 0.0f)
+ {
+ prevNormal.x /= prevLength;
+ prevNormal.y /= prevLength;
+ }
+ else
+ {
+ prevNormal.x = 0.0f;
+ prevNormal.y = 0.0f;
+ }
+ Vector2 prevRadius = {.5f * thick * prevNormal.x, .5f * thick * prevNormal.y};
+
for (int i = 0; i < pointCount - 1; i++)
{
- delta = (Vector2){ points[i + 1].x - points[i].x, points[i + 1].y - points[i].y };
- length = sqrtf(delta.x*delta.x + delta.y*delta.y);
+ Vector2 normal = {0};
+
+ if (i < pointCount - 2)
+ {
+ normal = (Vector2){-(points[i+2].y - points[i+1].y), (points[i+2].x - points[i+1].x)};
+ float normalLength = sqrtf(normal.x*normal.x + normal.y*normal.y);
+ if (normalLength > 0.0f)
+ {
+ normal.x /= normalLength;
+ normal.y /= normalLength;
+ }
+ else
+ {
+ normal.x = 0.0f;
+ normal.y = 0.0f;
+ }
+ }
+ else
+ {
+ normal = prevNormal;
+ }
+
+ Vector2 radius = {prevNormal.x + normal.x, prevNormal.y + normal.y};
+ float radiusLength = sqrtf(radius.x*radius.x + radius.y*radius.y);
+ if (radiusLength > 0.0f)
+ {
+ radius.x /= radiusLength;
+ radius.y /= radiusLength;
+ }
+ else
+ {
+ radius.x = 0.0f;
+ radius.y = 0.0f;
+ }
- if (length > 0) scale = thick/(2*length);
+ float cosTheta = radius.x * normal.x + radius.y * normal.y;
- Vector2 radius = { -scale*delta.y, scale*delta.x };
+ if (cosTheta != 0.0f)
+ {
+ radius.x *= thick * .5f / cosTheta;
+ radius.y *= thick * .5f / cosTheta;
+ }
+ else
+ {
+ radius.x = 0.0f;
+ radius.y = 0.0f;
+ }
+
Vector2 strip[4] = {
- { points[i].x - radius.x, points[i].y - radius.y },
- { points[i].x + radius.x, points[i].y + radius.y },
+ { points[i].x - prevRadius.x, points[i].y - prevRadius.y },
+ { points[i].x + prevRadius.x, points[i].y + prevRadius.y },
{ points[i + 1].x - radius.x, points[i + 1].y - radius.y },
{ points[i + 1].x + radius.x, points[i + 1].y + radius.y }
};
DrawTriangleStrip(strip, 4, color);
+
+ prevRadius = radius;
+ prevNormal = normal;
}
#if defined(SUPPORT_SPLINE_SEGMENT_CAPS)