Skip to content

Commit 0fc1765

Browse files
authored
Implement miters in DrawSplineLinear (#3585)
* Implement miters in DrawSplineLinear * Follow raylib style
1 parent 9de7986 commit 0fc1765

File tree

1 file changed

+70
-9
lines changed

1 file changed

+70
-9
lines changed

src/rshapes.c

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,26 +1546,87 @@ void DrawPolyLinesEx(Vector2 center, int sides, float radius, float rotation, fl
15461546
// Draw spline: linear, minimum 2 points
15471547
void DrawSplineLinear(Vector2 *points, int pointCount, float thick, Color color)
15481548
{
1549-
Vector2 delta = { 0 };
1550-
float length = 0.0f;
1551-
float scale = 0.0f;
1549+
if (pointCount < 2)
1550+
{
1551+
return;
1552+
}
1553+
1554+
Vector2 prevNormal = (Vector2){-(points[1].y - points[0].y), (points[1].x - points[0].x)};
1555+
float prevLength = sqrtf(prevNormal.x*prevNormal.x + prevNormal.y*prevNormal.y);
1556+
if (prevLength > 0.0f)
1557+
{
1558+
prevNormal.x /= prevLength;
1559+
prevNormal.y /= prevLength;
1560+
}
1561+
else
1562+
{
1563+
prevNormal.x = 0.0f;
1564+
prevNormal.y = 0.0f;
1565+
}
15521566

1567+
Vector2 prevRadius = {.5f * thick * prevNormal.x, .5f * thick * prevNormal.y};
1568+
15531569
for (int i = 0; i < pointCount - 1; i++)
15541570
{
1555-
delta = (Vector2){ points[i + 1].x - points[i].x, points[i + 1].y - points[i].y };
1556-
length = sqrtf(delta.x*delta.x + delta.y*delta.y);
1571+
Vector2 normal = {0};
1572+
1573+
if (i < pointCount - 2)
1574+
{
1575+
normal = (Vector2){-(points[i+2].y - points[i+1].y), (points[i+2].x - points[i+1].x)};
1576+
float normalLength = sqrtf(normal.x*normal.x + normal.y*normal.y);
1577+
if (normalLength > 0.0f)
1578+
{
1579+
normal.x /= normalLength;
1580+
normal.y /= normalLength;
1581+
}
1582+
else
1583+
{
1584+
normal.x = 0.0f;
1585+
normal.y = 0.0f;
1586+
}
1587+
}
1588+
else
1589+
{
1590+
normal = prevNormal;
1591+
}
1592+
1593+
Vector2 radius = {prevNormal.x + normal.x, prevNormal.y + normal.y};
1594+
float radiusLength = sqrtf(radius.x*radius.x + radius.y*radius.y);
1595+
if (radiusLength > 0.0f)
1596+
{
1597+
radius.x /= radiusLength;
1598+
radius.y /= radiusLength;
1599+
}
1600+
else
1601+
{
1602+
radius.x = 0.0f;
1603+
radius.y = 0.0f;
1604+
}
15571605

1558-
if (length > 0) scale = thick/(2*length);
1606+
float cosTheta = radius.x * normal.x + radius.y * normal.y;
15591607

1560-
Vector2 radius = { -scale*delta.y, scale*delta.x };
1608+
if (cosTheta != 0.0f)
1609+
{
1610+
radius.x *= thick * .5f / cosTheta;
1611+
radius.y *= thick * .5f / cosTheta;
1612+
}
1613+
else
1614+
{
1615+
radius.x = 0.0f;
1616+
radius.y = 0.0f;
1617+
}
1618+
15611619
Vector2 strip[4] = {
1562-
{ points[i].x - radius.x, points[i].y - radius.y },
1563-
{ points[i].x + radius.x, points[i].y + radius.y },
1620+
{ points[i].x - prevRadius.x, points[i].y - prevRadius.y },
1621+
{ points[i].x + prevRadius.x, points[i].y + prevRadius.y },
15641622
{ points[i + 1].x - radius.x, points[i + 1].y - radius.y },
15651623
{ points[i + 1].x + radius.x, points[i + 1].y + radius.y }
15661624
};
15671625

15681626
DrawTriangleStrip(strip, 4, color);
1627+
1628+
prevRadius = radius;
1629+
prevNormal = normal;
15691630
}
15701631
#if defined(SUPPORT_SPLINE_SEGMENT_CAPS)
15711632

0 commit comments

Comments
 (0)