[rshapes] Fix DrawPolyLinesEx()#6004
Conversation
If `innerRadius` is less than 0, it causes the outline to overlap itself and even grow outside of the polygon's bounds when `thick` is greater than `radius`. Clamping `innerRadius` to 0 fixes these problems.
|
The Separately (pre-existing, not introduced by this PR, but it's on the exact line you're already touching): Checked this numerically (compiled the same formula in a standalone C program) for
|
|
Yeah, that also looked weird to me as well. I think you would want to use the apothem for good results, because ideally the line thickness is measured from the edges and not the vertices. This produces good visual results with the test program at the bottom. float apothem = radius*cosf(DEG2RAD*180.0f/(float)sides);
float innerRadius = fmaxf(0.0f, radius - thick*(radius/apothem));I don't know how mathematically correct this formula is, but it seems to work out nicely. Test program #include "raylib.h"
int main(void) {
InitWindow(800, 450, "Test");
SetTargetFPS(60);
int thick = 2;
while (!WindowShouldClose()) {
if (IsKeyPressed(KEY_UP)) thick = thick + 1;
if (IsKeyPressed(KEY_DOWN)) thick = thick - 1;
thick = (thick < 1) ? 1 : (thick > 60) ? 60 : thick;
BeginDrawing();
ClearBackground(DARKGRAY);
DrawText(TextFormat("thick = %d", thick), 10, 300, 20, GREEN);
// These should match (except for maybe some floating point rounding)
DrawRectangle(10, 10, 40, 40, LIGHTGRAY);
DrawRectangleLinesEx((Rectangle){10, 10, 40, 40}, (float)thick, BLUE);
DrawPoly((Vector2){30.0f, 80.0f}, 4, 28.284f, 45.0f, LIGHTGRAY);
DrawPolyLinesEx((Vector2){30.0f, 80.0f}, 4, 28.284f, 45.0f, (float)thick, BLUE);
// These should match (except for maybe some floating point rounding)
DrawCircle(80, 30, 20.0f, LIGHTGRAY);
DrawCircleLinesEx((Vector2){80.0f, 30.0f}, 20.0f, (float)thick, BLUE);
DrawPoly((Vector2){80.0f, 80.0f}, 36, 20.0f, 0.0f, LIGHTGRAY);
DrawPolyLinesEx((Vector2){80.0f, 80.0f}, 36, 20.0f, 0.0f, (float)thick, BLUE);
// Some other shapes
DrawPoly((Vector2){190.0f, 70.0f}, 3, 60.0f, 0.0f, LIGHTGRAY);
DrawPolyLinesEx((Vector2){190.0f, 70.0f}, 3, 60.0f, 0.0f, (float)thick, BLUE);
DrawPoly((Vector2){330.0f, 70.0f}, 4, 60.0f, 0.0f, LIGHTGRAY);
DrawPolyLinesEx((Vector2){330.0f, 70.0f}, 4, 60.0f, 0.0f, (float)thick, BLUE);
DrawPoly((Vector2){470.0f, 70.0f}, 5, 60.0f, 0.0f, LIGHTGRAY);
DrawPolyLinesEx((Vector2){470.0f, 70.0f}, 5, 60.0f, 0.0f, (float)thick, BLUE);
DrawPoly((Vector2){610.0f, 70.0f}, 6, 60.0f, 0.0f, LIGHTGRAY);
DrawPolyLinesEx((Vector2){610.0f, 70.0f}, 6, 60.0f, 0.0f, (float)thick, BLUE);
DrawPoly((Vector2){190.0f, 210.0f}, 7, 60.0f, 0.0f, LIGHTGRAY);
DrawPolyLinesEx((Vector2){190.0f, 210.0f}, 7, 60.0f, 0.0f, (float)thick, BLUE);
DrawPoly((Vector2){330.0f, 210.0f}, 8, 60.0f, 0.0f, LIGHTGRAY);
DrawPolyLinesEx((Vector2){330.0f, 210.0f}, 8, 60.0f, 0.0f, (float)thick, BLUE);
DrawPoly((Vector2){470.0f, 210.0f}, 9, 60.0f, 0.0f, LIGHTGRAY);
DrawPolyLinesEx((Vector2){470.0f, 210.0f}, 9, 60.0f, 0.0f, (float)thick, BLUE);
DrawPoly((Vector2){610.0f, 210.0f}, 10, 60.0f, 0.0f, LIGHTGRAY);
DrawPolyLinesEx((Vector2){610.0f, 210.0f}, 10, 60.0f, 0.0f, (float)thick, BLUE);
EndDrawing();
}
CloseWindow();
return 0;
} |
If
innerRadiusis less than 0, it causes the outline to overlap itself and even grow outside of the polygon's bounds whenthickis greater thanradius. ClampinginnerRadiusto 0 fixes these problems.