Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rtextures] Adding ImageDrawLineEx function #4097

Merged
merged 2 commits into from
Jun 24, 2024
Merged

Conversation

Bigfoot71
Copy link
Contributor

Description

I propose adding the ImageDrawLineEx function with the same signature as DrawLineEx from rshapes.c, enabling line rendering with specified thickness on an Image. This might not be the most efficient method used here, but it is the most concise, understandable, and maintainable, and it is fully functional.

In this PR, I have also revised the ImageDrawLine function to be shorter and more readable in my opinion, thus more maintainable as well. It avoids the use of float types entirely, performing all calculations using integers and utilizing a bit-shift trick for slope calculation.

Additionally, I took the liberty of modifying the ImageDrawLineV function to round input coordinates to the nearest integer using (int)(xfloat + 0.5f), which I find preferable. However, I can revert this change to the original state if there are objections to this approach.

Basic Example

#include "raylib.h"
#include <math.h>

int main(void)
{
    InitWindow(800, 600, "Lines");

    Image image = GenImageColor(800, 600, BLACK);
    Texture texture = LoadTextureFromImage(image);

    Vector2 p1 = { 0 };
    Vector2 p2 = { 0 };

    while (!WindowShouldClose())
    {
        BeginDrawing();

        ImageClearBackground(&image, BLACK);

        p1.x = 400 + cosf(GetTime())*1000;
        p1.y = 300 - sinf(GetTime())*1000;
        p2.x = 400 + cosf(GetTime() + PI)*1000;
        p2.y = 300 - sinf(GetTime() + PI)*1000;

        ImageDrawLineEx(&image, p1, p2, 8, BLUE);
        ImageDrawLineEx(&image, (Vector2) { 0, 0 }, (Vector2) { 800, 600 }, 8, RED);
        ImageDrawLineEx(&image, (Vector2) { 800, 0 }, (Vector2) { 0, 600 }, 8, RED);

        UpdateTexture(texture, image.data);
        DrawTexture(texture, 0, 0, WHITE);

        EndDrawing();
    }

    CloseWindow();

    return 0;
}

GIF Example

screenrec001

also review other functions for drawing lines in images
@raysan5 raysan5 merged commit c2df169 into raysan5:master Jun 24, 2024
14 checks passed
@raysan5
Copy link
Owner

raysan5 commented Jun 24, 2024

@Bigfoot71 Nice! Thanks for the addition!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants