[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;
} |
|
@MatthewRoush thanks for the review, change looks good to mee, thanks for the test example @Gooh456 thanks for the review, feel free to send a PR if you think it requires further review, as peer the visual result, seems to look good... |
|
Sounds good, agreed the current result looks right visually. I'll open a follow-up PR if anything else turns up during further review. |
|
The proposed formula is the correct one, not just a stylistic alternative — worth spelling out why. For a regular polygon with n sides, the apothem (perpendicular distance from center to an edge) is
That's exactly your proposed formula ( The original formula shrinks the circumradius by So yeah - please do submit the PR with the apothem-based formula, it's the geometrically correct fix, not just a preference. |
|
Sounds good, thanks for clarifying the math. |
Follow up to raysan5#6004. The previous formula would produce inconsistent results for different polygons with the same `thick` parameter.
Follow up to raysan5#6004. The previous formula would produce inconsistent results for different polygons with the same `thick` parameter.
|
nice, matches what I derived. one implementation note for whoever writes the actual PR: |
Follow up to #6004. The previous formula would produce inconsistent results for different polygons with the same `thick` parameter.


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.